From e008e08105cd5e9ee791c8c5c24f03af9f2d484b Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 16 Feb 2024 11:08:48 -0300 Subject: [PATCH] bunker: send responses to relays concurrently. --- bunker.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/bunker.go b/bunker.go index 1a0d24a..2c53b20 100644 --- a/bunker.go +++ b/bunker.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "sync" "github.com/fatih/color" "github.com/nbd-wtf/go-nostr" @@ -85,10 +86,12 @@ var bunker = &cli.Command{ }) signer := nip46.NewStaticKeySigner(sec) + handlerWg := sync.WaitGroup{} + printLock := sync.Mutex{} for ie := range events { req, resp, eventResponse, harmless, err := signer.HandleRequest(ie.Event) 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 } @@ -98,15 +101,23 @@ var bunker = &cli.Command{ log("~ responding with %s\n", string(jresp)) if alwaysYes || harmless || askProceed(ie.Event.PubKey) { + handlerWg.Add(len(relayURLs)) for _, relayURL := range relayURLs { - if relay, _ := pool.EnsureRelay(relayURL); relay != nil { - if err := relay.Publish(c.Context, eventResponse); err == nil { - log("* sent response through %s\n", relay.URL) - } else { - log("* failed to send response: %s\n", err) + go func(relayURL string) { + if relay, _ := pool.EnsureRelay(relayURL); relay != nil { + err := relay.Publish(c.Context, eventResponse) + printLock.Lock() + 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() } }