Skip to content

query: add OnMaxTries #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kcalvinalvin
Copy link

@kcalvinalvin kcalvinalvin commented Feb 17, 2025

OnMaxTries allows an outside caller to dictate what should happen to a given worker in case of a timeout.
This allows for peer selection strategies to be made outside of the package, allowing for greater flexibility.

EDIT:
This PR is a dependency for btcsuite/btcd#2226

OnMaxTries config lets the caller choose what happens when a query goes
over the maximum allowed tries. This config helps meet the needs of
different callers as some may choose to immediately disconnect vs others
that may choose to try again.
The peer may already be disconnected but the go runtime could have
chosen to handle the message. To ensure that the handleResponse doesn't
get called when the peer is already disconnected, add a select before
handleResponse.
@kcalvinalvin kcalvinalvin marked this pull request as ready for review April 21, 2025 15:33
@kcalvinalvin kcalvinalvin marked this pull request as draft April 21, 2025 16:14
@kcalvinalvin kcalvinalvin marked this pull request as ready for review May 12, 2025 15:55
@saubyk
Copy link

saubyk commented May 13, 2025

cc: @mohamedawnallah for review

@@ -94,6 +94,10 @@ type Config struct {
// make this configurable to easily mock the worker used during tests.
NewWorker func(Peer) Worker

// OnMaxTries is function closure that's called on max retries on
// workers.
OnMaxTries func(string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we should instead use a stricter type here (net.Addr) and mention what is being passed to this call-back so that the caller doesnt have to go look at the code to figure out what is being passed to the call-back it is setting.

The commit message isnt very accurate imo. OnMaxTries config lets the caller choose what happens when a query goes over the maximum allowed tries isnt really true - more so: OnMaxTries gives the caller access to the peer's address once a maxmum number of retries have been attempted" or something like that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's worth linking the PR that makes use of this new config option in the PR description so that reviewers can understand the context a bit more

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we should instead use a stricter type here (net.Addr) and mention what is being passed to this call-back so that the caller doesnt have to go look at the code to figure out what is being passed to the call-back it is setting.

The commit message isnt very accurate imo. OnMaxTries config lets the caller choose what happens when a query goes over the maximum allowed tries isnt really true - more so: OnMaxTries gives the caller access to the peer's address once a maxmum number of retries have been attempted" or something like that

Will address both points.

Comment on lines +164 to +173
select {
// If the peer disconnects before giving us a valid
// answer, we'll also exit with an error.
case <-peer.OnDisconnect():
log.Debugf("Peer %v for worker disconnected, "+
"cancelling job %v", peer.Addr(),
job.Index())

jobErr = ErrPeerDisconnected
break Loop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this adds anything. This select is already covered in below and so if peer.OnDisconnect() triggers before the message is received on the msgChan, we will already catch the case below.

I think what you instead want is for job.HandleResp's function to know if it can exit or not.

The idiomatic way of doing this would be to pass it a ctx context.Context which gets cancelled when the peer disconnects. An intermediate step can just be to pass it a quit chan struct{} which you can derive from the peers OnDisconnect method and then listen on that in the HandleResp call-back

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the reason for adding this is because I saw that there are cases where case resp := <- msgChan: gets triggered first before the below case <-peer.OnDisconnect(): gets triggered.

I think what you instead want is for job.HandleResp's function to know if it can exit or not.

Yeah that would also solve the problem I was seeing.

An intermediate step can just be to pass it a quit chan struct{} which you can derive from the peers OnDisconnect method

That would be ok as well except now we force every implementation of HandleResp to handle the case where quit chan struct{} is passed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'll also add that I don't really have a preference on how that's handled so I'll push changes so that HandleResp takes a quti chan struct{} as an argument.

@lightninglabs-deploy
Copy link

@kcalvinalvin, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants