From c711aa25119b50c1395905d2c3ee0d6a1c408ff8 Mon Sep 17 00:00:00 2001 From: "shenhui.backend" Date: Tue, 18 Jun 2019 21:13:32 +0800 Subject: [PATCH] add random load balance for query authds --- internal/auth/authorizations.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/auth/authorizations.go b/internal/auth/authorizations.go index 10dbbe8c6..7ce826ee4 100644 --- a/internal/auth/authorizations.go +++ b/internal/auth/authorizations.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "math/rand" "net/url" "regexp" "time" @@ -78,7 +79,10 @@ func (a *State) IsExpired() bool { func QueryAnyAuthd(authd []string, remoteIP string, tlsEnabled bool, commonName string, authSecret string, connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) { - for _, a := range authd { + start := rand.Int() + n := len(authd) + for i := 0; i < n; i++ { + a := authd[(i+start)%n] authState, err := QueryAuthd(a, remoteIP, tlsEnabled, commonName, authSecret, connectTimeout, requestTimeout) if err != nil { log.Printf("Error: failed auth against %s %s", a, err)