Skip to content

Commit

Permalink
nip46: do not allow one faulty relay to break RPC calls to other relays.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Aug 29, 2024
1 parent 1db4da7 commit 5edb54e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions nip46/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,25 @@ func (bunker *BunkerClient) RPC(ctx context.Context, method string, params []str

respWaiter := make(chan Response)
bunker.listeners.Store(id, respWaiter)
hasWorked := false

for _, r := range bunker.relays {
relay, err := bunker.pool.EnsureRelay(r)
if err == nil {
hasWorked = true
}
relay.Publish(ctx, evt)
hasWorked := make(chan struct{})

for _, url := range bunker.relays {
go func(url string) {
relay, err := bunker.pool.EnsureRelay(url)
if err == nil {
select {
case hasWorked <- struct{}{}:
default:
}
}
relay.Publish(ctx, evt)
}(url)
}

if !hasWorked {
select {
case <-hasWorked:
// continue
case <-ctx.Done():
return "", fmt.Errorf("couldn't connect to any relay")
}

Expand Down

0 comments on commit 5edb54e

Please sign in to comment.