- 使用 slices.Contains 替代 contains/containsInt 函数 - 使用 maps.Copy 替代手动遍历复制 - 删除 internal/cache 中不再需要的辅助函数 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
35 lines
888 B
Go
35 lines
888 B
Go
// Package loadbalance 负载均衡包,提供多种负载均衡算法实现。
|
||
//
|
||
// 该文件包含负载均衡相关的核心逻辑,包括:
|
||
// - 轮询算法(Round Robin)
|
||
// - 加权轮询算法(Weighted Round Robin)
|
||
// - 最少连接算法(Least Connections)
|
||
// - IP 哈希算法(IP Hash)
|
||
// - 一致性哈希算法(Consistent Hash)
|
||
//
|
||
// 主要用途:
|
||
//
|
||
// 用于在后端服务器之间分发请求,提高服务可用性和性能。
|
||
//
|
||
// 作者:xfy
|
||
package loadbalance
|
||
|
||
import "slices"
|
||
|
||
// ValidAlgorithms 是支持的负载均衡算法列表。
|
||
var ValidAlgorithms = []string{
|
||
"round_robin",
|
||
"weighted_round_robin",
|
||
"least_conn",
|
||
"ip_hash",
|
||
"consistent_hash",
|
||
}
|
||
|
||
// IsValidAlgorithm 检查算法是否有效。
|
||
func IsValidAlgorithm(alg string) bool {
|
||
if alg == "" {
|
||
return true
|
||
}
|
||
return slices.Contains(ValidAlgorithms, alg)
|
||
}
|