From 7aff9aac82b8ffde088c081556fe3bff782d8c1d Mon Sep 17 00:00:00 2001 From: Skyxim Date: Wed, 18 May 2022 22:29:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20sticky-sessions=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/outboundgroup/loadbalance.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index 9cc873772..9ca0a597a 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -152,12 +152,14 @@ func strategyConsistentHashing() strategyFn { func strategyStickySessions() strategyFn { ttl := time.Minute * 10 - c := cache.New[uint64, int](1 * time.Second) + lruCache := cache.NewLRUCache[uint64, int]( + cache.WithAge[uint64, int](int64(ttl.Seconds())), + cache.WithSize[uint64, int](1000)) return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy { key := uint64(murmur3.Sum32([]byte(getKeyWithSrcAndDst(metadata)))) length := len(proxies) - idx, expireTime := c.GetWithExpire(key) - if expireTime.IsZero() { + idx, has := lruCache.Get(key) + if !has { idx = int(jumpHash(key+uint64(time.Now().UnixMilli()), int32(length))) } @@ -166,8 +168,8 @@ func strategyStickySessions() strategyFn { proxy := proxies[nowIdx] if proxy.Alive() { if nowIdx != idx { - c.Put(key, idx, -1) - c.Put(key, nowIdx, ttl) + lruCache.Delete(key) + lruCache.Set(key, nowIdx) } return proxy