-
Notifications
You must be signed in to change notification settings - Fork 17
/
handler.go
138 lines (112 loc) · 3.11 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package delta
import (
"bytes"
"io"
"log"
"net/http"
"time"
)
type Handler struct {
server *Server
}
func NewHandler(server *Server) *Handler {
return &Handler{
server: server,
}
}
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
backendNames := handler.server.onSelectBackendHandler(req)
backendCount := len(backendNames)
masterResponseCh := make(chan *Response, 1)
responseCh := make(chan *Response, backendCount)
done := make(chan bool)
bodies := make(map[string]io.Reader)
if req.Body != nil {
writers := make([]io.Writer, len(backendNames))
for i, name := range backendNames {
b := new(bytes.Buffer)
writers[i] = b
bodies[name] = b
}
io.Copy(io.MultiWriter(writers...), req.Body)
}
for _, name := range backendNames {
backend := handler.server.Backends[name]
go handler.dispatchProxyRequest(backend, req, bodies[name], masterResponseCh, responseCh)
}
// Wait for all responses asynchronously
go func() {
responses := make(map[string]*Response)
requestCount := 0
for {
response := <-responseCh
requestCount = requestCount + 1
if response != nil {
responses[response.Backend.Name] = response
}
if requestCount >= backendCount {
if handler.server.onBackendFinishedHandler != nil {
handler.server.onBackendFinishedHandler(responses)
}
done <- true
break
}
}
}()
// Wait for only master response in a blocking way
response := <-masterResponseCh
if response == nil {
http.Error(writer, "Internal Server Error", 500)
} else {
for key, values := range response.HttpResponse.Header {
for i := range values {
writer.Header().Add(key, values[i])
}
}
writer.WriteHeader(response.HttpResponse.StatusCode)
_, err := writer.Write(response.Data)
if err != nil {
log.Printf("HTTP Response Write Error: %s\n", err)
}
}
<-done
}
func (handler *Handler) dispatchProxyRequest(backend *Backend, req *http.Request, body io.Reader, masterResponseCh chan *Response, responseCh chan *Response) {
proxyRequest := handler.copyRequest(backend, req, body)
client := new(http.Client)
now := time.Now()
res, err := client.Do(proxyRequest)
elapsed := time.Now().Sub(now)
var response *Response
if err != nil {
log.Printf("HTTP Request Error: %s\n", err)
response = nil
} else {
response, err = NewResponse(backend, res, elapsed)
if err != nil {
log.Printf("HTTP Response Read Error: %s\n", err)
}
}
responseCh <- response
if backend.IsMaster {
masterResponseCh <- response
}
}
func (handler *Handler) copyRequest(backend *Backend, req *http.Request, body io.Reader) *http.Request {
proxyRequest, err := http.NewRequest(req.Method, backend.URL(req.URL.String()), body)
if err != nil {
log.Fatal(err)
}
proxyRequest.Proto = req.Proto
proxyRequest.Host = backend.HostPort()
// Copy deeply because we may modify header later
for key, values := range req.Header {
for i := range values {
proxyRequest.Header.Add(key, values[i])
}
}
if handler.server.onMungeHeaderHandler != nil {
handler.server.onMungeHeaderHandler(backend.Name, &proxyRequest.Header)
}
return proxyRequest
}