Open
Conversation
Key changes: - XREADGROUP BLOCK replaces non-blocking reads - Interleaved PEL processing via XAUTOCLAIM with configurable ratio_slice - PEL stall detection: compares size across full traversals, advances ratio_slice and pel_wait_slice indices when stalled, resets on progress - Adaptive backoff (backoff_slice) when both phases return empty - Context-aware sleeps for clean shutdown - Config validation in InitConsumer (fail-fast before any network call) - New: Stats(), DefaultConsumerName(), ConsumerStats type - Remove: PendingMessages(), exported NewMessages()/AutoClaimMessages() - Add ErrInvalidConfig to errors package - 44 unit tests, all passing
There was a problem hiding this comment.
Pull request overview
This PR introduces an adaptive Redis Streams consumer loop that interleaves blocking XREADGROUP reads with periodic PEL processing via XAUTOCLAIM, adds stall detection/backoff behavior, and bumps the module major version to v4 with updated dependencies, documentation, and CI.
Changes:
- Reworked consumer internals: blocking reads (
BLOCK), interleaved PEL processing, stall detection, adaptive backoff, and context-aware sleeps. - Added config validation (fail-fast), new error (
ErrInvalidConfig), plus observability helpers (Stats(),ConsumerStats) andDefaultConsumerName(). - Migrated module/import paths to
github.com/enerBit/redsumer/v4, updated dependencies, tests, README, and CI.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/producer/producer_test.go | Updates imports to v4 and removes now-unused constants. |
| pkg/producer/producer.go | Updates client import path to v4. |
| pkg/errors/error.go | Adds ErrInvalidConfig and aligns error var formatting. |
| pkg/consumer/consumer_test.go | Adds extensive unit tests covering new consumer behavior and helpers. |
| pkg/consumer/consumer.go | Implements the new adaptive consume loop, config validation, stats, and default consumer naming. |
| pkg/client/client_test.go | Updates test to avoid network usage by injecting a mock client. |
| pkg/client/client.go | Allows injecting a pre-built/mocked Valkey client by short-circuiting InitClient when Instance is set. |
| go.mod | Bumps module to /v4, updates Go version and dependency versions. |
| go.sum | Updates dependency checksums in line with go.mod changes. |
| README.md | Rewrites docs to describe the new adaptive consumer behavior and v4 usage. |
| .github/workflows/CI.yaml | Updates Go version and test commands. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return nil, err | ||
| if c.pelTraversalComplete { | ||
| currentSize, sizeErr := c.pelSize(ctx) | ||
| if sizeErr != nil { |
Comment on lines
+351
to
+357
| if len(msgs) > 0 { | ||
| // New messages take priority; PEL cursor advanced as a side effect. | ||
| return msgs, nil | ||
| } | ||
| if len(pelMsgs) > 0 { | ||
| c.backoffIdx = 0 | ||
| return pelMsgs, nil |
| "fmt" | ||
| "time" | ||
| "log" | ||
| "os" |
Comment on lines
+125
to
+143
| ctx, cancel := context.WithCancel(context.Background()) | ||
|
|
||
| // cancel on SIGTERM / SIGINT | ||
| go func() { | ||
| c := make(chan os.Signal, 1) | ||
| signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
| <-c | ||
| cancel() | ||
| }() | ||
|
|
||
| for { | ||
| msgs, err := consumer.Consume(ctx) | ||
| if err != nil { | ||
| if errors.Is(err, context.Canceled) { | ||
| break | ||
| } | ||
| log.Println(err) | ||
| } | ||
| // ... |
Comment on lines
+23
to
+32
| // Consumer holds configuration and internal adaptive state for consuming a Redis Stream. | ||
| // All slice fields must have at least one element; BatchSize and ClaimBatch must be > 0. | ||
| type Consumer struct { | ||
| Client *client.ClientArgs | ||
|
|
||
| Tries []int | ||
|
|
||
| // --- User configuration --- | ||
| Client *client.ClientArgs | ||
| StreamName string | ||
| GroupName string | ||
| ConsumerName string | ||
| Tries []int // seconds between retries while waiting for stream existence | ||
|
|
Comment on lines
+82
to
+99
| // validateConfig checks all required configuration fields before connecting. | ||
| func validateConfig(c *Consumer) error { | ||
| if len(c.RatioSlice) == 0 { | ||
| return fmt.Errorf("%w: ratio_slice must not be empty", errors_custom.ErrInvalidConfig) | ||
| } | ||
| if len(c.PelWaitSlice) == 0 { | ||
| return fmt.Errorf("%w: pel_wait_slice must not be empty", errors_custom.ErrInvalidConfig) | ||
| } | ||
| if len(c.BackoffSlice) == 0 { | ||
| return fmt.Errorf("%w: backoff_slice must not be empty", errors_custom.ErrInvalidConfig) | ||
| } | ||
| if c.BatchSize <= 0 { | ||
| return fmt.Errorf("%w: batch_size must be > 0", errors_custom.ErrInvalidConfig) | ||
| } | ||
| if c.ClaimBatch <= 0 { | ||
| return fmt.Errorf("%w: claim_batch must be > 0", errors_custom.ErrInvalidConfig) | ||
| } | ||
| return nil |
Comment on lines
+141
to
+144
| if err := c.exist(ctx, c.StreamName); err == nil { | ||
| return nil | ||
| } | ||
| time.Sleep(time.Second * time.Duration(waitTime)) |
Comment on lines
+263
to
274
| // contextSleep sleeps for d, returning ctx.Err() immediately if the context is cancelled. | ||
| func contextSleep(ctx context.Context, d time.Duration) error { | ||
| if d <= 0 { | ||
| return nil | ||
| } | ||
| select { | ||
| case <-time.After(d): | ||
| return nil | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Key changes:
ratio_slice and pel_wait_slice indices when stalled, resets on progress