refactor: 使用标准库 slices/maps 替代自定义函数

- 使用 slices.Contains 替代 contains/containsInt 函数
- 使用 maps.Copy 替代手动遍历复制
- 删除 internal/cache 中不再需要的辅助函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-13 13:15:13 +08:00
parent 78c49f7288
commit 95b6119e34
5 changed files with 14 additions and 64 deletions

View File

@ -19,6 +19,7 @@ package cache
import (
"container/list"
"slices"
"sync"
"time"
)
@ -405,12 +406,12 @@ func (c *ProxyCache) MatchRule(path, method string, status int) *ProxyCacheRule
}
// 检查方法
if len(rule.Methods) > 0 && !contains(rule.Methods, method) {
if len(rule.Methods) > 0 && !slices.Contains(rule.Methods, method) {
continue
}
// 检查状态码
if len(rule.Statuses) > 0 && !containsInt(rule.Statuses, status) {
if len(rule.Statuses) > 0 && !slices.Contains(rule.Statuses, status) {
continue
}
@ -419,40 +420,6 @@ func (c *ProxyCache) MatchRule(path, method string, status int) *ProxyCacheRule
return nil
}
// contains 检查字符串切片是否包含某值。
//
// 参数:
// - slice: 字符串切片
// - val: 待查找的值
//
// 返回值:
// - bool: true 表示包含false 表示不包含
func contains(slice []string, val string) bool {
for _, s := range slice {
if s == val {
return true
}
}
return false
}
// containsInt 检查整数切片是否包含某值。
//
// 参数:
// - slice: 整数切片
// - val: 待查找的值
//
// 返回值:
// - bool: true 表示包含false 表示不包含
func containsInt(slice []int, val int) bool {
for _, i := range slice {
if i == val {
return true
}
}
return false
}
// Delete 删除缓存条目。
func (c *ProxyCache) Delete(hashKey uint64) {
c.mu.Lock()

View File

@ -14,6 +14,8 @@
// 作者xfy
package loadbalance
import "slices"
// ValidAlgorithms 是支持的负载均衡算法列表。
var ValidAlgorithms = []string{
"round_robin",
@ -28,10 +30,5 @@ func IsValidAlgorithm(alg string) bool {
if alg == "" {
return true
}
for _, a := range ValidAlgorithms {
if a == alg {
return true
}
}
return false
return slices.Contains(ValidAlgorithms, alg)
}

View File

@ -36,6 +36,7 @@ import (
"errors"
"fmt"
"hash/fnv"
"slices"
"strings"
"sync"
"sync/atomic"
@ -465,13 +466,7 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
statusCode := ctx.Response.StatusCode()
upstreamStatus = statusCode
shouldRetry := false
for _, code := range httpCodes {
if statusCode == code {
shouldRetry = true
break
}
}
shouldRetry := slices.Contains(httpCodes, statusCode)
if shouldRetry {
// 释放缓存锁

View File

@ -43,6 +43,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"sync"
"rua.plus/lolly/internal/config"
@ -692,12 +693,7 @@ func isInsecureCipher(id uint16) bool {
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
}
for _, insecure := range insecureCiphers {
if id == insecure {
return true
}
}
return false
return slices.Contains(insecureCiphers, id)
}
// defaultCipherSuites 返回 TLS 1.2 推荐的加密套件。

View File

@ -18,6 +18,7 @@
package variable
import (
"maps"
"strconv"
"strings"
"sync"
@ -78,9 +79,7 @@ func SetGlobalVariables(vars map[string]string) {
globalVariablesLock.Lock()
defer globalVariablesLock.Unlock()
globalVariables = make(map[string]string, len(vars))
for k, v := range vars {
globalVariables[k] = v
}
maps.Copy(globalVariables, vars)
}
// GetGlobalVariable 获取全局变量值。
@ -104,9 +103,7 @@ func GetAllGlobalVariables() map[string]string {
}
// 返回副本,避免外部修改影响全局存储
result := make(map[string]string, len(globalVariables))
for k, v := range globalVariables {
result[k] = v
}
maps.Copy(result, globalVariables)
return result
}
@ -146,9 +143,7 @@ func NewContext(ctx *fasthttp.RequestCtx) *Context {
}
// 注入全局变量
globals := GetAllGlobalVariables()
for name, value := range globals {
vc.store[name] = value
}
maps.Copy(vc.store, globals)
return vc
}