make context work

This commit is contained in:
1l0 2024-09-24 15:08:27 +09:00 committed by fiatjaf_
parent 23ddac33dd
commit 39f7a99894

View File

@ -76,7 +76,6 @@ func ConnectBunker(
pool,
onAuth,
)
_, err = bunker.RPC(ctx, "connect", []string{targetPublicKey, secret})
return bunker, err
}
@ -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 {
@ -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
}