-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathendpoint_provider.go
343 lines (295 loc) · 10.5 KB
/
endpoint_provider.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package endpoints
import (
"context"
"fmt"
"math/rand"
"net/http"
"github.com/uber-go/tally/v4"
"go.uber.org/fx"
"go.uber.org/zap"
"golang.org/x/xerrors"
"github.com/coinbase/chainstorage/internal/config"
"github.com/coinbase/chainstorage/internal/utils/consts"
"github.com/coinbase/chainstorage/internal/utils/log"
"github.com/coinbase/chainstorage/internal/utils/picker"
"github.com/coinbase/chainstorage/internal/utils/utils"
)
type (
// EndpointProvider encapsulates the client-side routing logic.
// By default, the request is routed to one of the primary endpoints based on their weights.
// When the failover context is seen, the request is instead routed to one of the secondary endpoints.
// In most cases, the primary endpoints are referring to the `EndpointGroup.Endpoints` config,
// while the secondary endpoints are referring to the `EndpointGroup.EndpointsFailover` config.
// But when `EndpointGroup.UseFailover` is turned on, the two list are swapped.
EndpointProvider interface {
GetEndpoint(ctx context.Context) (*Endpoint, error)
GetAllEndpoints() []*Endpoint
GetActiveEndpoints(ctx context.Context) []*Endpoint
WithFailoverContext(ctx context.Context) (context.Context, error)
HasFailoverContext(ctx context.Context) bool
}
Endpoint struct {
Name string
Config *config.Endpoint
Client *http.Client
requestsCounter tally.Counter
}
EndpointProviderParams struct {
fx.In
Config *config.Config
Logger *zap.Logger
Scope tally.Scope
}
EndpointProviderResult struct {
fx.Out
Master EndpointProvider `name:"master"`
Slave EndpointProvider `name:"slave"`
Validator EndpointProvider `name:"validator"`
Consensus EndpointProvider `name:"consensus"`
}
)
type (
endpointProvider struct {
name string
endpointGroup *config.EndpointGroup
logger *zap.Logger
scope tally.Scope
primaryPicker picker.Picker
primaryEndpoints []*Endpoint
secondaryPicker picker.Picker
secondaryEndpoints []*Endpoint
}
contextKey string
)
const (
masterEndpointGroupName = "master"
slaveEndpointGroupName = "slave"
validatorEndpointGroupName = "validator"
consensusEndpointGroupName = "consensus"
contextKeyFailover = "failover:"
)
var (
ErrFailoverUnavailable = xerrors.New("no endpoint is available for failover")
)
func NewEndpointProvider(params EndpointProviderParams) (EndpointProviderResult, error) {
logger := log.WithPackage(params.Logger)
scope := params.Scope.SubScope("endpoints")
master, err := newEndpointProvider(logger, params.Config, scope, ¶ms.Config.Chain.Client.Master.EndpointGroup, masterEndpointGroupName)
if err != nil {
return EndpointProviderResult{}, xerrors.Errorf("failed to create master endpoint provider: %w", err)
}
slave, err := newEndpointProvider(logger, params.Config, scope, ¶ms.Config.Chain.Client.Slave.EndpointGroup, slaveEndpointGroupName)
if err != nil {
return EndpointProviderResult{}, xerrors.Errorf("failed to create slave endpoint provider: %w", err)
}
validator, err := newEndpointProvider(logger, params.Config, scope, ¶ms.Config.Chain.Client.Validator.EndpointGroup, validatorEndpointGroupName)
if err != nil {
return EndpointProviderResult{}, xerrors.Errorf("failed to create validator endpoint provider: %w", err)
}
// Consensus client is set as Slave by default
var consensus *endpointProvider
if !params.Config.Chain.Client.Consensus.EndpointGroup.Empty() {
consensus, err = newEndpointProvider(logger, params.Config, scope, ¶ms.Config.Chain.Client.Consensus.EndpointGroup, consensusEndpointGroupName)
if err != nil {
return EndpointProviderResult{}, xerrors.Errorf("failed to create consensus endpoint provider: %w", err)
}
} else {
consensus, err = newEndpointProvider(logger, params.Config, scope, ¶ms.Config.Chain.Client.Slave.EndpointGroup, consensusEndpointGroupName)
if err != nil {
return EndpointProviderResult{}, xerrors.Errorf("failed to create consensus endpoint provider with slave endpoints: %w", err)
}
}
return EndpointProviderResult{
Master: master,
Slave: slave,
Validator: validator,
Consensus: consensus,
}, nil
}
func newEndpointProvider(logger *zap.Logger, cfg *config.Config, scope tally.Scope, endpointGroup *config.EndpointGroup, name string) (*endpointProvider, error) {
primaryEndpoints, primaryPicker, err := newEndpoints(
logger,
cfg,
scope,
endpointGroup.Endpoints,
&endpointGroup.EndpointConfig,
)
if err != nil {
return nil, xerrors.Errorf("failed to create primary endpoints: %w", err)
}
secondaryEndpoints, secondaryPicker, err := newEndpoints(
logger,
cfg,
scope,
endpointGroup.EndpointsFailover,
&endpointGroup.EndpointConfigFailover,
)
if err != nil {
return nil, xerrors.Errorf("failed to create secondary endpoints: %w", err)
}
if endpointGroup.UseFailover {
logger.Warn("using failover endpoints", zap.String("endpoint_group", name))
primaryEndpoints, secondaryEndpoints = secondaryEndpoints, primaryEndpoints
primaryPicker, secondaryPicker = secondaryPicker, primaryPicker
}
return &endpointProvider{
name: name,
endpointGroup: endpointGroup,
logger: logger,
scope: scope,
primaryPicker: primaryPicker,
primaryEndpoints: primaryEndpoints,
secondaryPicker: secondaryPicker,
secondaryEndpoints: secondaryEndpoints,
}, nil
}
func newEndpoints(
logger *zap.Logger,
cfg *config.Config,
scope tally.Scope,
endpoints []config.Endpoint,
endpointConfig *config.EndpointConfig,
) ([]*Endpoint, picker.Picker, error) {
res := make([]*Endpoint, len(endpoints))
for i := range endpoints {
endpoint, err := newEndpoint(cfg, logger, scope, &endpoints[i], endpointConfig)
if err != nil {
return nil, nil, xerrors.Errorf("failed to create endpoint: %w", err)
}
res[i] = endpoint
}
// Shuffle the endpoints to prevent overloading the first endpoint right after a deployment.
rand.Shuffle(len(res), func(i, j int) {
res[i], res[j] = res[j], res[i]
})
choices := make([]*picker.Choice, len(endpoints))
for i, endpoint := range res {
choices[i] = &picker.Choice{
Item: endpoint,
Weight: int(endpoint.Config.Weight),
}
}
picker := picker.New(choices)
return res, picker, nil
}
func newEndpoint(
cfg *config.Config,
logger *zap.Logger,
scope tally.Scope,
endpoint *config.Endpoint,
endpointConfig *config.EndpointConfig,
) (*Endpoint, error) {
var opts []ClientOption
//TODO: check if this is still needed
//if cfg.Env() == config.EnvLocal {
// // Skip TLS verify due to go1.18 issue in MacOS https://github.com/golang/go/issues/51991
// opts = append(opts, withRootCAs(fix.Roots()))
//}
stickySession := endpointConfig.StickySession
if stickySession.Enabled() {
if stickySession.CookiePassive {
opts = append(opts, withStickySessionCookiePassive())
logger.Info(
"creating http client with sticky session",
zap.String("endpoint", endpoint.Name),
zap.String("type", "CookiePassive"),
)
} else if stickySession.HeaderHash != "" {
stickySessionHeaderKey := stickySession.HeaderHash
stickySessionHeaderValue := getStickySessionValue(cfg)
opts = append(opts, withStickySessionHeaderHash(stickySessionHeaderKey, stickySessionHeaderValue))
logger.Info(
"creating http client with sticky session",
zap.String("endpoint", endpoint.Name),
zap.String("type", "HeaderHash"),
zap.String("stickySessionHeaderKey", stickySessionHeaderKey),
zap.String("stickySessionHeaderValue", stickySessionHeaderValue),
)
} else if stickySession.CookieHash != "" {
stickySessionCookie := stickySession.CookieHash
stickySessionValue := getStickySessionValue(cfg)
logger.Info(
"creating http client with sticky session",
zap.String("endpoint", endpoint.Name),
zap.String("type", "CookieHash"),
zap.String("stickySessionCookie", stickySessionCookie),
zap.String("stickySessionValue", stickySessionValue),
)
opts = append(opts, withStickySessionCookieHash(endpoint.Url, stickySessionCookie, stickySessionValue))
} else {
return nil, xerrors.Errorf("unknown sticky session type: %+v", stickySession)
}
}
if len(endpointConfig.Headers) > 0 {
opts = append(opts, withHeaders(endpointConfig.Headers))
}
if cfg.Chain.Client.HttpTimeout > 0 {
opts = append(opts, withOverrideRequestTimeout(cfg.Chain.Client.HttpTimeout))
}
if endpoint.RPS > 0 {
opts = append(opts, withRPS(endpoint.RPS))
}
client, err := newHTTPClient(opts...)
if err != nil {
return nil, xerrors.Errorf("failed to create http client for %v: %w", endpoint.Name, err)
}
providerID := "unknown"
if endpoint.ProviderID != "" {
providerID = endpoint.ProviderID
}
return &Endpoint{
Name: endpoint.Name,
Config: endpoint,
Client: client,
requestsCounter: scope.
Tagged(map[string]string{
"endpoint_name": endpoint.Name,
"provider_id": providerID,
}).
Counter("requests"),
}, nil
}
func getStickySessionValue(cfg *config.Config) string {
headerValue := fmt.Sprintf("%v-%v-%v", consts.ServiceName, cfg.Chain.Network.GetName(), cfg.Env())
return utils.GenerateSha256HashString(headerValue)
}
func (e *endpointProvider) GetEndpoint(ctx context.Context) (*Endpoint, error) {
activeEndpoints, activePicker := e.getActiveEndpoints(ctx)
if len(activeEndpoints) == 0 {
return nil, xerrors.New("no endpoint is available")
}
pick := activePicker.Next().(*Endpoint)
return pick, nil
}
func (e *endpointProvider) GetAllEndpoints() []*Endpoint {
return append(e.primaryEndpoints, e.secondaryEndpoints...)
}
func (e *endpointProvider) GetActiveEndpoints(ctx context.Context) []*Endpoint {
endpoints, _ := e.getActiveEndpoints(ctx)
return endpoints
}
func (e *endpointProvider) WithFailoverContext(ctx context.Context) (context.Context, error) {
if len(e.secondaryEndpoints) == 0 {
return nil, ErrFailoverUnavailable
}
return context.WithValue(ctx, e.getFailoverContextKey(), struct{}{}), nil
}
func (e *endpointProvider) getActiveEndpoints(ctx context.Context) ([]*Endpoint, picker.Picker) {
activeEndpoints := e.primaryEndpoints
activePicker := e.primaryPicker
if e.HasFailoverContext(ctx) {
activeEndpoints = e.secondaryEndpoints
activePicker = e.secondaryPicker
}
return activeEndpoints, activePicker
}
func (e *endpointProvider) HasFailoverContext(ctx context.Context) bool {
return ctx.Value(e.getFailoverContextKey()) != nil
}
func (e *endpointProvider) getFailoverContextKey() contextKey {
// Use different key for master and slave.
return contextKey(contextKeyFailover + e.name)
}
func (e *Endpoint) IncRequestsCounter(n int64) {
e.requestsCounter.Inc(n)
}