Skip to content
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

Pattern for hedged requests #4

Merged
merged 6 commits into from
May 6, 2024
Merged

Pattern for hedged requests #4

merged 6 commits into from
May 6, 2024

Conversation

annirudh
Copy link
Contributor

@annirudh annirudh commented May 6, 2024

No description provided.

@annirudh annirudh merged commit 82cc1cd into main May 6, 2024
7 checks passed
responses := make(chan T) // unbuffered, we only expect one response
ctx, cancel := context.WithCancelCause(ctx)

group := ErrGroup(Limited(ctx, cfg.numHedgedRequests+1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

because we don't send an unbounded number of tasks to the group, it can be Unlimited for cheaper. A Limited group with N max parallelism that only ever receives N tasks doesn't do anything different.

}

go func() {
_ = group.Wait()
Copy link
Collaborator

Choose a reason for hiding this comment

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

putting group.Wait() here in a bare, unmanaged goroutine will hard-crash the entire process if any panic happens in any of the worker goroutines.

rather than spawning a loose goroutine to Wait(), it is much more preferable to write the result value to a captured var here using a sync.Once, calling .Wait() here in the function, checking its error, and then managing what (if anything) was put into that return value var.


res, err := requester(ctx)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

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

is the plan to return the first-returned value-or-error from any attempted request? To return any successful value if any had no error? to return a value if we got one but include any error that occurred in any of the other attempts, including context canceled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

First return value or error from any attempted request

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.

2 participants