-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
responses := make(chan T) // unbuffered, we only expect one response | ||
ctx, cancel := context.WithCancelCause(ctx) | ||
|
||
group := ErrGroup(Limited(ctx, cfg.numHedgedRequests+1)) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
No description provided.