-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_handler.go
69 lines (63 loc) · 1.9 KB
/
forward_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
package aggregadantur
import (
"crypto/tls"
"github.com/orange-cloudfoundry/aggregadantur/models"
log "github.com/sirupsen/logrus"
"github.com/vulcand/oxy/buffer"
"github.com/vulcand/oxy/forward"
"net"
"net/http"
"time"
)
type ForwardHandler struct {
aggrRoute *models.AggregateRoute
reverseHandler http.Handler
}
func NewForwardHandler(aggrRoute *models.AggregateRoute) (*ForwardHandler, error) {
reverseHandler, err := createReverseHandler(aggrRoute)
if err != nil {
return nil, err
}
return &ForwardHandler{
aggrRoute: aggrRoute,
reverseHandler: reverseHandler,
}, nil
}
func (f ForwardHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
f.aggrRoute.InjectForwardUrl(req)
f.reverseHandler.ServeHTTP(w, req)
}
func createReverseHandler(proxyRoute *models.AggregateRoute) (http.Handler, error) {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: proxyRoute.InsecureSkipVerify,
},
}
entry := log.WithField("route_name", proxyRoute.Name)
var err error
var fwd *forward.Forwarder
if !proxyRoute.NoBuffer {
entry.Debug("orange-cloudfoundry/aggregadantur/proxy: Handler for routes will use buffer.")
fwd, err = forward.New(forward.RoundTripper(transport))
} else {
entry.Debug("orange-cloudfoundry/aggregadantur/proxy: Handler for routes will use direct stream.")
fwd, err = forward.New(forward.RoundTripper(transport), forward.Stream(true))
}
if err != nil {
return nil, err
}
if proxyRoute.NoBuffer {
return fwd, nil
}
return buffer.New(fwd, buffer.Retry(`IsNetworkError() && Attempts() < 2`))
}