rpcclient: add context.Context support for HTTP POST mode#2506
Open
seeforschauer wants to merge 5 commits into
Open
rpcclient: add context.Context support for HTTP POST mode#2506seeforschauer wants to merge 5 commits into
seeforschauer wants to merge 5 commits into
Conversation
Add SendCmdWithContext and GetBlockCountWithContext methods that propagate a context.Context to the underlying HTTP request. This allows callers to cancel or timeout individual RPC calls. Changes: - Add ctx field to jsonRequest struct - Use http.NewRequestWithContext in handleSendPostMessage - Respect context cancellation during retry backoff - Add SendCmdWithContext to Client - Add GetBlockCountWithContext as the first context-aware method - Existing methods are unchanged (non-breaking) Fixes btcsuite#2499
7 tasks
Roasbeef
requested changes
Mar 24, 2026
Refactor SendCmd to delegate to SendCmdWithContext instead of duplicating the marshalling logic. SendCmdWithContext is now the single implementation; SendCmd passes context.Background(). Add a context error check after httpClient.Do in the retry loop so a cancelled context bails out immediately instead of retrying. Use camelCase for test function names.
|
Diff looks good, tests passing. Nice work @seeforschauer! |
Author
|
@Roasbeef all four items addressed — mind taking another look when you get a chance? |
Collaborator
kcalvinalvin
left a comment
There was a problem hiding this comment.
Looks good overall. Some minor test related issues that I saw
Author
|
@kcalvinalvin pls review |
…-support # Conflicts: # rpcclient/infrastructure.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
Add
context.Contextsupport to rpcclient's HTTP POST mode, allowing callers to cancel or timeout individual RPC calls. Fixes #2499.Currently, rpcclient methods like
GetBlockCount()don't accept a context, which means callers have no way to cancel or set deadlines on individual RPC calls. In HTTP POST mode,handleSendPostMessageuseshttp.NewRequestwithout context, and the 10-retry loop doesn't respect external cancellation.Changes
ctxfield tojsonRequeststructhttp.NewRequestWithContextinhandleSendPostMessagewhen ctx is setselectonreqCtx.Done())SendCmdWithContext(ctx, cmd)toClientGetBlockCountWithContext(ctx)as the first context-aware method variantDesign decisions
SendCmdWithContextmirrorsSendCmdwith an added ctx parameterctxis nil (e.g., from existingSendCmdcallers),context.Background()is used as fallback*WithContextmethods for other RPC calls can be added incrementally in follow-up PRsSteps to Test
Pull Request Checklist
Testing
Code Style and Documentation