Skip to content

Commit

Permalink
fix: try multiple times to connect to an AP
Browse files Browse the repository at this point in the history
I sometimes get "connection refused" for no apparent reason, even though
the same AP works at other times. So when that happens, try again with a
different AP.

I don't know why this happens, but with this patch the connection code
should be a bit more robust.

(cherry picked from commit 0ad7f6a)
  • Loading branch information
aykevl authored and devgianlu committed Sep 17, 2024
1 parent d1cf460 commit 8b64df6
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions ap/ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,26 @@ func (ap *Accesspoint) init() (err error) {
}

// open connection to accesspoint
ap.conn, err = net.Dial("tcp", ap.addr())
if err != nil {
return fmt.Errorf("failed dialing accesspoint: %w", err)
attempts := 0
for {
attempts++
addr := ap.addr()
ap.conn, err = net.DialTimeout("tcp", addr, time.Second*30)
if err == nil {
// Successfully connected.
log.Infoln("connected to", addr)

// set last ping in the future
ap.lastPongAck = time.Now().Add(pongAckInterval)

return nil
} else if attempts >= 6 {
// Only try a few times before giving up.
return fmt.Errorf("failed to connect to AP %v: %e", addr, err)
}
// Try again with a different AP.
log.Warnf("failed to connect to AP %v (error: %v), retrying with a different AP", addr, err)
}

// set last ping in the future
ap.lastPongAck = time.Now().Add(pongAckInterval)

return nil
}

func (ap *Accesspoint) ConnectUserPass(username, password string) error {
Expand Down

0 comments on commit 8b64df6

Please sign in to comment.