use set/get for reconnectingTimeout

This commit is contained in:
Maycon Santos
2025-07-20 22:16:35 +02:00
parent d7d57a4ec4
commit cc1c77f6dc
2 changed files with 17 additions and 3 deletions

View File

@@ -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)

View File

@@ -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")