pickfirst sets ShuffleAddressListForTesting to use rand.Shuffle in its init function
pickfirst also directly uses ShuffleAddressListForTesting in UpdateClientConnState to get the actual shuffle function
So, for someone reading the code, it can be a little misleading as to why pickfirst is getting the shuffle func from a var with a ForTesting suffix.
The suggestion from the comment is to instead do something like:
package pickfirst
var shuffleFunc func(n int, swap func(i, j int))
func init() {
shuffleFunc = rand.Shuffle
internal.ShuffleAddressListForTesting = func(sf func(n int, swap func(i, j int))) {
shuffleFunc = sf
}
}
func (...) UpdateClientConnState(...) {
// Directly use `shuffleFunc` to do the shuffling
}
Note that I've changed the signature of internal.ShuffleAddressListForTesting to accept a func. We could rename it to something like internal.PickfirstShuffleFunc. Also, note that we could completely move this var to the pickfirst package and have our tests directly use that, but would pollute the external API.
Based on the PR review comment https://github.com/grpc/grpc-go/pull/7498#discussion_r1718899646,
pickfirst
setsShuffleAddressListForTesting
to userand.Shuffle
in its init functionpickfirst
also directly usesShuffleAddressListForTesting
inUpdateClientConnState
to get the actual shuffle functionSo, for someone reading the code, it can be a little misleading as to why
pickfirst
is getting the shuffle func from a var with aForTesting
suffix.The suggestion from the comment is to instead do something like: