-
Notifications
You must be signed in to change notification settings - Fork 53
/
backend.go
55 lines (45 loc) · 1.19 KB
/
backend.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
package weaver
import (
"net"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/gojektech/weaver/config"
"github.com/pkg/errors"
)
type Backend struct {
Handler http.Handler
Server *url.URL
Name string
}
type BackendOptions struct {
Timeout time.Duration
}
func NewBackend(name string, serverURL string, options BackendOptions) (*Backend, error) {
server, err := url.Parse(serverURL)
if err != nil {
return nil, errors.Wrapf(err, "URL Parsing failed for: %s", serverURL)
}
return &Backend{
Name: name,
Handler: newWeaverReverseProxy(server, options),
Server: server,
}, nil
}
func newWeaverReverseProxy(target *url.URL, options BackendOptions) *httputil.ReverseProxy {
proxyConfig := config.Proxy()
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: options.Timeout,
KeepAlive: proxyConfig.ProxyDialerKeepAliveInMS(),
DualStack: true,
}).DialContext,
MaxIdleConns: proxyConfig.ProxyMaxIdleConns(),
IdleConnTimeout: proxyConfig.ProxyIdleConnTimeoutInMS(),
DisableKeepAlives: !proxyConfig.KeepAliveEnabled(),
}
return proxy
}