mirror of
https://github.com/fiatjaf/nak.git
synced 2026-06-04 17:51:15 +02:00
bunker: fix waiting for slow relays?
This commit is contained in:
94
bunker.go
94
bunker.go
@@ -452,54 +452,56 @@ var bunker = &cli.Command{
|
|||||||
for ie := range events {
|
for ie := range events {
|
||||||
cancelPreviousBunkerInfoPrint() // this prevents us from printing a million bunker info blocks
|
cancelPreviousBunkerInfoPrint() // this prevents us from printing a million bunker info blocks
|
||||||
|
|
||||||
// handle the NIP-46 request event
|
|
||||||
from := ie.Event.PubKey
|
|
||||||
req, resp, eventResponse, err := signer.HandleRequest(ctx, ie.Event)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, nip46.AlreadyHandled) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log("< failed to handle request from %s: %s\n", from.Hex(), err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
jreq, _ := json.MarshalIndent(req, "", " ")
|
|
||||||
log("- got request from '%s': %s\n", color.New(color.Bold, color.FgBlue).Sprint(from.Hex()), string(jreq))
|
|
||||||
jresp, _ := json.MarshalIndent(resp, "", " ")
|
|
||||||
log("~ responding with %s\n", string(jresp))
|
|
||||||
|
|
||||||
// use custom relays if they are defined for this client
|
|
||||||
// (normally if the initial connection came from a nostrconnect:// URL)
|
|
||||||
relays := relayURLs
|
|
||||||
for _, c := range config.Clients {
|
|
||||||
if c.PubKey == from && len(c.CustomRelays) > 0 {
|
|
||||||
relays = c.CustomRelays
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for res := range sys.Pool.PublishMany(ctx, relays, eventResponse) {
|
|
||||||
if res.Error == nil {
|
|
||||||
log("* sent response through %s\n", res.Relay.URL)
|
|
||||||
} else {
|
|
||||||
log("* failed to send response through %s: %s\n", res.RelayURL, res.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// just after handling one request we trigger this
|
|
||||||
go func() {
|
go func() {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
// handle the NIP-46 request event
|
||||||
defer cancel()
|
from := ie.Event.PubKey
|
||||||
cancelPreviousBunkerInfoPrint = cancel
|
req, resp, eventResponse, err := signer.HandleRequest(ctx, ie.Event)
|
||||||
// the idea is that we will print the bunker URL again so it is easier to copy-paste by users
|
if err != nil {
|
||||||
// but we will only do if the bunker is inactive for more than 5 minutes
|
if errors.Is(err, nip46.AlreadyHandled) {
|
||||||
select {
|
return
|
||||||
case <-ctx.Done():
|
}
|
||||||
case <-time.After(time.Minute * 5):
|
|
||||||
log("\n")
|
log("< failed to handle request from %s: %s\n", from.Hex(), err.Error())
|
||||||
printBunkerInfo()
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jreq, _ := json.MarshalIndent(req, "", " ")
|
||||||
|
log("- got request from '%s': %s\n", color.New(color.Bold, color.FgBlue).Sprint(from.Hex()), string(jreq))
|
||||||
|
jresp, _ := json.MarshalIndent(resp, "", " ")
|
||||||
|
log("~ responding with %s\n", string(jresp))
|
||||||
|
|
||||||
|
// use custom relays if they are defined for this client
|
||||||
|
// (normally if the initial connection came from a nostrconnect:// URL)
|
||||||
|
relays := relayURLs
|
||||||
|
for _, c := range config.Clients {
|
||||||
|
if c.PubKey == from && len(c.CustomRelays) > 0 {
|
||||||
|
relays = c.CustomRelays
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for res := range sys.Pool.PublishMany(ctx, relays, eventResponse) {
|
||||||
|
if res.Error == nil {
|
||||||
|
log("* sent response through %s\n", res.Relay.URL)
|
||||||
|
} else {
|
||||||
|
log("* failed to send response through %s: %s\n", res.RelayURL, res.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// just after handling one request we trigger this
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
cancelPreviousBunkerInfoPrint = cancel
|
||||||
|
// the idea is that we will print the bunker URL again so it is easier to copy-paste by users
|
||||||
|
// but we will only do if the bunker is inactive for more than 5 minutes
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-time.After(time.Minute * 5):
|
||||||
|
log("\n")
|
||||||
|
printBunkerInfo()
|
||||||
|
}
|
||||||
|
}()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user