From fe25ae83df4943da65403d37c257a3ee8a5fb58b Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 19 May 2022 20:27:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9sticky-session?= =?UTF-8?q?=E5=B0=9D=E8=AF=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/outboundgroup/loadbalance.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index 9ca0a597..5b962983 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -151,7 +151,7 @@ func strategyConsistentHashing() strategyFn { func strategyStickySessions() strategyFn { ttl := time.Minute * 10 - + maxRetry := 5 lruCache := cache.NewLRUCache[uint64, int]( cache.WithAge[uint64, int](int64(ttl.Seconds())), cache.WithSize[uint64, int](1000)) @@ -160,11 +160,11 @@ func strategyStickySessions() strategyFn { length := len(proxies) idx, has := lruCache.Get(key) if !has { - idx = int(jumpHash(key+uint64(time.Now().UnixMilli()), int32(length))) + idx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length))) } - for i := 0; i < length; i++ { - nowIdx := (idx + 1) % length + nowIdx := idx + for i := 1; i < maxRetry; i++ { proxy := proxies[nowIdx] if proxy.Alive() { if nowIdx != idx { @@ -173,9 +173,13 @@ func strategyStickySessions() strategyFn { } return proxy + } else { + nowIdx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length))) } } + lruCache.Delete(key) + lruCache.Set(key, 0) return proxies[0] } }