Skip to content
This repository was archived by the owner on Jan 24, 2026. It is now read-only.

Commit 78da155

Browse files
authored
Fix: pool reconnect (#200)
* fix: bump ping timeout for poor connections * fix: always close relay on ping failure * fix: add multiple ping attempts before closing relay connection * fix: only 3 ping attempts * fix: pool check for relay context done * fix: include relay URL in debug log * fix: do not return after closing relay on max ping attempts * fix: compile error
1 parent fff8322 commit 78da155

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (c *Connection) Close() error {
5959

6060
// Ping sends a ping message to the websocket connection.
6161
func (c *Connection) Ping(ctx context.Context) error {
62-
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*800, errors.New("ping took too long"))
62+
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*10000, errors.New("ping took too long"))
6363
defer cancel()
6464
return c.conn.Ping(ctx)
6565
}

relay.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"log"
1010
"net/http"
1111
"strconv"
12-
"strings"
1312
"sync"
1413
"sync/atomic"
1514
"time"
@@ -168,6 +167,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
168167

169168
// queue all write operations here so we don't do mutex spaghetti
170169
go func() {
170+
pingAttempt := 0
171171
for {
172172
select {
173173
case <-r.connectionContext.Done():
@@ -180,12 +180,24 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
180180
return
181181

182182
case <-ticker.C:
183+
debugLogf("{%s} pinging relay", r.URL)
183184
err := r.Connection.Ping(r.connectionContext)
184-
if err != nil && !strings.Contains(err.Error(), "failed to wait for pong") {
185-
InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
186-
r.Close() // this should trigger a context cancelation
187-
return
185+
if err != nil {
186+
pingAttempt++
187+
debugLogf("{%s} error writing ping (attempt %d): %v", r.URL, pingAttempt, err)
188+
189+
if pingAttempt >= 3 {
190+
debugLogf("{%s} error writing ping after multiple attempts; closing websocket", r.URL)
191+
err = r.Close() // this should trigger a context cancelation
192+
if err != nil {
193+
debugLogf("{%s} failed to close relay: %v", r.URL, err)
194+
}
195+
}
196+
continue
188197
}
198+
// ping was OK
199+
debugLogf("{%s} ping OK", r.URL)
200+
pingAttempt = 0
189201

190202
case writeRequest := <-r.writeQueue:
191203
// all write requests will go through this to prevent races

0 commit comments

Comments
 (0)