mirror of
https://github.com/fiatjaf/nak.git
synced 2024-10-30 09:09:08 -04:00
connect to relays once per call instead of in each iteration and fail early if no connection works.
This commit is contained in:
parent
6a7a5eb26e
commit
11fe6b5809
18
event.go
18
event.go
|
@ -97,6 +97,16 @@ example:
|
||||||
},
|
},
|
||||||
ArgsUsage: "[relay...]",
|
ArgsUsage: "[relay...]",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
// try to connect to the relays here
|
||||||
|
var relays []*nostr.Relay
|
||||||
|
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
||||||
|
_, relays = connectToAllRelays(c.Context, relayUrls)
|
||||||
|
if len(relays) == 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// gather the secret key first
|
// gather the secret key first
|
||||||
sec := c.String("sec")
|
sec := c.String("sec")
|
||||||
if c.Bool("prompt-sec") {
|
if c.Bool("prompt-sec") {
|
||||||
|
@ -204,12 +214,12 @@ example:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
relays := c.Args().Slice()
|
|
||||||
if len(relays) > 0 {
|
if len(relays) > 0 {
|
||||||
fmt.Println(evt.String())
|
fmt.Println(evt.String())
|
||||||
for _, url := range relays {
|
os.Stdout.Sync()
|
||||||
fmt.Fprintf(os.Stderr, "publishing to %s... ", url)
|
for _, relay := range relays {
|
||||||
if relay, err := nostr.RelayConnect(c.Context, url); err != nil {
|
fmt.Fprintf(os.Stderr, "publishing to %s... ", relay.URL)
|
||||||
|
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err)
|
fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err)
|
||||||
} else {
|
} else {
|
||||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
|
|
16
helpers.go
16
helpers.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -99,6 +100,21 @@ func validate32BytesHex(target string) error {
|
||||||
return nil
|
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) {
|
func lineProcessingError(c *cli.Context, msg string, args ...any) {
|
||||||
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
||||||
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
||||||
|
|
22
req.go
22
req.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
@ -95,6 +96,21 @@ example:
|
||||||
},
|
},
|
||||||
ArgsUsage: "[relay...]",
|
ArgsUsage: "[relay...]",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
var pool *nostr.SimplePool
|
||||||
|
relayUrls := c.Args().Slice()
|
||||||
|
if len(relayUrls) > 0 {
|
||||||
|
var relays []*nostr.Relay
|
||||||
|
pool, relays = connectToAllRelays(c.Context, relayUrls)
|
||||||
|
if len(relays) == 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
|
relayUrls = make([]string, len(relays))
|
||||||
|
for i, relay := range relays {
|
||||||
|
relayUrls[i] = relay.URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for stdinFilter := range getStdinLinesOrBlank() {
|
for stdinFilter := range getStdinLinesOrBlank() {
|
||||||
filter := nostr.Filter{}
|
filter := nostr.Filter{}
|
||||||
if stdinFilter != "" {
|
if stdinFilter != "" {
|
||||||
|
@ -155,14 +171,12 @@ example:
|
||||||
filter.Limit = limit
|
filter.Limit = limit
|
||||||
}
|
}
|
||||||
|
|
||||||
relays := c.Args().Slice()
|
if len(relayUrls) > 0 {
|
||||||
if len(relays) > 0 {
|
|
||||||
pool := nostr.NewSimplePool(c.Context)
|
|
||||||
fn := pool.SubManyEose
|
fn := pool.SubManyEose
|
||||||
if c.Bool("stream") {
|
if c.Bool("stream") {
|
||||||
fn = pool.SubMany
|
fn = pool.SubMany
|
||||||
}
|
}
|
||||||
for ie := range fn(c.Context, relays, nostr.Filters{filter}) {
|
for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) {
|
||||||
fmt.Println(ie.Event)
|
fmt.Println(ie.Event)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user