Skip to content

Commit

Permalink
make context work
Browse files Browse the repository at this point in the history
  • Loading branch information
1l0 authored and fiatjaf committed Sep 24, 2024
1 parent 23ddac3 commit 39f7a99
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions nip46/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func ConnectBunker(
pool,
onAuth,
)

_, err = bunker.RPC(ctx, "connect", []string{targetPublicKey, secret})
return bunker, err
}
Expand Down Expand Up @@ -237,6 +236,10 @@ func (bunker *BunkerClient) RPC(ctx context.Context, method string, params []str

respWaiter := make(chan Response)
bunker.listeners.Store(id, respWaiter)
defer func() {
bunker.listeners.Delete(id)
close(respWaiter)
}()
hasWorked := make(chan struct{})

for _, url := range bunker.relays {
Expand All @@ -259,10 +262,13 @@ func (bunker *BunkerClient) RPC(ctx context.Context, method string, params []str
return "", fmt.Errorf("couldn't connect to any relay")
}

resp := <-respWaiter
if resp.Error != "" {
return "", fmt.Errorf("response error: %s", resp.Error)
select {
case <-ctx.Done():
return "", fmt.Errorf("context canceled")
case resp := <-respWaiter:
if resp.Error != "" {
return "", fmt.Errorf("response error: %s", resp.Error)
}
return resp.Result, nil
}

return resp.Result, nil
}

0 comments on commit 39f7a99

Please sign in to comment.