🔨 Refactor client and request generation to use random value cycling

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

View File

@ -280,23 +280,7 @@ func getDialFunc(proxy *config.Proxy, timeout time.Duration) (fasthttp.DialFunc,
// getSharedClientFuncMultiple returns a ClientGeneratorFunc that cycles through a list of fasthttp.HostClient instances. // 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 function uses a local random number generator to determine the starting index and stop index for cycling through the clients.
func getSharedClientFuncMultiple(clients []*fasthttp.HostClient, localRand *rand.Rand) ClientGeneratorFunc { func getSharedClientFuncMultiple(clients []*fasthttp.HostClient, localRand *rand.Rand) ClientGeneratorFunc {
var ( return utils.RandomValueCycle(clients, localRand)
clientsCount int = len(clients)
currentIndex int = localRand.Intn(clientsCount)
stopIndex int = clientsCount
)
return func() *fasthttp.HostClient {
client := clients[currentIndex%clientsCount]
if currentIndex == stopIndex {
currentIndex = localRand.Intn(clientsCount)
stopIndex = currentIndex - 1
} else {
currentIndex = (currentIndex + 1) % clientsCount
}
return client
}
} }
// getSharedClientFuncSingle returns a ClientGeneratorFunc that always returns the provided fasthttp.HostClient instance. // getSharedClientFuncSingle returns a ClientGeneratorFunc that always returns the provided fasthttp.HostClient instance.

View File

@ -8,6 +8,7 @@ import (
"github.com/aykhans/dodo/config" "github.com/aykhans/dodo/config"
customerrors "github.com/aykhans/dodo/custom_errors" customerrors "github.com/aykhans/dodo/custom_errors"
"github.com/aykhans/dodo/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
@ -108,19 +109,7 @@ func getRequestGeneratorFunc(
if bodiesLen == 1 { if bodiesLen == 1 {
getBody = func() string { return Bodies[0] } getBody = func() string { return Bodies[0] }
} else if bodiesLen > 1 { } else if bodiesLen > 1 {
currentIndex := localRand.Intn(bodiesLen) getBody = utils.RandomValueCycle(Bodies, localRand)
stopIndex := bodiesLen - 1
getBody = func() string {
body := Bodies[currentIndex%bodiesLen]
if currentIndex == stopIndex {
currentIndex = localRand.Intn(bodiesLen)
stopIndex = currentIndex - 1
} else {
currentIndex = (currentIndex + 1) % bodiesLen
}
return body
}
} }
getHeaders := getKeyValueSetFunc(Headers, localRand) getHeaders := getKeyValueSetFunc(Headers, localRand)
getCookies := getKeyValueSetFunc(Cookies, localRand) getCookies := getKeyValueSetFunc(Cookies, localRand)
@ -226,20 +215,7 @@ func getKeyValueSetFunc[
if valuesLen == 1 { if valuesLen == 1 {
getKeyValue = func() string { return values[0] } getKeyValue = func() string { return values[0] }
} else if valuesLen > 1 { } else if valuesLen > 1 {
currentIndex := localRand.Intn(valuesLen) getKeyValue = utils.RandomValueCycle(values, localRand)
stopIndex := valuesLen - 1
getKeyValue = func() string {
value := values[currentIndex%valuesLen]
if currentIndex == stopIndex {
currentIndex = localRand.Intn(valuesLen)
stopIndex = currentIndex - 1
} else {
currentIndex = (currentIndex + 1) % valuesLen
}
return value
}
isRandom = true isRandom = true
} }

View File

@ -1,5 +1,7 @@
package utils package utils
import "math/rand"
func Flatten[T any](nested [][]*T) []*T { func Flatten[T any](nested [][]*T) []*T {
flattened := make([]*T, 0) flattened := make([]*T, 0)
for _, n := range nested { for _, n := range nested {
@ -16,3 +18,25 @@ func Contains[T comparable](slice []T, item T) bool {
} }
return false 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
}
}