From bdfd72d7ad34fad1830717bbbf6232cf1abbe8d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Tue, 29 Oct 2024 16:29:05 +0100 Subject: [PATCH] Use exponent ticker --- relay/client/guard.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/relay/client/guard.go b/relay/client/guard.go index 89484b13ad..db189904f8 100644 --- a/relay/client/guard.go +++ b/relay/client/guard.go @@ -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) +}