mirror of
https://github.com/aykhans/dodo.git
synced 2025-07-02 00:16:20 +00:00
🔨 Refactor client and request generation to use random value cycling
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user