From cc1c77f6dcdbea47418ecf08fc85906e12807118 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Sun, 20 Jul 2025 22:16:35 +0200 Subject: [PATCH] use set/get for reconnectingTimeout --- relay/client/guard.go | 16 +++++++++++++++- relay/client/manager_test.go | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/relay/client/guard.go b/relay/client/guard.go index 100892d81..33f617b0b 100644 --- a/relay/client/guard.go +++ b/relay/client/guard.go @@ -2,6 +2,7 @@ package client import ( "context" + "sync" "time" "github.com/cenkalti/backoff/v4" @@ -10,8 +11,21 @@ import ( var ( reconnectingTimeout = 60 * time.Second + mux sync.Mutex ) +func getReconnectingTimeout() time.Duration { + mux.Lock() + defer mux.Unlock() + return reconnectingTimeout +} + +func setReconnectingTimeout(timeout time.Duration) { + mux.Lock() + defer mux.Unlock() + reconnectingTimeout = timeout +} + // 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 manager about a new relay client instance. @@ -128,7 +142,7 @@ func exponentTicker(ctx context.Context) *backoff.Ticker { bo := backoff.WithContext(&backoff.ExponentialBackOff{ InitialInterval: 2 * time.Second, Multiplier: 2, - MaxInterval: reconnectingTimeout, + MaxInterval: getReconnectingTimeout(), Clock: backoff.SystemClock, }, ctx) diff --git a/relay/client/manager_test.go b/relay/client/manager_test.go index 96d46b09e..93793b15c 100644 --- a/relay/client/manager_test.go +++ b/relay/client/manager_test.go @@ -301,7 +301,7 @@ func TestForeginAutoClose(t *testing.T) { func TestAutoReconnect(t *testing.T) { ctx := context.Background() - reconnectingTimeout = 2 * time.Second + setReconnectingTimeout(2 * time.Second) srvCfg := server.ListenerConfig{ Address: "localhost:1234", @@ -362,7 +362,7 @@ func TestAutoReconnect(t *testing.T) { } log.Infof("waiting for reconnection") - time.Sleep(reconnectingTimeout + 1*time.Second) + time.Sleep(getReconnectingTimeout() + 1*time.Second) log.Infof("reopent the connection") _, err = clientAlice.OpenConn(ctx, ra, "bob")