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
+15 -1
View File
@@ -2,6 +2,7 @@ package client
import ( import (
"context" "context"
"sync"
"time" "time"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
@@ -10,8 +11,21 @@ import (
var ( var (
reconnectingTimeout = 60 * time.Second 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. // Guard manage the reconnection tries to the Relay server in case of disconnection event.
type Guard struct { type Guard struct {
// OnNewRelayClient is a channel that is used to notify the relay manager about a new relay client instance. // 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{ bo := backoff.WithContext(&backoff.ExponentialBackOff{
InitialInterval: 2 * time.Second, InitialInterval: 2 * time.Second,
Multiplier: 2, Multiplier: 2,
MaxInterval: reconnectingTimeout, MaxInterval: getReconnectingTimeout(),
Clock: backoff.SystemClock, Clock: backoff.SystemClock,
}, ctx) }, ctx)
+2 -2
View File
@@ -301,7 +301,7 @@ func TestForeginAutoClose(t *testing.T) {
func TestAutoReconnect(t *testing.T) { func TestAutoReconnect(t *testing.T) {
ctx := context.Background() ctx := context.Background()
reconnectingTimeout = 2 * time.Second setReconnectingTimeout(2 * time.Second)
srvCfg := server.ListenerConfig{ srvCfg := server.ListenerConfig{
Address: "localhost:1234", Address: "localhost:1234",
@@ -362,7 +362,7 @@ func TestAutoReconnect(t *testing.T) {
} }
log.Infof("waiting for reconnection") log.Infof("waiting for reconnection")
time.Sleep(reconnectingTimeout + 1*time.Second) time.Sleep(getReconnectingTimeout() + 1*time.Second)
log.Infof("reopent the connection") log.Infof("reopent the connection")
_, err = clientAlice.OpenConn(ctx, ra, "bob") _, err = clientAlice.OpenConn(ctx, ra, "bob")