Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync -- add exponential backoff #1684

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions x/sync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"math"
"sync/atomic"
"time"

Expand All @@ -23,7 +24,9 @@ import (
)

const (
failedRequestSleepInterval = 10 * time.Millisecond
initialRetryWait = 10 * time.Millisecond
maxRetryWait = time.Second
retryWaitFactor = 1.5 // Larger --> timeout grows more quickly

epsilon = 1e-6 // small amount to add to time to avoid division by 0
)
Expand Down Expand Up @@ -211,6 +214,11 @@ func getAndParse[T any](
lastErr = err
}

retryWait := initialRetryWait * time.Duration(math.Pow(retryWaitFactor, float64(attempt)))
if retryWait > maxRetryWait || retryWait < 0 { // Handle overflows with negative check.
retryWait = maxRetryWait
}

select {
case <-ctx.Done():
if lastErr != nil {
Expand All @@ -221,7 +229,7 @@ func getAndParse[T any](
)
}
return nil, ctx.Err()
case <-time.After(failedRequestSleepInterval):
case <-time.After(retryWait):
}
}
}
Expand Down