Fix timers

This commit is contained in:
Zoltán Papp 2024-06-18 11:20:01 +02:00
parent 5b86a7f3f2
commit f7d8d03e55
4 changed files with 18 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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