Skip to content

Commit

Permalink
fix pinging relays: use the main relay context, not the relay connect…
Browse files Browse the repository at this point in the history
…ion temporary context.
  • Loading branch information
fiatjaf committed Jan 17, 2025
1 parent 06a15fd commit 3e1c0dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 4 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package nostr
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"time"

ws "github.com/coder/websocket"
)
Expand Down Expand Up @@ -51,5 +53,7 @@ func (c *Connection) Close() error {
}

func (c *Connection) Ping(ctx context.Context) error {
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*800, errors.New("ping took too long"))
defer cancel()
return c.conn.Ping(ctx)
}
3 changes: 2 additions & 1 deletion relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
select {
case <-ticker.C:
if r.Connection != nil {
err := r.Connection.Ping(ctx)
err := r.Connection.Ping(r.connectionContext)
if err != nil {
InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
r.Close() // this should trigger a context cancelation
Expand All @@ -213,6 +213,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error

for {
buf.Reset()

if err := conn.ReadMessage(r.connectionContext, buf); err != nil {
r.ConnectionError = err
r.close(err)
Expand Down
10 changes: 4 additions & 6 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ func TestFollowListRecursion(t *testing.T) {
results := make(chan result)
go func() {
for _, item := range followList.Items {
go func() {
fl := sys.FetchFollowList(ctx, item.Pubkey)
meta := sys.FetchProfileMetadata(ctx, item.Pubkey)
fmt.Println(" ~", item.Pubkey, meta.Name, len(fl.Items))
results <- result{item.Pubkey, fl, meta}
}()
fl := sys.FetchFollowList(ctx, item.Pubkey)
meta := sys.FetchProfileMetadata(ctx, item.Pubkey)
fmt.Println(" ~", item.Pubkey, meta.Name, len(fl.Items))
results <- result{item.Pubkey, fl, meta}
}
}()

Expand Down

0 comments on commit 3e1c0dd

Please sign in to comment.