Skip to content

Pel protection backoff#24

Open
santi-giraldo2 wants to merge 6 commits intoenerBit:mainfrom
santi-giraldo2:pel-protection-backoff
Open

Pel protection backoff#24
santi-giraldo2 wants to merge 6 commits intoenerBit:mainfrom
santi-giraldo2:pel-protection-backoff

Conversation

@santi-giraldo2
Copy link
Contributor

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

santi-giraldo2 and others added 6 commits September 9, 2024 12:25
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
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) and DefaultConsumerName().
  • 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants