mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-20 11:11:26 +00:00
🔨 Select client with random indexes
This commit is contained in:
parent
891f1f1333
commit
de5a5dc96e
@ -3,6 +3,7 @@ package requests
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -276,24 +277,22 @@ func getDialFunc(proxy *config.Proxy, timeout time.Duration) (fasthttp.DialFunc,
|
|||||||
return dialer, nil
|
return dialer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSharedClientFuncMultiple returns a ClientGeneratorFunc that cycles through
|
// getSharedClientFuncMultiple returns a ClientGeneratorFunc that cycles through a list of fasthttp.HostClient instances.
|
||||||
// a provided list of fasthttp.HostClient instances. Each call to the returned
|
// The function uses a local random number generator to determine the starting index and stop index for cycling through the clients.
|
||||||
// function will return the next client in the list, cycling back to the first
|
func getSharedClientFuncMultiple(clients []*fasthttp.HostClient, localRand *rand.Rand) ClientGeneratorFunc {
|
||||||
// client after reaching the end of the slice.
|
|
||||||
//
|
|
||||||
// The returned function isn't thread-safe and should be used in a single-threaded context.
|
|
||||||
func getSharedClientFuncMultiple(clients []*fasthttp.HostClient) ClientGeneratorFunc {
|
|
||||||
var (
|
var (
|
||||||
currentIndex int = 0
|
|
||||||
clientsCount int = len(clients)
|
clientsCount int = len(clients)
|
||||||
|
currentIndex int = localRand.Intn(clientsCount)
|
||||||
|
stopIndex int = clientsCount
|
||||||
)
|
)
|
||||||
|
|
||||||
return func() *fasthttp.HostClient {
|
return func() *fasthttp.HostClient {
|
||||||
client := clients[currentIndex]
|
client := clients[currentIndex%clientsCount]
|
||||||
if currentIndex == clientsCount-1 {
|
if currentIndex == stopIndex {
|
||||||
currentIndex = 0
|
currentIndex = localRand.Intn(clientsCount)
|
||||||
|
stopIndex = currentIndex - 1
|
||||||
} else {
|
} else {
|
||||||
currentIndex++
|
currentIndex = (currentIndex + 1) % clientsCount
|
||||||
}
|
}
|
||||||
|
|
||||||
return client
|
return client
|
||||||
|
@ -69,7 +69,7 @@ func newRequest(
|
|||||||
if clientsCount == 1 {
|
if clientsCount == 1 {
|
||||||
getClient = getSharedClientFuncSingle(clients[0])
|
getClient = getSharedClientFuncSingle(clients[0])
|
||||||
} else {
|
} else {
|
||||||
getClient = getSharedClientFuncMultiple(clients)
|
getClient = getSharedClientFuncMultiple(clients, localRand)
|
||||||
}
|
}
|
||||||
|
|
||||||
getRequest := getRequestGeneratorFunc(
|
getRequest := getRequestGeneratorFunc(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user