Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <dominic.evans@uk.ibm.com>
dnwe committed Sep 6, 2023
1 parent 0b17025 commit 2dbe0c8
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
@@ -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):
@@ -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 {
12 changes: 12 additions & 0 deletions consumer_group_test.go
Original file line number Diff line number Diff line change
@@ -232,3 +232,15 @@ func TestConsumerGroupSessionDoesNotRetryForever(t *testing.T) {

wg.Wait()
}

func TestConsumerShouldNotRetrySessionIfContextCancelled(t *testing.T) {
c := &consumerGroup{
config: NewTestConfig(),
}
ctx, cancel := context.WithCancelCause(context.Background())
cancel(errors.New("cancelled in unittest"))
_, 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 2dbe0c8

Please sign in to comment.