feat(ai-load-balancer): enhance global least request load balancer (#3255)

This commit is contained in:
nixidexiangjiao
2025-12-29 09:28:56 +08:00
committed by GitHub
parent 9c11c5406f
commit b98b51ef06
3 changed files with 401 additions and 25 deletions

View File

@@ -0,0 +1,24 @@
package global_least_request
import (
"fmt"
"github.com/higress-group/wasm-go/pkg/wrapper"
)
func (lb GlobalLeastRequestLoadBalancer) checkRateLimit(hostSelected string, currentCount int64, ctx wrapper.HttpContext, routeName string, clusterName string) bool {
// 如果没有配置最大请求数,直接通过
if lb.maxRequestCount <= 0 {
return true
}
// 如果当前请求数大于最大请求数,则限流
// 注意Lua脚本已经加了1所以这里比较的是加1后的值
if currentCount > lb.maxRequestCount {
// 恢复 Redis 计数
lb.redisClient.HIncrBy(fmt.Sprintf(RedisKeyFormat, routeName, clusterName), hostSelected, -1, nil)
return false
}
return true
}