Use exponent ticker

This commit is contained in:
Zoltán Papp 2024-10-29 16:29:05 +01:00
parent 0f5dc1bbf4
commit bdfd72d7ad

View File

@ -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.
@ -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 {
@ -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)
}