mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
bunker: send responses to relays concurrently.
This commit is contained in:
parent
5dd5a7c699
commit
e008e08105
25
bunker.go
25
bunker.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
@ -85,10 +86,12 @@ var bunker = &cli.Command{
|
||||||
})
|
})
|
||||||
|
|
||||||
signer := nip46.NewStaticKeySigner(sec)
|
signer := nip46.NewStaticKeySigner(sec)
|
||||||
|
handlerWg := sync.WaitGroup{}
|
||||||
|
printLock := sync.Mutex{}
|
||||||
for ie := range events {
|
for ie := range events {
|
||||||
req, resp, eventResponse, harmless, err := signer.HandleRequest(ie.Event)
|
req, resp, eventResponse, harmless, err := signer.HandleRequest(ie.Event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log("< failed to handle request from %s: %s", ie.Event.PubKey, err.Error())
|
log("< failed to handle request from %s: %s\n", ie.Event.PubKey, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,15 +101,23 @@ var bunker = &cli.Command{
|
||||||
log("~ responding with %s\n", string(jresp))
|
log("~ responding with %s\n", string(jresp))
|
||||||
|
|
||||||
if alwaysYes || harmless || askProceed(ie.Event.PubKey) {
|
if alwaysYes || harmless || askProceed(ie.Event.PubKey) {
|
||||||
|
handlerWg.Add(len(relayURLs))
|
||||||
for _, relayURL := range relayURLs {
|
for _, relayURL := range relayURLs {
|
||||||
if relay, _ := pool.EnsureRelay(relayURL); relay != nil {
|
go func(relayURL string) {
|
||||||
if err := relay.Publish(c.Context, eventResponse); err == nil {
|
if relay, _ := pool.EnsureRelay(relayURL); relay != nil {
|
||||||
log("* sent response through %s\n", relay.URL)
|
err := relay.Publish(c.Context, eventResponse)
|
||||||
} else {
|
printLock.Lock()
|
||||||
log("* failed to send response: %s\n", err)
|
if err == nil {
|
||||||
|
log("* sent response through %s\n", relay.URL)
|
||||||
|
} else {
|
||||||
|
log("* failed to send response: %s\n", err)
|
||||||
|
}
|
||||||
|
printLock.Unlock()
|
||||||
|
handlerWg.Done()
|
||||||
}
|
}
|
||||||
}
|
}(relayURL)
|
||||||
}
|
}
|
||||||
|
handlerWg.Wait()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user