New backoff (#1153)

* new backoff function

* use backoff from util/backoff

* remove reset atemts

* change comment

* fmt
This commit is contained in:
tpam28 2020-02-02 23:32:55 +03:00 committed by GitHub
parent 079102ea59
commit 449bcb46fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 18 deletions

View File

@ -2,13 +2,13 @@ package client
import ( import (
"context" "context"
"math"
"time" "time"
"github.com/micro/go-micro/v2/util/backoff"
) )
type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error) 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) { 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
} }

View File

@ -225,9 +225,6 @@ func (n *network) acceptNetConn(l tunnel.Listener, recv chan *message) {
sleep := backoff.Do(i) sleep := backoff.Do(i)
log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep) log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep)
time.Sleep(sleep) time.Sleep(sleep)
if i > 5 {
i = 0
}
i++ i++
continue continue
} }
@ -255,10 +252,6 @@ func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *message) {
sleep := backoff.Do(i) sleep := backoff.Do(i)
log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep) log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep)
time.Sleep(sleep) time.Sleep(sleep)
if i > 5 {
// reset the counter
i = 0
}
i++ i++
continue continue
} }
@ -1572,11 +1565,6 @@ func (n *network) connect() {
case <-time.After(time.Second + backoff.Do(attempts)): case <-time.After(time.Second + backoff.Do(attempts)):
// we have to try again // we have to try again
attempts++ attempts++
// reset attempts 5 == ~2mins
if attempts > 5 {
attempts = 0
}
} }
} }
} }

View File

@ -6,9 +6,11 @@ import (
"time" "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 { func Do(attempts int) time.Duration {
if attempts == 0 { if attempts > 13 {
return time.Duration(0) 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
} }