mirror of https://github.com/fiatjaf/nak.git
query batching on nak req --outbox.
This commit is contained in:
parent
13452e6916
commit
9251702460
2
go.mod
2
go.mod
|
@ -22,6 +22,7 @@ require (
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/urfave/cli/v3 v3.0.0-beta1
|
github.com/urfave/cli/v3 v3.0.0-beta1
|
||||||
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b
|
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b
|
||||||
|
golang.org/x/sync v0.16.0
|
||||||
golang.org/x/term v0.32.0
|
golang.org/x/term v0.32.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,7 +71,6 @@ require (
|
||||||
go.etcd.io/bbolt v1.4.2 // indirect
|
go.etcd.io/bbolt v1.4.2 // indirect
|
||||||
golang.org/x/crypto v0.39.0 // indirect
|
golang.org/x/crypto v0.39.0 // indirect
|
||||||
golang.org/x/net v0.41.0 // indirect
|
golang.org/x/net v0.41.0 // indirect
|
||||||
golang.org/x/sync v0.16.0 // indirect
|
|
||||||
golang.org/x/sys v0.35.0 // indirect
|
golang.org/x/sys v0.35.0 // indirect
|
||||||
golang.org/x/text v0.26.0 // indirect
|
golang.org/x/text v0.26.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|
44
req.go
44
req.go
|
@ -4,7 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"fiatjaf.com/nostr/nip42"
|
"fiatjaf.com/nostr/nip42"
|
||||||
|
@ -12,6 +14,7 @@ import (
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/mailru/easyjson"
|
"github.com/mailru/easyjson"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -186,18 +189,53 @@ example:
|
||||||
}
|
}
|
||||||
|
|
||||||
// relays for each pubkey
|
// relays for each pubkey
|
||||||
|
errg := errgroup.Group{}
|
||||||
|
errg.SetLimit(16)
|
||||||
|
mu := sync.Mutex{}
|
||||||
for _, pubkey := range filter.Authors {
|
for _, pubkey := range filter.Authors {
|
||||||
|
errg.Go(func() error {
|
||||||
n := int(c.Uint("outbox-relays-number"))
|
n := int(c.Uint("outbox-relays-number"))
|
||||||
this := filter.Clone()
|
|
||||||
this.Authors = []nostr.PubKey{pubkey}
|
|
||||||
for _, url := range sys.FetchOutboxRelays(ctx, pubkey, n) {
|
for _, url := range sys.FetchOutboxRelays(ctx, pubkey, n) {
|
||||||
|
if slices.Contains(relayUrls, url) {
|
||||||
|
// already hardcoded, ignore
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !nostr.IsValidRelayURL(url) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url }
|
||||||
|
idx := slices.IndexFunc(defs, matchUrl)
|
||||||
|
if idx == -1 {
|
||||||
|
// new relay, add it
|
||||||
|
mu.Lock()
|
||||||
|
// check again after locking to prevent races
|
||||||
|
idx = slices.IndexFunc(defs, matchUrl)
|
||||||
|
if idx == -1 {
|
||||||
|
// then add it
|
||||||
|
filter := filter.Clone()
|
||||||
|
filter.Authors = []nostr.PubKey{pubkey}
|
||||||
defs = append(defs, nostr.DirectedFilter{
|
defs = append(defs, nostr.DirectedFilter{
|
||||||
Filter: this,
|
Filter: filter,
|
||||||
Relay: url,
|
Relay: url,
|
||||||
})
|
})
|
||||||
|
mu.Unlock()
|
||||||
|
continue // done with this relay url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// otherwise we'll just use the idx
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// existing relay, add this pubkey
|
||||||
|
defs[idx].Authors = append(defs[idx].Authors, pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errg.Wait()
|
||||||
|
|
||||||
if c.Bool("stream") {
|
if c.Bool("stream") {
|
||||||
results = sys.Pool.BatchedSubscribeMany(ctx, defs, opts)
|
results = sys.Pool.BatchedSubscribeMany(ctx, defs, opts)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue