diff --git a/client/backoff.go b/client/backoff.go index 70070da2..7281f4a8 100644 --- a/client/backoff.go +++ b/client/backoff.go @@ -2,13 +2,13 @@ package client import ( "context" - "math" "time" + + "github.com/micro/go-micro/v2/util/backoff" ) type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error) -// exponential backoff is a function x^e multiplied by a factor of 0.1 second. func exponentialBackoff(ctx context.Context, req Request, attempts int) (time.Duration, error) { - return time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100, nil + return backoff.Do(attempts), nil } diff --git a/network/default.go b/network/default.go index a5613827..6fb347ae 100644 --- a/network/default.go +++ b/network/default.go @@ -225,9 +225,6 @@ func (n *network) acceptNetConn(l tunnel.Listener, recv chan *message) { sleep := backoff.Do(i) log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep) time.Sleep(sleep) - if i > 5 { - i = 0 - } i++ continue } @@ -255,10 +252,6 @@ func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *message) { sleep := backoff.Do(i) log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep) time.Sleep(sleep) - if i > 5 { - // reset the counter - i = 0 - } i++ continue } @@ -1572,11 +1565,6 @@ func (n *network) connect() { case <-time.After(time.Second + backoff.Do(attempts)): // we have to try again attempts++ - - // reset attempts 5 == ~2mins - if attempts > 5 { - attempts = 0 - } } } } diff --git a/util/backoff/backoff.go b/util/backoff/backoff.go index 013d5291..cadc2fc1 100644 --- a/util/backoff/backoff.go +++ b/util/backoff/backoff.go @@ -6,9 +6,11 @@ import ( "time" ) +// Do is a function x^e multiplied by a factor of 0.1 second. +// Result is limited to 2 minute. func Do(attempts int) time.Duration { - if attempts == 0 { - return time.Duration(0) + if attempts > 13 { + return 2 * time.Minute } - return time.Duration(math.Pow(10, float64(attempts))) * time.Millisecond + return time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100 }