-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransport.go
109 lines (91 loc) · 2.64 KB
/
transport.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
package consulresolver
import (
"context"
"fmt"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/friendsofgo/errors"
)
type ServiceAddress struct {
Host string
Port int
}
type Resolver interface {
// Resolve should return a ServiceAddress, or a non-nil error if resolution failed
Resolve(context.Context) (ServiceAddress, error)
// ServiceName should return the name of the service the resolver is providing targets for
// Target resolution will be skipped for hosts that do not match the name returned by ServiceName
ServiceName() string
}
type DialFn func(ctx context.Context, network, addr string) (net.Conn, error)
type LoadBalancedTransport struct {
resolvers map[string]Resolver
base http.RoundTripper
log LogFn
resolverFallback bool
}
func NewLoadBalancedTransport(conf TransportConfig) (*LoadBalancedTransport, error) {
if len(conf.Resolvers) == 0 {
return nil, errors.New("no resolver provided")
}
if conf.Log == nil {
conf.Log = log.Printf
}
var base http.RoundTripper
if conf.Base == nil {
base = getDefaultTransport()
} else {
base = conf.Base
}
resolvers := make(map[string]Resolver, len(conf.Resolvers))
for _, r := range conf.Resolvers {
resolvers[r.ServiceName()] = r
}
return &LoadBalancedTransport{
resolvers: resolvers,
base: base,
log: conf.Log,
resolverFallback: conf.NetResolverFallback,
}, nil
}
func (t *LoadBalancedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
host := strings.Split(req.Host, ":")[0]
r, ok := t.resolvers[host]
if !ok {
t.log("[LoadBalancedTransport] no resolver found for host %s", req.Host)
return t.base.RoundTrip(req)
}
tgt, err := r.Resolve(req.Context())
if err != nil {
t.log("[LoadBalancedTransport] failed resolving target - %s", err.Error())
if t.resolverFallback {
t.log("[LoadBalancedTransport] falling back to default resolver")
return t.base.RoundTrip(req)
}
return nil, err
}
// RoundTrip must not modify the original request - so we clone it
cloned := req.Clone(req.Context())
cloned.URL.Host = fmt.Sprintf("%s:%d", tgt.Host, tgt.Port)
return t.base.RoundTrip(cloned)
}
func getDefaultTransport() *http.Transport {
if h, ok := http.DefaultTransport.(*http.Transport); ok {
return h.Clone()
}
return &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,
}
}