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.
Description
This PR changes the implementation of
Distribute()
to use a dynamically generated select via the reflect package, instead of randomly choosing a single channel to send to.In the case where all "out" channels are available to receive a message, the behavior is the same: the channel is selected pseudo-randomly. However, the pseudo-random choice is made inside the Go library's
select
implementation, which is presumably highly optimized for this purpose.Benchmark_Distribute-8
performance is roughly doubled on my system (831 ns/op after the change vs. 1582 ns/op before).In the case where some of the "out" channels are not able to receive a message right now, the
select
call will immediately choose one of the available channels. IfDistribute()
is being used to send units of work to a pool of workers, we avoid the case where some workers sit idle because we happened to choose a busy worker to receive the next unit.No changes are made to the library API and this should be fully backwards compatible with all library users, unless they rely on the exact details of the channel selection (and it's hard to see how one would do this).