🔨 Refactor client and request generation to use random value cycling

This commit is contained in:
2024-09-15 17:34:24 +04:00
parent de5a5dc96e
commit 2f37f8c2c1
3 changed files with 28 additions and 44 deletions

View File

@ -1,5 +1,7 @@
package utils
import "math/rand"
func Flatten[T any](nested [][]*T) []*T {
flattened := make([]*T, 0)
for _, n := range nested {
@ -16,3 +18,25 @@ func Contains[T comparable](slice []T, item T) bool {
}
return false
}
func RandomValueCycle[Value any](values []Value, localRand *rand.Rand) func() Value {
var (
clientsCount int = len(values)
currentIndex int = localRand.Intn(clientsCount)
stopIndex int = currentIndex
)
return func() Value {
client := values[currentIndex]
currentIndex++
if currentIndex == clientsCount {
currentIndex = 0
}
if currentIndex == stopIndex {
currentIndex = localRand.Intn(clientsCount)
stopIndex = currentIndex
}
return client
}
}