-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
675 lines (574 loc) · 18.2 KB
/
server.go
File metadata and controls
675 lines (574 loc) · 18.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
package streamfleet
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"strings"
"time"
"github.com/puzpuzpuz/xsync/v4"
"github.com/redis/go-redis/v9"
)
// TODO Figure out a good way to trim the stream.
// Right now my strategy is to use XDEL on old entries, but that doesn't work for crashed clients, and there might be more caveats.
// The Redis stream group name used by servers to receive new messages.
const serverGroupName = "streamfleet-server"
const claimLoopInterval = 1 * time.Minute
// TaskHandler is a function that handles a task.
// Once it returns, the task will be acknowledged as complete if error is nil, otherwise it will be re-queued according to its retry policy.
// If the task is canceled, ctx will be canceled.
type TaskHandler func(ctx context.Context, task *Task) error
// ErrMissingServerUniqueId is returned when a server is instantiated without a unique ID.
var ErrMissingServerUniqueId = fmt.Errorf("streamfleet: server unique ID is missing")
// ErrMissingRedisOpt is returned when a server is instantiated without the necessary options to construct a Redis client.
var ErrMissingRedisOpt = fmt.Errorf("streamfleet: Redis client options are missing")
// ServerOpt are options for Streamfleet servers.
type ServerOpt struct {
// The unique identifier of the server.
// This ID should be unique among all servers accepting work from any particular queue.
// Usually, this should correspond to the name of the logical machine, e.g. its hostname (if unique).
// Collisions in server unique IDs can result in unexpected behavior, such as work not being distributed evenly among servers.
//
// Required.
ServerUniqueId string
// The Redis client options to use.
// See the RedisClientOpt, RedisClusterClientOpt or RedisClusterClientOpt struct for more details.
//
// Required.
RedisOpt ToRedisClient
// The number of concurrent handlers to run at once.
// If unspecified or <1, defaults to 1.
HandlerConcurrency 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
}
// Server is a work queue server (a worker).
// It accepts tasks from the queue and handles them.
type Server struct {
// Whether the server is running.
isRunning bool
// Server options.
opt ServerOpt
// Mapping of underlying stream keys to their queue keys.
streamToQueue map[string]string
// Mapping of queue keys to their handlers.
queueHandlers map[string]TaskHandler
// The underlying Redis client.
client redis.UniversalClient
// Pending tasks to be picked up by worker goroutines.
// Its capacity must match ServerOpt.HandlerConcurrency.
pendingTasks chan *queuedTask
// Channel written to for each task processing loop that has exited.
finishedChan chan struct{}
// A mapping of IDs of tasks that are currently in progress to their cancellation context.
// This is used to cancel tasks that are in progress.
inProgress *xsync.Map[string, context.CancelFunc]
// The logger to use.
logger *slog.Logger
}
// NewServer creates a new server instance.
// It does not connect to Redis until Server.Run is called.
func NewServer(ctx context.Context, opt ServerOpt) (*Server, error) {
if opt.ServerUniqueId == "" {
return nil, ErrMissingServerUniqueId
}
if opt.RedisOpt == nil {
return nil, ErrMissingRedisOpt
}
client := opt.RedisOpt.ToClient()
if opt.HandlerConcurrency < 1 {
opt.HandlerConcurrency = 1
}
var logger *slog.Logger
if opt.logger == nil {
logger = slog.Default()
} else {
logger = opt.logger
}
if opt.ReceiverStreamGcInterval == 0 {
opt.ReceiverStreamGcInterval = DefaultReceiverStreamGcInterval
}
s := &Server{
isRunning: false,
opt: opt,
streamToQueue: make(map[string]string),
queueHandlers: make(map[string]TaskHandler),
client: client,
pendingTasks: make(chan *queuedTask, opt.HandlerConcurrency),
finishedChan: make(chan struct{}, opt.HandlerConcurrency),
inProgress: xsync.NewMap[string, context.CancelFunc](),
logger: logger,
}
for range opt.HandlerConcurrency {
go s.procLoop()
}
go s.gcLoop()
go s.claimLoop()
return s, nil
}
func (s *Server) gcLoop() {
ctx := context.Background()
queues := make([]string, 0, len(s.streamToQueue))
for _, queue := range s.streamToQueue {
queues = append(queues, queue)
}
for s.isRunning {
time.Sleep(s.opt.ReceiverStreamGcInterval)
if !s.isRunning {
break
}
err := runReceiverStreamGc(ctx, s.client, queues)
if err != nil {
s.logger.Log(ctx, slog.LevelError, "failed to run receiver stream garbage collector",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"error", err,
)
}
}
}
// Handle sets the handler for the queue with the specified key.
// Once the handler returns, the task will be acknowledged as complete if error is nil, otherwise it will be re-queued according to its retry policy.
// If the server is already running, this function panics.
func (s *Server) Handle(queueKey string, handler TaskHandler) {
if s.isRunning {
panic(fmt.Sprintf("streamfleet: server is already running, cannot add handler (queueKey: %s)", queueKey))
}
s.queueHandlers[queueKey] = handler
s.streamToQueue[KeyPrefix+queueKey] = queueKey
}
// sendTaskNotif sends a task notification.
// It logs an error if it fails rather than returning an error.
func (s *Server) sendTaskNotif(clientId string, notif TaskNotification) {
ctx := context.Background()
stream := mkRecvStreamKey(clientId)
for s.isRunning {
err := s.client.XAdd(ctx, &redis.XAddArgs{
Stream: stream,
Values: notif.encode(),
}).Err()
if err != nil {
// Retry if it's due to a network error.
var opErr *net.OpError
if errors.As(err, &opErr) {
s.logger.Log(ctx, slog.LevelWarn, "failed to send task notification due to network error, will retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", notif.TaskId,
"error", err,
)
time.Sleep(1 * time.Second)
continue
}
s.logger.Log(ctx, slog.LevelError, "failed to send task notification",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_client_id", clientId,
"task_id", notif.TaskId,
"task_notification_type", notif.Type,
"error", err,
)
break
}
break
}
}
func (s *Server) retry(queued *queuedTask, cause error) {
go func() {
ctx := context.Background()
task := queued.Task
time.Sleep(task.RetryDelay)
task.retries++
res, err := s.client.XAdd(ctx, &redis.XAddArgs{
Stream: queued.Stream,
Values: queued.Task.encode(),
}).Result()
if err != nil {
s.logger.Log(ctx, slog.LevelError, "failed to re-queue task after handler error",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", task.Id,
"stream", queued.Stream,
"error", err,
)
if task.sendNotifications {
// Send error notification with cause error.
s.sendTaskNotif(task.ClientId, TaskNotification{
TaskId: task.Id,
Type: TaskNotificationTypeError,
ErrMsg: cause.Error(),
})
}
// TODO Introduce local holding queue if re-queue on Redis fails.
// This is because if Redis is down, there may be cascading failures in handlers, and we don't want to burn all retries.
}
queued.RedisId = res
}()
}
func (s *Server) procLoop() {
defer func() {
s.finishedChan <- struct{}{}
}()
for queued := range s.pendingTasks {
task := queued.Task
if !s.isRunning {
s.logger.Log(context.Background(), slog.LevelDebug, "canceling pending task because server is closed",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", task.Id,
)
if task.sendNotifications {
s.sendTaskNotif(task.ClientId, TaskNotification{
TaskId: task.Id,
Type: TaskNotificationTypeCanceled,
})
}
continue
}
if !queued.Task.ExpiresTs.IsZero() && time.Now().After(queued.Task.ExpiresTs) {
if task.sendNotifications {
s.sendTaskNotif(queued.Task.ClientId, TaskNotification{
TaskId: queued.Task.Id,
Type: TaskNotificationTypeExpired,
})
}
continue
}
handler, hasHandler := s.queueHandlers[queued.Queue]
if !hasHandler {
s.logger.Log(context.Background(), slog.LevelError, "received message from queue without a registered, this is a bug in streamfleet",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"queue", queued.Queue,
)
continue
}
ctx, cancel := context.WithCancel(context.Background())
cleanup := func() {
cancel()
s.inProgress.Delete(queued.Task.Id)
}
s.inProgress.Store(queued.Task.Id, cancel)
handlerDone := make(chan struct{})
// Make sure the task is periodically claimed by this worker while it is in progress.
// This prevents other workers from thinking this server is dead and claiming the task for themselves.
go func() {
timer := time.NewTimer(TaskUpdatePendingInterval)
for {
select {
case <-timer.C:
err := s.client.XClaim(ctx, &redis.XClaimArgs{
Stream: queued.Stream,
Group: serverGroupName,
Consumer: s.opt.ServerUniqueId,
Messages: []string{queued.RedisId},
}).Err()
if err != nil && !errors.Is(err, context.Canceled) {
s.logger.Log(ctx, slog.LevelError, "failed to keep task in server custody with XCLAIM",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
)
}
case <-handlerDone:
timer.Stop()
return
}
}
}()
// Actually run the handler.
err := handler(ctx, queued.Task)
handlerDone <- struct{}{}
if err != nil {
if queued.Task.MaxRetries == 0 || queued.Task.retries < queued.Task.MaxRetries {
s.logger.Log(ctx, slog.LevelError, "task handler returned error, will retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
"retries", queued.Task.retries,
"max_retries", queued.Task.MaxRetries,
"will_retry", true,
)
// Try to re-queue.
s.retry(queued, err)
} else {
s.logger.Log(ctx, slog.LevelError, "task handler returned error, will not retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
"retries", queued.Task.retries,
"max_retries", queued.Task.MaxRetries,
"will_retry", false,
)
if task.sendNotifications {
// Since there are no retries left, fail task with error.
s.sendTaskNotif(task.ClientId, TaskNotification{
TaskId: task.Id,
Type: TaskNotificationTypeError,
ErrMsg: err.Error(),
})
}
}
} else if task.sendNotifications {
s.sendTaskNotif(task.ClientId, TaskNotification{
TaskId: task.Id,
Type: TaskNotificationTypeCompleted,
})
}
skipIter := false
for s.isRunning {
// Regardless of whether the handler exited with success or failure, acknowledge the message.
// If it failed, it should have already had a duplicate added by this point.
err = s.client.XAck(context.Background(), queued.Stream, serverGroupName, queued.RedisId).Err()
if err != nil {
// Retry if it's due to a network error.
var opErr *net.OpError
if errors.As(err, &opErr) {
s.logger.Log(ctx, slog.LevelWarn, "failed to acknowledge handled task due to network error, will retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
)
time.Sleep(1 * time.Second)
continue
}
s.logger.Log(ctx, slog.LevelError, "failed to acknowledge handled task",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
)
cleanup()
skipIter = true
break
}
break
}
if skipIter {
continue
}
// TODO Replace delete with trim?
err = s.client.XDel(context.Background(), queued.Stream, queued.RedisId).Err()
if err != nil {
s.logger.Log(ctx, slog.LevelError, "failed to delete handled task",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", queued.Task.Id,
"error", err,
)
cleanup()
continue
}
cleanup()
}
}
func (s *Server) claimLoop() {
ctx := context.Background()
for s.isRunning {
time.Sleep(claimLoopInterval)
if !s.isRunning {
break
}
for stream, queue := range s.streamToQueue {
var msgs []redis.XMessage
var cursor string
var err error
for cursor != "0-0" {
msgs, cursor, err = s.client.XAutoClaim(ctx, &redis.XAutoClaimArgs{
Stream: stream,
Start: cursor,
Group: serverGroupName,
MinIdle: TaskMaxPendingTime,
}).Result()
if err != nil {
// Retry if it's due to a network error.
var opErr *net.OpError
if errors.As(err, &opErr) {
s.logger.Log(ctx, slog.LevelWarn, "failed to run XAUTOCLAIM to claim idle tasks due to network error, will retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"cursor", cursor,
"error", err,
)
time.Sleep(1 * time.Second)
continue
}
s.logger.Log(ctx, slog.LevelError, "failed to run XAUTOCLAIM to claim idle tasks",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"cursor", cursor,
"error", err,
)
// Since the cursor will be "" if the command failed, continuing will just restart the iteration, which is what we want.
continue
}
for _, msg := range msgs {
task, err := decodeTask(msg.Values)
if err != nil {
s.logger.Log(ctx, slog.LevelError, "failed to decode incoming task",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", msg.ID,
"queue", queue,
"error", err,
)
continue
}
s.pendingTasks <- &queuedTask{
Stream: stream,
Queue: queue,
Task: task,
RedisId: msg.ID,
}
}
}
}
}
}
// Main stream receiver loop.
// Only errors if there is a failure reading from Redis or decoding tasks.
// If this function returns, the Redis client should be considered as closed.
func (s *Server) recvLoop() error {
ctx := context.Background()
for s.isRunning {
streams := make([]string, 0, len(s.streamToQueue)*2)
for stream := range s.streamToQueue {
streams = append(streams, stream, ">")
}
skipIter := false
var streamResults []redis.XStream
var err error
for s.isRunning {
streamResults, err = s.client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: serverGroupName,
Streams: streams,
Consumer: s.opt.ServerUniqueId,
Block: 0,
Count: int64(s.opt.HandlerConcurrency),
}).Result()
if err != nil {
if !s.isRunning || errors.Is(err, redis.ErrClosed) {
skipIter = true
break
}
var opErr *net.OpError
if errors.As(err, &opErr) {
s.logger.Log(ctx, slog.LevelWarn, "failed to read tasks stream from Redis due to network error, will retry",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"error", err,
)
time.Sleep(1 * time.Second)
continue
}
return fmt.Errorf("streamfleet: server failed to read from Redis stream %s: %w", streamResults, err)
}
break
}
if skipIter {
continue
}
// For each stream, decode and queue all received tasks.
for _, result := range streamResults {
queue, hasQ := s.streamToQueue[result.Stream]
if !hasQ {
s.logger.Log(ctx, slog.LevelError, "received message from unknown stream, this is a bug in streamfleet",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"stream", result.Stream,
)
continue
}
// Queue each task from the stream.
for _, msg := range result.Messages {
var task *Task
task, err = decodeTask(msg.Values)
if err != nil {
s.logger.Log(ctx, slog.LevelError, "failed to decode incoming task",
"service", "streamfleet.Server",
"server_id", s.opt.ServerUniqueId,
"task_id", msg.ID,
"queue", queue,
"error", err,
)
continue
}
// Guard to prevent send on closed channel.
if s.isRunning {
s.pendingTasks <- &queuedTask{
Stream: result.Stream,
Queue: queue,
Task: task,
RedisId: msg.ID,
}
}
}
}
}
return nil
}
// Run runs the server.
// It will only return if the server's initial connection fails, or retrying the connection times out.
// Note that handlers are not guaranteed to run on the same goroutine as this call.
//
// Since this method will run for the duration of the server's life, it blocks and should usually be run in its own goroutine.
func (s *Server) Run() error {
ctx := context.Background()
if len(s.streamToQueue) == 0 {
return ErrNoQueues
}
for stream := range s.streamToQueue {
err := s.client.XGroupCreateMkStream(ctx, stream, serverGroupName, "0").Err()
if err != nil {
// Ignore BUSYGROUP errors.
if strings.Contains(err.Error(), "BUSYGROUP") {
continue
}
return fmt.Errorf("streamfleet: failed to create stream %s or group %s for the stream: %w", stream, serverGroupName, err)
}
}
s.isRunning = true
for s.isRunning {
err := s.recvLoop()
if err != nil {
return fmt.Errorf("streamfleet: server failed to run: %w", err)
}
}
return nil
}
// Close stops the server.
// Will cancel pending tasks and wait for processing loops to finish.
// The server must not be used after calling Close.
// Subsequent Close calls are no-op.
func (s *Server) Close() error {
if !s.isRunning {
return nil
}
s.isRunning = false
close(s.pendingTasks)
// Cancel all in-progress tasks.
s.inProgress.Range(func(id string, cancel context.CancelFunc) bool {
cancel()
s.inProgress.Delete(id)
return true
})
// Wait for all task processing loops to finish.
for range s.opt.HandlerConcurrency {
<-s.finishedChan
}
// Finally, after everything has finished, close the Redis client.
if err := s.client.Close(); err != nil {
return fmt.Errorf("streamfleet: failed to close Redis client while closing server: %w", err)
}
return nil
}