diff --git a/requests/client.go b/requests/client.go index 9ea3110..52fef8d 100644 --- a/requests/client.go +++ b/requests/client.go @@ -279,6 +279,7 @@ func getDialFunc(proxy *config.Proxy, timeout time.Duration) (fasthttp.DialFunc, // getSharedClientFuncMultiple returns a ClientGeneratorFunc that cycles through a list of fasthttp.HostClient instances. // The function uses a local random number generator to determine the starting index and stop index for cycling through the clients. +// The returned function isn't thread-safe and should be used in a single-threaded context. func getSharedClientFuncMultiple(clients []*fasthttp.HostClient, localRand *rand.Rand) ClientGeneratorFunc { return utils.RandomValueCycle(clients, localRand) } diff --git a/utils/slice.go b/utils/slice.go index 32b6283..e323663 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -19,6 +19,14 @@ func Contains[T comparable](slice []T, item T) bool { return false } +// RandomValueCycle returns a function that cycles through the provided slice of values +// in a random order. Each call to the returned function will yield a value from the slice. +// The order of values is determined by the provided random number generator. +// +// The returned function will cycle through the values in a random order until all values +// have been returned at least once. After all values have been returned, the function will +// reset and start cycling through the values in a random order again. +// The returned function isn't thread-safe and should be used in a single-threaded context. func RandomValueCycle[Value any](values []Value, localRand *rand.Rand) func() Value { var ( clientsCount int = len(values)