Skip to content

Commit

Permalink
fix(consumer): don't retry session if ctx canceled
Browse files Browse the repository at this point in the history
Fixes #2640

Signed-off-by: Dominic Evans <[email protected]>
  • Loading branch information
dnwe committed Sep 6, 2023
1 parent 0b17025 commit 05cb9fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func (c *consumerGroup) ResumeAll() {

func (c *consumerGroup) retryNewSession(ctx context.Context, topics []string, handler ConsumerGroupHandler, retries int, refreshCoordinator bool) (*consumerGroupSession, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.closed:
return nil, ErrClosedConsumerGroup
case <-time.After(c.config.Consumer.Group.Rebalance.Retry.Backoff):
Expand All @@ -261,6 +263,9 @@ func (c *consumerGroup) retryNewSession(ctx context.Context, topics []string, ha
}

func (c *consumerGroup) newSession(ctx context.Context, topics []string, handler ConsumerGroupHandler, retries int) (*consumerGroupSession, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
coordinator, err := c.client.Coordinator(c.groupID)
if err != nil {
if retries <= 0 {
Expand Down
12 changes: 12 additions & 0 deletions consumer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,15 @@ func TestConsumerGroupSessionDoesNotRetryForever(t *testing.T) {

wg.Wait()
}

func TestConsumerShouldNotRetrySessionIfContextCancelled(t *testing.T) {
c := &consumerGroup{
config: NewTestConfig(),
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := c.newSession(ctx, nil, nil, 1024)
assert.Equal(t, context.Canceled, err)
_, err = c.retryNewSession(ctx, nil, nil, 1024, true)
assert.Equal(t, context.Canceled, err)
}

0 comments on commit 05cb9fa

Please sign in to comment.