|
| 1 | +package redisstream |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/redis/go-redis/v9" |
| 12 | +) |
| 13 | + |
| 14 | +type redisClient interface { |
| 15 | + XGroupCreateMkStream(ctx context.Context, stream string, group string, start string) *redis.StatusCmd |
| 16 | + XReadGroup(ctx context.Context, a *redis.XReadGroupArgs) *redis.XStreamSliceCmd |
| 17 | + XAck(ctx context.Context, stream string, group string, ids ...string) *redis.IntCmd |
| 18 | +} |
| 19 | + |
| 20 | +type TaskHandler interface { |
| 21 | + Submit(ctx context.Context, id string, payload map[string]string, ack func() error) error |
| 22 | +} |
| 23 | + |
| 24 | +type StreamOptions struct { |
| 25 | + BatchSize int64 |
| 26 | + BlockTimeout time.Duration |
| 27 | + ErrHandler func(err error) |
| 28 | +} |
| 29 | + |
| 30 | +func DefaultStreamOptions() StreamOptions { |
| 31 | + return StreamOptions{ |
| 32 | + BatchSize: 10, |
| 33 | + BlockTimeout: 2 * time.Second, |
| 34 | + ErrHandler: func(err error) { |
| 35 | + log.Printf("[redispool] stream error: %v", err) |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +type StreamAdapter struct { |
| 41 | + rdb redisClient |
| 42 | + streamName string |
| 43 | + groupName string |
| 44 | + consumerID string |
| 45 | + opts StreamOptions |
| 46 | +} |
| 47 | + |
| 48 | +func NewStreamAdapter(rdb redisClient, stream, group, consumer string, opts StreamOptions) *StreamAdapter { |
| 49 | + if opts.BatchSize <= 0 { |
| 50 | + opts.BatchSize = DefaultStreamOptions().BatchSize |
| 51 | + } |
| 52 | + if opts.BlockTimeout <= 0 { |
| 53 | + opts.BlockTimeout = DefaultStreamOptions().BlockTimeout |
| 54 | + } |
| 55 | + if opts.ErrHandler == nil { |
| 56 | + opts.ErrHandler = DefaultStreamOptions().ErrHandler |
| 57 | + } |
| 58 | + |
| 59 | + return &StreamAdapter{ |
| 60 | + rdb: rdb, |
| 61 | + streamName: stream, |
| 62 | + groupName: group, |
| 63 | + consumerID: consumer, |
| 64 | + opts: opts, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (a *StreamAdapter) Initialize(ctx context.Context, offset string) error { |
| 69 | + if err := a.rdb.XGroupCreateMkStream(ctx, a.streamName, a.groupName, offset).Err(); err != nil { |
| 70 | + if strings.HasPrefix(err.Error(), "BUSYGROUP") { |
| 71 | + return nil |
| 72 | + } |
| 73 | + return fmt.Errorf("could not create group stream: %w", err) |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// Consume replaces FetchTasks injecting tasks directly into the handler. |
| 79 | +func (a *StreamAdapter) Consume(ctx context.Context, handler TaskHandler) { |
| 80 | + for { |
| 81 | + select { |
| 82 | + case <-ctx.Done(): |
| 83 | + return |
| 84 | + default: |
| 85 | + streams, err := a.rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ |
| 86 | + Group: a.groupName, |
| 87 | + Consumer: a.consumerID, |
| 88 | + Streams: []string{a.streamName, ">"}, |
| 89 | + Count: a.opts.BatchSize, |
| 90 | + Block: a.opts.BlockTimeout, |
| 91 | + }).Result() |
| 92 | + if err != nil { |
| 93 | + if errors.Is(err, redis.Nil) { |
| 94 | + continue |
| 95 | + } |
| 96 | + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| 97 | + return |
| 98 | + } |
| 99 | + a.opts.ErrHandler(err) |
| 100 | + |
| 101 | + select { |
| 102 | + case <-ctx.Done(): |
| 103 | + return |
| 104 | + case <-time.After(time.Second): |
| 105 | + } |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + for _, stream := range streams { |
| 110 | + for _, msg := range stream.Messages { |
| 111 | + msgID := msg.ID |
| 112 | + |
| 113 | + payload := make(map[string]string) |
| 114 | + for k, v := range msg.Values { |
| 115 | + if str, ok := v.(string); ok { |
| 116 | + payload[k] = str |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if err := handler.Submit(ctx, msg.ID, payload, func() error { |
| 121 | + ackCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 122 | + defer cancel() |
| 123 | + return a.rdb.XAck(ackCtx, a.streamName, a.groupName, msgID).Err() |
| 124 | + }); err != nil { |
| 125 | + a.opts.ErrHandler(fmt.Errorf("could not submit task to handler: %w", err)) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments