From 2f37f8c2c172b6b74ad8b837812d338e9872d744 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sun, 15 Sep 2024 17:34:24 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Refactor=20client=20and=20reques?= =?UTF-8?q?t=20generation=20to=20use=20random=20value=20cycling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requests/client.go | 18 +----------------- requests/request.go | 30 +++--------------------------- utils/slice.go | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 44 deletions(-) diff --git a/requests/client.go b/requests/client.go index fc5c75f..9ea3110 100644 --- a/requests/client.go +++ b/requests/client.go @@ -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. // 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 { - var ( - 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 - } + return utils.RandomValueCycle(clients, localRand) } // getSharedClientFuncSingle returns a ClientGeneratorFunc that always returns the provided fasthttp.HostClient instance. diff --git a/requests/request.go b/requests/request.go index b36b449..29ca89c 100644 --- a/requests/request.go +++ b/requests/request.go @@ -8,6 +8,7 @@ import ( "github.com/aykhans/dodo/config" customerrors "github.com/aykhans/dodo/custom_errors" + "github.com/aykhans/dodo/utils" "github.com/valyala/fasthttp" ) @@ -108,19 +109,7 @@ func getRequestGeneratorFunc( if bodiesLen == 1 { getBody = func() string { return Bodies[0] } } else if bodiesLen > 1 { - currentIndex := localRand.Intn(bodiesLen) - 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 - } + getBody = utils.RandomValueCycle(Bodies, localRand) } getHeaders := getKeyValueSetFunc(Headers, localRand) getCookies := getKeyValueSetFunc(Cookies, localRand) @@ -226,20 +215,7 @@ func getKeyValueSetFunc[ if valuesLen == 1 { getKeyValue = func() string { return values[0] } } else if valuesLen > 1 { - currentIndex := localRand.Intn(valuesLen) - 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 - } - + getKeyValue = utils.RandomValueCycle(values, localRand) isRandom = true } diff --git a/utils/slice.go b/utils/slice.go index cf2afd1..32b6283 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -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 + } +}