connect to relays once per call instead of in each iteration and fail early if no connection works.

This commit is contained in:
fiatjaf
2023-11-13 14:57:35 -03:00
parent 6a7a5eb26e
commit 11fe6b5809
3 changed files with 48 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import (
"os"
"strings"
"github.com/nbd-wtf/go-nostr"
"github.com/urfave/cli/v2"
)
@@ -99,6 +100,21 @@ func validate32BytesHex(target string) error {
return nil
}
func connectToAllRelays(ctx context.Context, relayUrls []string) (*nostr.SimplePool, []*nostr.Relay) {
relays := make([]*nostr.Relay, 0, len(relayUrls))
pool := nostr.NewSimplePool(ctx)
for _, url := range relayUrls {
fmt.Fprintf(os.Stderr, "connecting to %s... ", url)
if relay, err := pool.EnsureRelay(url); err == nil {
relays = append(relays, relay)
fmt.Fprintf(os.Stderr, "ok.\n")
} else {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}
}
return pool, relays
}
func lineProcessingError(c *cli.Context, msg string, args ...any) {
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
fmt.Fprintf(os.Stderr, msg+"\n", args...)