From f7d8d03e55f91e4adae7509462f6b4e40981e142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Tue, 18 Jun 2024 11:20:01 +0200 Subject: [PATCH] Fix timers --- client/internal/peer/conn_ice.go | 6 +++++- client/internal/peer/conn_relay.go | 6 +++++- client/internal/peer/handshaker.go | 6 ++++-- relay/client/guard.go | 5 ++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/client/internal/peer/conn_ice.go b/client/internal/peer/conn_ice.go index 35b306068..2dacd0c29 100644 --- a/client/internal/peer/conn_ice.go +++ b/client/internal/peer/conn_ice.go @@ -360,10 +360,14 @@ func (conn *ConnectorICE) waitForReconnectTry() bool { minWait := 500 maxWait := 2000 duration := time.Duration(rand.Intn(maxWait-minWait)+minWait) * time.Millisecond + + timeout := time.NewTimer(duration) + defer timeout.Stop() + select { case <-conn.ctx.Done(): return false - case <-time.After(duration): + case <-timeout.C: return true } } diff --git a/client/internal/peer/conn_relay.go b/client/internal/peer/conn_relay.go index bbd1af219..ae42a4597 100644 --- a/client/internal/peer/conn_relay.go +++ b/client/internal/peer/conn_relay.go @@ -108,10 +108,14 @@ func (conn *ConnectorRelay) waitForReconnectTry() bool { minWait := 500 maxWait := 2000 duration := time.Duration(rand.Intn(maxWait-minWait)+minWait) * time.Millisecond + + timeout := time.NewTimer(duration) + defer timeout.Stop() + select { case <-conn.ctx.Done(): return false - case <-time.After(duration): + case <-timeout.C: return true } } diff --git a/client/internal/peer/handshaker.go b/client/internal/peer/handshaker.go index f2f2cff59..e10705f63 100644 --- a/client/internal/peer/handshaker.go +++ b/client/internal/peer/handshaker.go @@ -112,7 +112,6 @@ func (h *Handshaker) Handshake(args HandshakeArgs) (*OfferAnswer, error) { // OnRemoteOffer handles an offer from the remote peer and returns true if the message was accepted, false otherwise // doesn't block, discards the message if connection wasn't ready func (h *Handshaker) OnRemoteOffer(offer OfferAnswer) bool { - select { case h.remoteOffersCh <- offer: return true @@ -173,6 +172,9 @@ func (h *Handshaker) sendAnswer() error { } func (h *Handshaker) waitForRemoteOfferConfirmation() (*OfferAnswer, error) { + timeout := time.NewTimer(h.config.Timeout) + defer timeout.Stop() + select { case remoteOfferAnswer := <-h.remoteOffersCh: // received confirmation from the remote peer -> ready to proceed @@ -183,7 +185,7 @@ func (h *Handshaker) waitForRemoteOfferConfirmation() (*OfferAnswer, error) { return &remoteOfferAnswer, nil case remoteOfferAnswer := <-h.remoteAnswerCh: return &remoteOfferAnswer, nil - case <-time.After(h.config.Timeout): + case <-timeout.C: return nil, NewConnectionTimeoutError(h.config.Key, h.config.Timeout) case <-h.ctx.Done(): // closed externally diff --git a/relay/client/guard.go b/relay/client/guard.go index 47a6ff722..12fae4c54 100644 --- a/relay/client/guard.go +++ b/relay/client/guard.go @@ -24,8 +24,11 @@ func NewGuard(context context.Context, relayClient *Client) *Guard { } func (g *Guard) OnDisconnected() { + timeout := time.NewTimer(reconnectingTimeout) + defer timeout.Stop() + select { - case <-time.After(time.Second): + case <-timeout.C: _ = g.relayClient.Connect() case <-g.ctx.Done(): return