proxy: use buffered channels and only let one subrequest write a result #242
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.
Fixes #235
Once we’re in a “hedged” case, we have two goroutines running, and this select reading from both response channels for answers:
Once we get an answer from one of the two, the hedger unblocks and cancels the context that is shared by both subrequest goroutines.
This is what a subrequest looks like for reference:
So if there are two of these running, the context is cancelled once the fastest one returns and the hedger func exits, and there’s nothing to read from the “slow” channel, so the subrequest goroutine hangs on send.
There are two parts to the fix:
sync.Once
to avoid this problemThe tests were updated to detect the leak (and confirm it no longer happens)