-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
570 lines (474 loc) · 15.2 KB
/
client.go
File metadata and controls
570 lines (474 loc) · 15.2 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package streamfleet
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"strings"
"time"
"github.com/puzpuzpuz/xsync/v4"
"github.com/redis/go-redis/v9"
)
// ErrClientQueueFull is returned when the client's queue is full.
// This can be returned when enqueuing tasks, or be the logged error when a task is trying to be re-queued after an error.
var ErrClientQueueFull = fmt.Errorf(`streamfleet: client queue is full, tasks are being enqueued too quickly to send to Redis, or Redis is unreachable`)
const clientGroupName = "streamfleet-client"
const expiryLoopInterval = 1 * time.Second
// ClientOpt are options for Streamfleet clients.
type ClientOpt struct {
// The Redis client options to use.
// See the RedisClientOpt, RedisClusterClientOpt or RedisClusterClientOpt struct for more details.
//
// Required.
RedisOpt ToRedisClient
// The maximum number of tasks to queue locally in-memory while waiting for Redis to come back online.
// After the local queue has filled up, new tasks being queued will result in an error.
// Defaults to 25.
MaxLocalQueueSize int
// The logger to use.
// If omitted, uses slog.Default.
logger *slog.Logger
// The interval at which to run the receiver stream garbage collector.
// If omitted, defaults to DefaultReceiverStreamGcInterval.
// If you do not know what this is, you do not need to change it.
ReceiverStreamGcInterval time.Duration
}
// Client is a work queue client.
// It submits tasks to the queue and can receive notifications of their completion.
type Client struct {
// The client's unique ID.
id string
// Whether the server is running.
isRunning bool
// Mapping of queue keys to their underlying stream keys.
queueToStream map[string]string
// Client options.
opt ClientOpt
// The underlying Redis client.
client redis.UniversalClient
// Locally queued tasks to be submitted to the queue.
queuedTasks chan queuedTask
// A mapping of task IDs to their corresponding handles.
pendingTasks *xsync.Map[string, *TaskHandle]
// The logger to use.
logger *slog.Logger
// The interval at which to run the receiver stream GC.
recvGcInterval time.Duration
}
// NewClient creates a new client instance.
// The list of queue keys to support for this client must be exhaustive and cannot change without creating a new client.
// Returns ErrNoQueues if no queue keys were specified.
func NewClient(ctx context.Context, opt ClientOpt, queueKeys ...string) (*Client, error) {
if len(queueKeys) == 0 {
return nil, ErrNoQueues
}
id := MustUuidV7()
client := opt.RedisOpt.ToClient()
queueToStream := make(map[string]string)
for _, key := range queueKeys {
queueToStream[key] = KeyPrefix + key
}
if opt.MaxLocalQueueSize < 1 {
opt.MaxLocalQueueSize = 25
}
var logger *slog.Logger
if opt.logger == nil {
logger = slog.Default()
} else {
logger = opt.logger
}
if opt.ReceiverStreamGcInterval == 0 {
opt.ReceiverStreamGcInterval = DefaultReceiverStreamGcInterval
}
c := &Client{
id: id,
isRunning: true,
opt: opt,
queueToStream: queueToStream,
client: client,
queuedTasks: make(chan queuedTask, opt.MaxLocalQueueSize),
pendingTasks: xsync.NewMap[string, *TaskHandle](),
logger: logger,
recvGcInterval: receiverStreamHeartbeatInterval,
}
// Create stream and consumer group.
err := client.XGroupCreateMkStream(ctx, mkRecvStreamKey(id), clientGroupName, "0").Err()
if err != nil {
return nil, fmt.Errorf(`streamfleet: failed to create receiver for client with ID %s while instantiating with NewClient: %w`, id, err)
}
// Create heartbeat list entries.
if err = c.doHeartbeat(ctx); err != nil {
return nil, err
}
go c.heartbeatLoop()
go c.expiryLoop()
go c.enqueueLoop()
go c.notifLoop()
go c.gcLoop()
return c, nil
}
func (c *Client) gcLoop() {
ctx := context.Background()
queues := make([]string, 0, len(c.queueToStream))
for queue := range c.queueToStream {
queues = append(queues, queue)
}
for c.isRunning {
time.Sleep(c.recvGcInterval)
if !c.isRunning {
break
}
err := runReceiverStreamGc(ctx, c.client, queues)
if err != nil {
c.logger.Log(ctx, slog.LevelError, "failed to run receiver stream garbage collector",
"service", "streamfleet.Client",
"client_id", c.id,
"error", err,
)
}
}
}
func (c *Client) doHeartbeat(ctx context.Context) error {
for queueKey := range c.queueToStream {
hbKey := mkRecvHeartbeatKey(queueKey)
err := c.client.ZAdd(ctx, hbKey, redis.Z{
Score: float64(time.Now().UnixMilli()),
Member: c.id,
}).Err()
if err != nil {
return fmt.Errorf(`streamfleet: failed to create heartbeat key for client with ID %s for work queue %s: %w`, c.id, queueKey, err)
}
}
return nil
}
func (c *Client) expiryLoop() {
for c.isRunning {
time.Sleep(expiryLoopInterval)
if !c.isRunning {
break
}
c.pendingTasks.Range(func(id string, handle *TaskHandle) bool {
if !handle.expTs.IsZero() && time.Now().After(handle.expTs) {
c.pendingTasks.Delete(id)
// Try to send an expiry error.
select {
case handle.resultChan <- ErrTaskExpired:
default:
}
}
return true
})
}
}
func (c *Client) heartbeatLoop() {
ctx := context.Background()
for c.isRunning {
time.Sleep(receiverStreamHeartbeatInterval)
if !c.isRunning {
break
}
if err := c.doHeartbeat(ctx); err != nil {
c.logger.Log(ctx, slog.LevelError, "failed to do client heartbeat",
"service", "streamfleet.Client",
"error", err,
)
continue
}
}
}
// tryQueue tries putting a task on the local queue.
// If the queue is full, returns false. Otherwise, the task was queued and returns true.
func (c *Client) tryQueue(q queuedTask) bool {
// Guard to prevent send on closed channel.
if !c.isRunning {
return false
}
select {
case c.queuedTasks <- q:
return true
default:
return false
}
}
// ErrUnsupportedQueueKey is returned when trying to enqueue a task for a queue key that the client was not instantiated with.
var ErrUnsupportedQueueKey = fmt.Errorf(`streamfleet: unsupported queue key, client was not instantiated with support for this queue key`)
// EnqueueAndForget adds a task to the queue and immediately returns.
// Errors in queuing or the status of the task once it is picked up by a server are not tracked.
// If you need to know when the task has been completed, use EnqueueAndTrack.
//
// If the number of locally queued tasks would exceed ClientOpt.MaxLocalQueueSize, returns ErrClientQueueFull.
// This will normally only happen if the client cannot connect to Redis and tasks pile up.
func (c *Client) EnqueueAndForget(queueKey string, data string, opt TaskOpt) error {
if !c.isRunning {
panic(fmt.Sprintf("streamfleet: tried to queue task on closed server (queueKey: %s)", queueKey))
}
if _, hasQueue := c.queueToStream[queueKey]; !hasQueue {
return ErrUnsupportedQueueKey
}
task := newTask(data, c.id, false, opt)
if !c.tryQueue(queuedTask{
Stream: KeyPrefix + queueKey,
Queue: queueKey,
Task: task,
}) {
return ErrClientQueueFull
}
return nil
}
// EnqueueAndTrack adds a task to the queue and returns a handle to it.
// Errors in queuing or the status of the task once it is picked up by the server can be tracked using the returned handle.
// If you do not need to know when the task has been completed, use EnqueueAndForget.
// You should only use this method if you need to know the status of the task, as it is less efficient than EnqueueAndForget.
//
// If the number of locally queued tasks would exceed ClientOpt.MaxLocalQueueSize, returns ErrClientQueueFull.
// This will normally only happen if the client cannot connect to Redis and tasks pile up.
func (c *Client) EnqueueAndTrack(queueKey string, data string, opt TaskOpt) (*TaskHandle, error) {
if !c.isRunning {
panic(fmt.Sprintf("streamfleet: tried to queue task on closed server (queueKey: %s)", queueKey))
}
stream, hasQueue := c.queueToStream[queueKey]
if !hasQueue {
return nil, ErrUnsupportedQueueKey
}
task := newTask(data, c.id, true, opt)
taskHandle := &TaskHandle{
Id: task.Id,
expTs: task.ExpiresTs,
resultChan: make(chan error, 1),
}
c.pendingTasks.Store(task.Id, taskHandle)
if !c.tryQueue(queuedTask{
Stream: stream,
Queue: queueKey,
Task: task,
}) {
return nil, ErrClientQueueFull
}
return taskHandle, nil
}
func (c *Client) enqueueLoop() {
ctx := context.Background()
for queued := range c.queuedTasks {
pending, hasP := c.pendingTasks.Load(queued.Task.Id)
if !c.isRunning {
if hasP {
pending.resultChan <- ErrTaskCanceled
}
c.logger.Log(ctx, slog.LevelWarn, "client closed, canceling locally queued task",
"service", "streamfleet.Client",
"task_id", queued.Task.Id,
"task_notification_type", TaskNotificationTypeCanceled,
)
continue
}
retry := func() {
if !c.tryQueue(queued) {
// Failed to re-queue because the local queue was full.
if hasP {
pending.resultChan <- ErrClientQueueFull
}
c.logger.Log(ctx, slog.LevelError, "failed to put task on local queue in Redis, but could not put it back on the local queue because it was full",
"service", "streamfleet.Client",
"task_id", queued.Task.Id,
"error", ErrClientQueueFull,
)
}
}
if !queued.Task.ExpiresTs.IsZero() && time.Now().After(queued.Task.ExpiresTs) {
if hasP {
pending.resultChan <- ErrTaskExpired
}
c.logger.Log(ctx, slog.LevelWarn, "task expired before being sent to Redis",
"service", "streamfleet.Client",
"task_id", queued.Task.Id,
"error", ErrTaskExpired,
)
continue
}
skipIter := false
for c.isRunning {
err := c.client.XAdd(context.Background(), &redis.XAddArgs{
Stream: queued.Stream,
Values: queued.Task.encode(),
}).Err()
if err != nil {
// Retry if it's due to a network error.
var opErr *net.OpError
if errors.As(err, &opErr) {
c.logger.Log(ctx, slog.LevelWarn, "failed to send locally queued task due to network error, will retry",
"service", "streamfleet.Client",
"client_id", c.id,
"task_id", queued.Task.Id,
"stream", queued.Stream,
"error", err,
)
time.Sleep(1 * time.Second)
continue
}
c.logger.Log(ctx, slog.LevelError, "failed to send locally queued task to Redis",
"service", "streamfleet.Client",
"task_id", queued.Task.Id,
"error", err,
)
retry()
skipIter = true
break
}
break
}
if skipIter {
continue
}
}
}
// Listens for pending task notifications and notifies pending task handles.
func (c *Client) notifLoop() {
ctx := context.Background()
stream := mkRecvStreamKey(c.id)
for c.isRunning {
skipIter := false
// Wrap XReadGroup in loop so that it can be repeated if the consumer group needs to be recreated.
var streamResults []redis.XStream
var err error
needsRetry := true
for needsRetry {
needsRetry = false
streamResults, err = c.client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: clientGroupName,
Streams: []string{stream, ">"},
Consumer: c.id,
Block: 0,
Count: 10,
}).Result()
if err != nil {
if !c.isRunning || errors.Is(err, redis.ErrClosed) {
break
}
// If this is due to the stream or group not existing, recreate it and try again.
if strings.Contains(err.Error(), "NOGROUP") {
err = c.client.XGroupCreateMkStream(ctx, stream, clientGroupName, "0").Err()
if err != nil {
c.logger.Log(ctx, slog.LevelError, "failed to recreate receiver for client",
"service", "streamfleet.Client",
"client_id", c.id,
"stream", stream,
"consumer_group", clientGroupName,
"error", err,
)
// Wait a second before trying again.
time.Sleep(1 * time.Second)
skipIter = true
break
}
needsRetry = true
continue
}
c.logger.Log(ctx, slog.LevelError, "failed to receive task notifications from Redis",
"service", "streamfleet.Client",
"error", err,
)
// Wait a second before trying again.
time.Sleep(1 * time.Second)
skipIter = true
break
}
}
if skipIter {
continue
}
// No results.
// There should be exactly one stream.
if len(streamResults) == 0 || len(streamResults[0].Messages) == 0 {
continue
}
msgs := streamResults[0].Messages
for _, msg := range msgs {
var notif *TaskNotification
notif, err = decodeTaskNotification(msg.Values)
if err != nil {
c.logger.Log(ctx, slog.LevelError, "failed to decode incoming task notification",
"service", "streamfleet.Client",
"client_id", c.id,
"error", err,
)
continue
}
pending, hasP := c.pendingTasks.LoadAndDelete(notif.TaskId)
if !hasP {
// No corresponding local task.
continue
}
switch notif.Type {
case TaskNotificationTypeCompleted:
pending.resultChan <- nil
case TaskNotificationTypeCanceled:
pending.resultChan <- ErrTaskCanceled
case TaskNotificationTypeExpired:
pending.resultChan <- ErrTaskExpired
case TaskNotificationTypeError:
pending.resultChan <- fmt.Errorf(`streamfleet: task failed with error: %s`, notif.ErrMsg)
default:
c.logger.Log(ctx, slog.LevelWarn, "received unknown task notification type",
"service", "streamfleet.Client",
"client_id", c.id,
"task_notification_type", notif.Type,
)
}
}
// Trim stream before the last received message .
lastMsg := msgs[len(msgs)-1]
err = c.client.XTrimMinID(ctx, stream, lastMsg.ID).Err()
if err != nil {
c.logger.Log(ctx, slog.LevelError, "failed to trim task notifications stream",
"service", "streamfleet.Client",
"client_id", c.id,
"stream", stream,
"last_msg_id", lastMsg.ID,
)
continue
}
}
}
// Close closes resources associated with the client.
// Will cancel pending tasks. Locally queued tasks will be lost.
// The client must not be used after calling Close.
// Subsequent Close calls are no-op.
func (c *Client) Close() error {
if !c.isRunning {
return nil
}
c.isRunning = false
close(c.queuedTasks)
// Cancel tracked pending tasks.
c.pendingTasks.Range(func(id string, handle *TaskHandle) bool {
if len(handle.resultChan) < cap(handle.resultChan) {
handle.resultChan <- ErrTaskCanceled
}
return true
})
// Delete receiver stream and consumer group.
ctx := context.Background()
recvKey := mkRecvStreamKey(c.id)
err := c.client.XGroupDestroy(ctx, recvKey, clientGroupName).Err()
if err != nil {
return fmt.Errorf(`streamfleet: failed to delete receiver consumer group %s for stream %s while closing client with ID %s: %w`, clientGroupName, recvKey, c.id, err)
}
err = c.client.Del(ctx, recvKey).Err()
if err != nil {
return fmt.Errorf(`streamfleet: failed to delete receiver stream %s while closing client with ID %s: %w`, recvKey, c.id, err)
}
// Delete heartbeat entries.
for queueKey := range c.queueToStream {
hbKey := mkRecvHeartbeatKey(queueKey)
err = c.client.ZRem(ctx, hbKey, c.id).Err()
if err != nil {
return fmt.Errorf(`streamfleet: failed to remove heartbeat entry for queue key %s while closing client with ID %s: %w`, queueKey, c.id, err)
}
}
// Finally, after everything has finished, close the Redis client.
if err = c.client.Close(); err != nil {
return fmt.Errorf("streamfleet: failed to close Redis client while closing client: %w", err)
}
return nil
}