Skip to content

Commit

Permalink
Use exponent ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz committed Oct 29, 2024
1 parent 0f5dc1b commit bdfd72d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions relay/client/guard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
log "github.com/sirupsen/logrus"
)

var (
reconnectingTimeout = 5 * time.Second
)

// Guard manage the reconnection tries to the Relay server in case of disconnection event.
type Guard struct {
// OnNewRelayClient is a channel that is used to notify the relay client about a new relay client instance.
Expand Down Expand Up @@ -46,8 +43,7 @@ func (g *Guard) StartReconnectTrys(ctx context.Context, relayClient *Client) {
}

RETRY:
// todo use exponent ticker
ticker := time.NewTicker(reconnectingTimeout)
ticker := exponentTicker(ctx)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -110,3 +106,14 @@ func (g *Guard) isServerURLStillValid(rc *Client) bool {
}
return false
}

func exponentTicker(ctx context.Context) *backoff.Ticker {
bo := backoff.WithContext(&backoff.ExponentialBackOff{
InitialInterval: 2 * time.Second,
Multiplier: 2,
MaxInterval: 60 * time.Second,
Clock: backoff.SystemClock,
}, ctx)

return backoff.NewTicker(bo)
}

0 comments on commit bdfd72d

Please sign in to comment.