You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/01/26 16:33:28 UTC

[GitHub] [pulsar-client-go] wuYin commented on a change in pull request #454: Make backoff policy configurable and use uniformly

wuYin commented on a change in pull request #454:
URL: https://github.com/apache/pulsar-client-go/pull/454#discussion_r564615545



##########
File path: pulsar/internal/backoff.go
##########
@@ -18,28 +18,64 @@
 package internal
 
 import (
+	"math/rand"
 	"time"
 )
 
-// Backoff
+func init() {
+	rand.Seed(time.Now().UnixNano())
+}
+
 type Backoff struct {
-	backoff time.Duration
+	initGap, nextGap, maxGap time.Duration
+
+	firstRetry       time.Time
+	mandatoryStop    time.Duration
+	mandatoryStopped bool
 }
 
-const (
-	minBackoff = 100 * time.Millisecond
-	maxBackoff = 60 * time.Second
-)
+func NewBackoff(initGap, maxGap, mandatoryStop time.Duration) *Backoff {
+	return &Backoff{
+		initGap:          initGap,
+		nextGap:          initGap,
+		maxGap:           maxGap,
+		firstRetry:       time.Time{},
+		mandatoryStop:    mandatoryStop,
+		mandatoryStopped: false,
+	}
+}
+
+// for test mock convenience
+type nowTimeProvider func() time.Time
+
+var systemNow nowTimeProvider = func() time.Time {
+	return time.Now()
+}
 
-// Next
 func (b *Backoff) Next() time.Duration {
-	// Double the delay each time
-	b.backoff += b.backoff
-	if b.backoff.Nanoseconds() < minBackoff.Nanoseconds() {
-		b.backoff = minBackoff
-	} else if b.backoff.Nanoseconds() > maxBackoff.Nanoseconds() {
-		b.backoff = maxBackoff
+	curGap := b.nextGap
+	if curGap < b.maxGap {
+		b.nextGap = MinDuration(2*b.nextGap, b.maxGap)
 	}
 
-	return b.backoff
+	if b.mandatoryStop > 0 && !b.mandatoryStopped {
+		now := systemNow()
+		tried := time.Duration(0)
+		if curGap == b.initGap {
+			b.firstRetry = now
+		} else {
+			tried = now.Sub(b.firstRetry)
+		}
+		if tried+curGap > b.mandatoryStop {
+			// reached mandatory stop, reset current retry gap

Review comment:
       If mandatoryStop set to 0, this always be true in first time, means there is no mandatory stop which is on expected.
   So I rule it out in above `if b.mandatoryStop > 0`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org