This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
peer_client.go
435 lines (368 loc) · 12 KB
/
peer_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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"context"
"crypto/tls"
"fmt"
"sync"
"sync/atomic"
"github.com/mailgun/holster/v4/clock"
"github.com/mailgun/holster/v4/collections"
"github.com/mailgun/holster/v4/errors"
"github.com/mailgun/holster/v4/tracing"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
type PeerPicker interface {
GetByPeerInfo(PeerInfo) *PeerClient
Peers() []*PeerClient
Get(string) (*PeerClient, error)
New() PeerPicker
Add(*PeerClient)
}
type PeerClient struct {
client PeersV1Client
conn *grpc.ClientConn
conf PeerConfig
queue chan *request
queueClosed atomic.Bool
lastErrs *collections.LRUCache
wgMutex sync.RWMutex
wg sync.WaitGroup // Monitor the number of in-flight requests. GUARDED_BY(wgMutex)
}
type response struct {
rl *RateLimitResp
err error
}
type request struct {
request *RateLimitReq
reqState RateLimitReqState
resp chan *response
ctx context.Context
}
type PeerConfig struct {
TLS *tls.Config
Behavior BehaviorConfig
Info PeerInfo
Log FieldLogger
TraceGRPC bool
}
// NewPeerClient tries to establish a connection to a peer in a non-blocking fashion.
// If batching is enabled, it also starts a goroutine where batches will be processed.
func NewPeerClient(conf PeerConfig) (*PeerClient, error) {
peerClient := &PeerClient{
queue: make(chan *request, 1000),
conf: conf,
lastErrs: collections.NewLRUCache(100),
}
var opts []grpc.DialOption
if conf.TraceGRPC {
opts = []grpc.DialOption{
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
}
}
if conf.TLS != nil {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(conf.TLS)))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
var err error
peerClient.conn, err = grpc.Dial(conf.Info.GRPCAddress, opts...)
if err != nil {
return nil, err
}
peerClient.client = NewPeersV1Client(peerClient.conn)
if !conf.Behavior.DisableBatching {
go peerClient.runBatch()
}
return peerClient, nil
}
// Info returns PeerInfo struct that describes this PeerClient
func (c *PeerClient) Info() PeerInfo {
return c.conf.Info
}
// GetPeerRateLimit forwards a rate limit request to a peer. If the rate limit has `behavior == BATCHING` configured,
// this method will attempt to batch the rate limits
func (c *PeerClient) GetPeerRateLimit(ctx context.Context, r *RateLimitReq) (resp *RateLimitResp, err error) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.String("ratelimit.key", r.UniqueKey),
attribute.String("ratelimit.name", r.Name),
)
// If config asked for no batching
if c.conf.Behavior.DisableBatching || HasBehavior(r.Behavior, Behavior_NO_BATCHING) {
// If no metadata is provided
if r.Metadata == nil {
r.Metadata = make(map[string]string)
}
// Propagate the trace context along with the rate limit so
// peers can continue to report traces for this rate limit.
prop := propagation.TraceContext{}
prop.Inject(ctx, &MetadataCarrier{Map: r.Metadata})
// Send a single low latency rate limit request
resp, err := c.GetPeerRateLimits(ctx, &GetPeerRateLimitsReq{
Requests: []*RateLimitReq{r},
})
if err != nil {
err = errors.Wrap(err, "Error in GetPeerRateLimits")
return nil, c.setLastErr(err)
}
return resp.RateLimits[0], nil
}
resp, err = c.getPeerRateLimitsBatch(ctx, r)
if err != nil {
err = errors.Wrap(err, "Error in getPeerRateLimitsBatch")
return nil, c.setLastErr(err)
}
return resp, nil
}
// GetPeerRateLimits requests a list of rate limit statuses from a peer
func (c *PeerClient) GetPeerRateLimits(ctx context.Context, r *GetPeerRateLimitsReq) (resp *GetPeerRateLimitsResp, err error) {
// NOTE: This must be done within the Lock since calling Wait() in Shutdown() causes
// a race condition if called within a separate go routine if the internal wg is `0`
// when Wait() is called then Add(1) is called concurrently.
c.wgMutex.Lock()
c.wg.Add(1)
c.wgMutex.Unlock()
defer c.wg.Done()
resp, err = c.client.GetPeerRateLimits(ctx, r)
if err != nil {
err = errors.Wrap(err, "Error in client.GetPeerRateLimits")
// metricCheckErrorCounter is updated within client.GetPeerRateLimits().
return nil, c.setLastErr(err)
}
// Unlikely, but this avoids a panic if something wonky happens
if len(resp.RateLimits) != len(r.Requests) {
err = errors.New("number of rate limits in peer response does not match request")
metricCheckErrorCounter.WithLabelValues("Item mismatch").Add(1)
return nil, c.setLastErr(err)
}
return resp, nil
}
// UpdatePeerGlobals sends global rate limit status updates to a peer
func (c *PeerClient) UpdatePeerGlobals(ctx context.Context, r *UpdatePeerGlobalsReq) (resp *UpdatePeerGlobalsResp, err error) {
// See NOTE above about RLock and wg.Add(1)
c.wgMutex.Lock()
c.wg.Add(1)
c.wgMutex.Unlock()
defer c.wg.Done()
resp, err = c.client.UpdatePeerGlobals(ctx, r)
if err != nil {
_ = c.setLastErr(err)
}
return resp, err
}
func (c *PeerClient) setLastErr(err error) error {
// If we get a nil error return without caching it
if err == nil {
return err
}
// Prepend client address to error
errWithHostname := errors.Wrap(err, fmt.Sprintf("from host %s", c.conf.Info.GRPCAddress))
key := err.Error()
// Add error to the cache with a TTL of 5 minutes
c.lastErrs.AddWithTTL(key, errWithHostname, clock.Minute*5)
return err
}
func (c *PeerClient) GetLastErr() []string {
var errs []string
keys := c.lastErrs.Keys()
// Get errors from each key in the cache
for _, key := range keys {
err, ok := c.lastErrs.Get(key)
if ok {
errs = append(errs, err.(error).Error())
}
}
return errs
}
func (c *PeerClient) getPeerRateLimitsBatch(ctx context.Context, r *RateLimitReq) (resp *RateLimitResp, err error) {
funcTimer := prometheus.NewTimer(metricFuncTimeDuration.WithLabelValues("PeerClient.getPeerRateLimitsBatch"))
defer funcTimer.ObserveDuration()
req := request{
resp: make(chan *response, 1),
ctx: ctx,
request: r,
}
c.wgMutex.Lock()
c.wg.Add(1)
c.wgMutex.Unlock()
defer c.wg.Done()
// Enqueue the request to be sent
peerAddr := c.Info().GRPCAddress
metricBatchQueueLength.WithLabelValues(peerAddr).Set(float64(len(c.queue)))
if c.queueClosed.Load() {
// this check prevents "panic: send on close channel"
return nil, status.Error(codes.Canceled, "grpc: the client connection is closing")
}
select {
case c.queue <- &req:
// Successfully enqueued request.
case <-ctx.Done():
return nil, errors.Wrap(ctx.Err(), "Context error while enqueuing request")
}
// Wait for a response or context cancel
select {
case re := <-req.resp:
if re.err != nil {
err := errors.Wrap(c.setLastErr(re.err), "Request error")
return nil, c.setLastErr(err)
}
return re.rl, nil
case <-ctx.Done():
return nil, errors.Wrap(ctx.Err(), "Context error while waiting for response")
}
}
// runBatch processes batching requests by waiting for requests to be queued. Send
// the queue as a batch when either c.batchWait time has elapsed or the queue
// reaches c.batchLimit.
func (c *PeerClient) runBatch() {
var interval = NewInterval(c.conf.Behavior.BatchWait)
defer interval.Stop()
var queue []*request
for {
ctx := context.Background()
select {
case r, ok := <-c.queue:
if !ok {
// If the queue has shutdown, we need to send the rest of the queue
if len(queue) > 0 {
c.sendBatch(ctx, queue)
}
return
}
queue = append(queue, r)
// Send the queue if we reached our batch limit
if len(queue) >= c.conf.Behavior.BatchLimit {
c.conf.Log.WithContext(ctx).
WithFields(logrus.Fields{
"queueLen": len(queue),
"batchLimit": c.conf.Behavior.BatchLimit,
}).
Debug("runBatch() reached batch limit")
ref := queue
queue = nil
go c.sendBatch(ctx, ref)
continue
}
// If this is our first enqueued item since last
// sendBatch, reset interval timer.
if len(queue) == 1 {
interval.Next()
}
continue
case <-interval.C:
queue2 := queue
if len(queue2) > 0 {
queue = nil
go func() {
c.sendBatch(ctx, queue2)
}()
}
}
}
}
// sendBatch sends the queue provided and returns the responses to
// waiting go routines
func (c *PeerClient) sendBatch(ctx context.Context, queue []*request) {
batchSendTimer := prometheus.NewTimer(metricBatchSendDuration.WithLabelValues(c.conf.Info.GRPCAddress))
defer batchSendTimer.ObserveDuration()
funcTimer := prometheus.NewTimer(metricFuncTimeDuration.WithLabelValues("PeerClient.sendBatch"))
defer funcTimer.ObserveDuration()
var req GetPeerRateLimitsReq
for _, r := range queue {
// NOTE: This trace has the same name because it's in a separate trace than the one above.
// We link the two traces, so we can relate our rate limit trace back to the above trace.
r.ctx = tracing.StartNamedScope(r.ctx, "PeerClient.sendBatch",
trace.WithLinks(trace.LinkFromContext(ctx)))
// If no metadata is provided
if r.request.Metadata == nil {
r.request.Metadata = make(map[string]string)
}
// Propagate the trace context along with the batched rate limit so
// peers can continue to report traces for this rate limit.
prop := propagation.TraceContext{}
prop.Inject(r.ctx, &MetadataCarrier{Map: r.request.Metadata})
req.Requests = append(req.Requests, r.request)
tracing.EndScope(r.ctx, nil)
}
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, c.conf.Behavior.BatchTimeout)
resp, err := c.client.GetPeerRateLimits(timeoutCtx, &req)
timeoutCancel()
// An error here indicates the entire request failed
if err != nil {
logPart := "Error in client.GetPeerRateLimits"
c.conf.Log.WithContext(ctx).
WithError(err).
WithFields(logrus.Fields{
"queueLen": len(queue),
"batchTimeout": c.conf.Behavior.BatchTimeout.String(),
}).
Error(logPart)
err = errors.Wrap(err, logPart)
_ = c.setLastErr(err)
// metricCheckErrorCounter is updated within client.GetPeerRateLimits().
for _, r := range queue {
r.resp <- &response{err: err}
}
return
}
// Unlikely, but this avoids a panic if something wonky happens
if len(resp.RateLimits) != len(queue) {
err = errors.New("server responded with incorrect rate limit list size")
for _, r := range queue {
metricCheckErrorCounter.WithLabelValues("Item mismatch").Add(1)
r.resp <- &response{err: err}
}
return
}
// Provide responses to channels waiting in the queue
for i, r := range queue {
r.resp <- &response{rl: resp.RateLimits[i]}
}
}
// Shutdown waits until all outstanding requests have finished or the context is cancelled.
// Then it closes the grpc connection.
func (c *PeerClient) Shutdown(ctx context.Context) error {
// ensure we don't leak goroutines, even if the Shutdown times out
defer c.conn.Close()
waitChan := make(chan struct{})
go func() {
// drain in-flight requests
c.wgMutex.Lock()
defer c.wgMutex.Unlock()
c.wg.Wait()
// clear errors
c.lastErrs = collections.NewLRUCache(100)
// signal that no more items will be sent
c.queueClosed.Store(true)
close(c.queue)
close(waitChan)
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-waitChan:
return nil
}
}