mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-19 18:47:37 +01:00
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
This commit is contained in:
@@ -59,7 +59,7 @@ func (c *Connection) Close() error {
|
|||||||
|
|
||||||
// Ping sends a ping message to the websocket connection.
|
// Ping sends a ping message to the websocket connection.
|
||||||
func (c *Connection) Ping(ctx context.Context) error {
|
func (c *Connection) Ping(ctx context.Context) error {
|
||||||
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*800, errors.New("ping took too long"))
|
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*10000, errors.New("ping took too long"))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return c.conn.Ping(ctx)
|
return c.conn.Ping(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
22
relay.go
22
relay.go
@@ -9,7 +9,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -168,6 +167,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
|||||||
|
|
||||||
// queue all write operations here so we don't do mutex spaghetti
|
// queue all write operations here so we don't do mutex spaghetti
|
||||||
go func() {
|
go func() {
|
||||||
|
pingAttempt := 0
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-r.connectionContext.Done():
|
case <-r.connectionContext.Done():
|
||||||
@@ -180,12 +180,24 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
|||||||
return
|
return
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
debugLogf("{%s} pinging relay", r.URL)
|
||||||
err := r.Connection.Ping(r.connectionContext)
|
err := r.Connection.Ping(r.connectionContext)
|
||||||
if err != nil && !strings.Contains(err.Error(), "failed to wait for pong") {
|
if err != nil {
|
||||||
InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
|
pingAttempt++
|
||||||
r.Close() // this should trigger a context cancelation
|
debugLogf("{%s} error writing ping (attempt %d): %v", r.URL, pingAttempt, err)
|
||||||
return
|
|
||||||
|
if pingAttempt >= 3 {
|
||||||
|
debugLogf("{%s} error writing ping after multiple attempts; closing websocket", r.URL)
|
||||||
|
err = r.Close() // this should trigger a context cancelation
|
||||||
|
if err != nil {
|
||||||
|
debugLogf("{%s} failed to close relay: %v", r.URL, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
// ping was OK
|
||||||
|
debugLogf("{%s} ping OK", r.URL)
|
||||||
|
pingAttempt = 0
|
||||||
|
|
||||||
case writeRequest := <-r.writeQueue:
|
case writeRequest := <-r.writeQueue:
|
||||||
// all write requests will go through this to prevent races
|
// all write requests will go through this to prevent races
|
||||||
|
|||||||
Reference in New Issue
Block a user