Skip to content

Commit 1023ba7

Browse files
committed
exponential backoff logic
1 parent aaad671 commit 1023ba7

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

backend/pkg/transport/network/tcp/config.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type ClientConfig struct {
1414
Context context.Context
1515

1616
TryReconnect bool
17-
ConnectionBackoffFunction backoffFunction
17+
ConnectionBackoffFunction BackoffFunction
1818
MaxConnectionRetries int
1919
}
2020

@@ -34,7 +34,7 @@ func NewClientConfig(laddr net.Addr) ClientConfig {
3434
}
3535
}
3636

37-
type backoffFunction = func(int) time.Duration
37+
type BackoffFunction = func(int) time.Duration
3838

3939
const (
4040
defaultBackoffMin time.Duration = 100 * time.Millisecond
@@ -45,10 +45,9 @@ const (
4545
// NewExponentialBackoff returns an exponential backoff function with the given paramenters.
4646
//
4747
// It follows this formula: delay = (min * (exp ^ n); delay < max ? delay : max
48-
func NewExponentialBackoff(min time.Duration, exp float64, max time.Duration) backoffFunction {
48+
func NewExponentialBackoff(min time.Duration, exp float64, max time.Duration) BackoffFunction {
4949
return func(n int) time.Duration {
50-
curr := min
51-
curr = time.Duration(math.Trunc(math.Pow(float64(curr), exp)))
50+
curr := time.Duration(float64(min) * math.Trunc(math.Pow(exp, float64(n))))
5251
if curr >= max {
5352
return max
5453
}

backend/pkg/transport/transport_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestTransport(t *testing.T) {
1818
transport := transport_module.NewTransport(logger)
1919

2020
// Create a context that cancels after a timeout
21-
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Millisecond)
21+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond)
2222
defer cancel()
2323

2424
var wg sync.WaitGroup

0 commit comments

Comments
 (0)