refactor: 修改sticky-session尝试逻辑
This commit is contained in:
parent
d3e20a87de
commit
db2bf61714
1 changed files with 8 additions and 4 deletions
|
@ -151,7 +151,7 @@ func strategyConsistentHashing() strategyFn {
|
||||||
|
|
||||||
func strategyStickySessions() strategyFn {
|
func strategyStickySessions() strategyFn {
|
||||||
ttl := time.Minute * 10
|
ttl := time.Minute * 10
|
||||||
|
maxRetry := 5
|
||||||
lruCache := cache.NewLRUCache[uint64, int](
|
lruCache := cache.NewLRUCache[uint64, int](
|
||||||
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
||||||
cache.WithSize[uint64, int](1000))
|
cache.WithSize[uint64, int](1000))
|
||||||
|
@ -160,11 +160,11 @@ func strategyStickySessions() strategyFn {
|
||||||
length := len(proxies)
|
length := len(proxies)
|
||||||
idx, has := lruCache.Get(key)
|
idx, has := lruCache.Get(key)
|
||||||
if !has {
|
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
|
||||||
nowIdx := (idx + 1) % length
|
for i := 1; i < maxRetry; i++ {
|
||||||
proxy := proxies[nowIdx]
|
proxy := proxies[nowIdx]
|
||||||
if proxy.Alive() {
|
if proxy.Alive() {
|
||||||
if nowIdx != idx {
|
if nowIdx != idx {
|
||||||
|
@ -173,9 +173,13 @@ func strategyStickySessions() strategyFn {
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxy
|
return proxy
|
||||||
|
} else {
|
||||||
|
nowIdx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lruCache.Delete(key)
|
||||||
|
lruCache.Set(key, 0)
|
||||||
return proxies[0]
|
return proxies[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue