Skip to content

Commit

Permalink
refactor: 修改sticky-session尝试逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyxim committed May 19, 2022
1 parent c787bbe commit fe25ae8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions adapter/outboundgroup/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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]
}
}
Expand Down

0 comments on commit fe25ae8

Please sign in to comment.