Skip to content

Commit

Permalink
fix: dealer and accesspoint reconnection fixes
Browse files Browse the repository at this point in the history
Amend to #69
  • Loading branch information
devgianlu committed Aug 21, 2024
1 parent cdd6a53 commit fcbfa2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 3 additions & 3 deletions ap/ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ loop:
}
}

// always close as we might end up here because of application errors
_ = ap.conn.Close()

// if we shouldn't stop, try to reconnect
if !ap.stop {
// Only close when not stopping, as Close() handles it when stopping
_ = ap.conn.Close()

ap.connMu.Lock()
if err := backoff.Retry(ap.reconnect, backoff.NewExponentialBackOff()); err != nil {
log.WithError(err).Errorf("failed reconnecting accesspoint, bye bye")
Expand Down
17 changes: 8 additions & 9 deletions dealer/dealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (d *Dealer) Close() {
func (d *Dealer) startReceiving() {
d.recvLoopOnce.Do(func() {
log.Tracef("starting dealer recv loop")
go d.recvLoop()

// set last pong in the future
d.lastPong = time.Now().Add(pingInterval)

go d.recvLoop()
go d.pingTicker()
})
}
Expand Down Expand Up @@ -171,12 +171,11 @@ loop:
// no need to hold the connMu since reconnection happens in this routine
msgType, messageBytes, err := d.conn.Read(context.Background())

// Don't log closed error if we're stopping
// don't log closed error if we're stopping
if d.stop && websocket.CloseStatus(err) == websocket.StatusGoingAway {
log.Debugf("dealer connection closed")
break
}
if err != nil {
break loop
} else if err != nil {
log.WithError(err).Errorf("failed receiving dealer message")
break loop
} else if msgType != websocket.MessageText {
Expand Down Expand Up @@ -211,11 +210,11 @@ loop:
}
}

// always close as we might end up here because of application errors
_ = d.conn.Close(websocket.StatusInternalError, "")

// if we shouldn't stop, try to reconnect
if !d.stop {
// Only close when not stopping, as Close() handles it when stopping
_ = d.conn.Close(websocket.StatusInternalError, "")

d.connMu.Lock()
if err := backoff.Retry(d.reconnect, backoff.NewExponentialBackOff()); err != nil {
log.WithError(err).Errorf("failed reconnecting dealer, bye bye")
Expand Down

0 comments on commit fcbfa2c

Please sign in to comment.