make the relay connection timeout configurable.

This commit is contained in:
fiatjaf
2026-07-21 20:41:45 -03:00
parent aac995714b
commit 6855722ba5
5 changed files with 25 additions and 9 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strings"
"time"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip45"
@@ -67,7 +66,7 @@ var count = &cli.Command{
nm := nostr.NormalizeURL(relayUrl)
relay, ok := sys.Pool.Relays.Load(nm)
if !ok || relay == nil || !relay.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
relay, err = nostr.RelayConnect(ct, relayUrl, sys.Pool.RelayOptions)
cancel()

View File

@@ -548,7 +548,7 @@ func publishFlow(ctx context.Context, c *cli.Command, kr nostr.Signer, evt nostr
relays[i] = r
relay = r
} else {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
new_, err := nostr.RelayConnect(ct, relay.URL, sys.Pool.RelayOptions)
cancel()
if err == nil {

View File

@@ -246,7 +246,7 @@ var group = &cli.Command{
nm := nostr.NormalizeURL(relay)
r, ok := sys.Pool.Relays.Load(nm)
if !ok || r == nil || !r.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
cancel()
@@ -327,7 +327,7 @@ var group = &cli.Command{
nm := nostr.NormalizeURL(relay)
r, ok := sys.Pool.Relays.Load(nm)
if !ok || r == nil || !r.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
cancel()
@@ -493,7 +493,7 @@ write your forum post
nm := nostr.NormalizeURL(relay)
r, ok := sys.Pool.Relays.Load(nm)
if !ok || r == nil || !r.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
cancel()
@@ -608,7 +608,7 @@ write your forum post
nm := nostr.NormalizeURL(relay)
r, ok := sys.Pool.Relays.Load(nm)
if !ok || r == nil || !r.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
cancel()
@@ -941,7 +941,7 @@ func publishModerationEvent(ctx context.Context, c *cli.Command, kind nostr.Kind
nm := nostr.NormalizeURL(relay)
r, ok := sys.Pool.Relays.Load(nm)
if !ok || r == nil || !r.IsConnected() {
ct, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ct, cancel := context.WithTimeout(context.Background(), connectTimeout)
var err error
r, err = nostr.RelayConnect(ct, relay, sys.Pool.RelayOptions)
cancel()

View File

@@ -48,6 +48,8 @@ var (
stdout = func(args ...any) { fmt.Fprintln(color.Output, args...) }
)
var connectTimeout = 2 * time.Second
func isPiped() bool {
stat, err := os.Stdin.Stat()
if err != nil {
@@ -272,7 +274,7 @@ func connectToSingleRelay(
relay, ok := sys.Pool.Relays.Load(nm)
if !ok || relay == nil || !relay.IsConnected() {
connectCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
connectCtx, cancel := context.WithTimeout(context.Background(), connectTimeout)
defer cancel()
var err error

15
main.go
View File

@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip42"
@@ -42,6 +43,7 @@ var defaultKeyFlags = []cli.Flag{
Name: "prompt-sec",
Usage: "prompt the user to paste a hex or nsec with which to sign the event",
Category: CATEGORY_SIGNER,
Hidden: true,
},
&SecretKeyFlag{
Name: "connect-as",
@@ -50,6 +52,7 @@ var defaultKeyFlags = []cli.Flag{
Sources: cli.EnvVars("NOSTR_CLIENT_KEY"),
Value: defaultKey(),
DefaultText: "the default key (see `nak key default`)",
Hidden: true,
},
}
@@ -128,6 +131,7 @@ var app = &cli.Command{
}
return nil
},
Hidden: true,
},
&cli.BoolFlag{
Name: "verbose",
@@ -141,6 +145,17 @@ var app = &cli.Command{
}
return nil
},
Hidden: true,
},
&cli.DurationFlag{
Name: "connect-timeout",
Usage: "timeout for connecting to relays",
Value: connectTimeout,
Action: func(ctx context.Context, c *cli.Command, d time.Duration) error {
connectTimeout = d
return nil
},
Hidden: true,
},
},
defaultKeyFlags,