Skip to content

Commit c587b5c

Browse files
GilthonielCopilotcrossoverJie
authored
[Issue 1384][consumer] Fix the default nack backoff policy (#1385)
Co-authored-by: Copilot <[email protected]> Co-authored-by: crossoverJie <[email protected]>
1 parent 12b966d commit c587b5c

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

pulsar/negative_backoff_policy.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ func (nbp *defaultNackBackoffPolicy) Next(redeliveryCount uint32) time.Duration
4141
minNackTime := 1 * time.Second // 1sec
4242
maxNackTime := 10 * time.Minute // 10min
4343

44-
if redeliveryCount < 0 {
45-
return minNackTime
44+
backoff := float64(minNackTime << redeliveryCount)
45+
if backoff == 0 {
46+
// Overflow so we assign the maximum value of the backoff.
47+
backoff = float64(maxNackTime)
4648
}
4749

48-
return time.Duration(math.Min(math.Abs(float64(minNackTime<<redeliveryCount)), float64(maxNackTime)))
50+
return time.Duration(math.Min(backoff, float64(maxNackTime)))
4951
}

pulsar/negative_backoff_policy_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package pulsar
1919

2020
import (
21+
"math"
2122
"testing"
2223
"time"
2324

@@ -27,9 +28,10 @@ import (
2728
func TestDefaultNackBackoffPolicy_Next(t *testing.T) {
2829
defaultNackBackoff := new(defaultNackBackoffPolicy)
2930

30-
res0 := defaultNackBackoff.Next(0)
31-
assert.Equal(t, 1*time.Second, res0)
32-
33-
res5 := defaultNackBackoff.Next(5)
34-
assert.Equal(t, 32*time.Second, res5)
31+
assert.Equal(t, 1*time.Second, defaultNackBackoff.Next(0))
32+
assert.Equal(t, 32*time.Second, defaultNackBackoff.Next(5))
33+
assert.Equal(t, 10*time.Minute, defaultNackBackoff.Next(1000))
34+
assert.Equal(t, 10*time.Minute, defaultNackBackoff.Next(math.MaxUint8))
35+
assert.Equal(t, 10*time.Minute, defaultNackBackoff.Next(math.MaxUint16))
36+
assert.Equal(t, 10*time.Minute, defaultNackBackoff.Next(math.MaxUint32))
3537
}

0 commit comments

Comments
 (0)