-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
148 lines (120 loc) · 3.34 KB
/
client.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
139
140
141
142
143
144
145
146
147
148
package http
import (
"net/http"
r "github.com/alexfalkowski/go-service/retry"
"github.com/alexfalkowski/go-service/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport/http/breaker"
"github.com/alexfalkowski/go-service/transport/http/meta"
"github.com/alexfalkowski/go-service/transport/http/retry"
lzap "github.com/alexfalkowski/go-service/transport/http/telemetry/logger/zap"
"github.com/alexfalkowski/go-service/transport/http/telemetry/metrics"
htracer "github.com/alexfalkowski/go-service/transport/http/telemetry/tracer"
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
)
// ClientOption for HTTP.
type ClientOption interface{ apply(opts *clientOptions) }
type clientOptions struct {
logger *zap.Logger
tracer htracer.Tracer
meter metric.Meter
retry *r.Config
userAgent string
breaker bool
roundTripper http.RoundTripper
}
type clientOptionFunc func(*clientOptions)
func (f clientOptionFunc) apply(o *clientOptions) { f(o) }
// WithClientRoundTripper for HTTP.
func WithClientRoundTripper(rt http.RoundTripper) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.roundTripper = rt
})
}
// WithClientRetry for HTTP.
func WithClientRetry(cfg *r.Config) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.retry = cfg
})
}
// WithClientBreaker for HTTP.
func WithClientBreaker() ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.breaker = true
})
}
// WithClientLogger for HTTP.
func WithClientLogger(logger *zap.Logger) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.logger = logger
})
}
// WithClientTracer for HTTP.
func WithClientTracer(tracer htracer.Tracer) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.tracer = tracer
})
}
// WithClientMetrics for HTTP.
func WithClientMetrics(meter metric.Meter) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.meter = meter
})
}
// WithUserAgent for HTTP.
func WithClientUserAgent(userAgent string) ClientOption {
return clientOptionFunc(func(o *clientOptions) {
o.userAgent = userAgent
})
}
// NewRoundTripper for HTTP.
func NewRoundTripper(opts ...ClientOption) (http.RoundTripper, error) {
os := &clientOptions{tracer: tracer.NewNoopTracer("http")}
for _, o := range opts {
o.apply(os)
}
hrt := os.roundTripper
if hrt == nil {
hrt = Transport()
}
if os.retry != nil {
hrt = retry.NewRoundTripper(os.retry, hrt)
}
if os.breaker {
hrt = breaker.NewRoundTripper(hrt)
}
if os.logger != nil {
hrt = lzap.NewRoundTripper(os.logger, hrt)
}
if os.meter != nil {
rt, err := metrics.NewRoundTripper(os.meter, hrt)
if err != nil {
return nil, err
}
hrt = rt
}
hrt = htracer.NewRoundTripper(os.tracer, hrt)
hrt = meta.NewRoundTripper(os.userAgent, hrt)
return hrt, nil
}
// NewClient for HTTP.
func NewClient(opts ...ClientOption) (*http.Client, error) {
defaultOptions := &clientOptions{tracer: tracer.NewNoopTracer("http")}
for _, o := range opts {
o.apply(defaultOptions)
}
rt, err := NewRoundTripper(opts...)
if err != nil {
return nil, err
}
client := &http.Client{Transport: rt}
return client, nil
}
// Transport for HTTP.
func Transport() *http.Transport {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100
return t
}