fix: sticky-sessions异常

This commit is contained in:
Skyxim 2022-05-18 22:29:27 +08:00
parent 8b09db5f7f
commit 7aff9aac82

View File

@ -152,12 +152,14 @@ func strategyConsistentHashing() strategyFn {
func strategyStickySessions() strategyFn { func strategyStickySessions() strategyFn {
ttl := time.Minute * 10 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 { return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy {
key := uint64(murmur3.Sum32([]byte(getKeyWithSrcAndDst(metadata)))) key := uint64(murmur3.Sum32([]byte(getKeyWithSrcAndDst(metadata))))
length := len(proxies) length := len(proxies)
idx, expireTime := c.GetWithExpire(key) idx, has := lruCache.Get(key)
if expireTime.IsZero() { if !has {
idx = int(jumpHash(key+uint64(time.Now().UnixMilli()), int32(length))) idx = int(jumpHash(key+uint64(time.Now().UnixMilli()), int32(length)))
} }
@ -166,8 +168,8 @@ func strategyStickySessions() strategyFn {
proxy := proxies[nowIdx] proxy := proxies[nowIdx]
if proxy.Alive() { if proxy.Alive() {
if nowIdx != idx { if nowIdx != idx {
c.Put(key, idx, -1) lruCache.Delete(key)
c.Put(key, nowIdx, ttl) lruCache.Set(key, nowIdx)
} }
return proxy return proxy