From 95b6119e344d15795606f5ef4b1a7f3a994e901c Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 13 Apr 2026 13:15:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=BA=93=20slices/maps=20=E6=9B=BF=E4=BB=A3=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 slices.Contains 替代 contains/containsInt 函数 - 使用 maps.Copy 替代手动遍历复制 - 删除 internal/cache 中不再需要的辅助函数 Co-Authored-By: Claude Opus 4.6 --- internal/cache/file_cache.go | 39 +++--------------------------- internal/loadbalance/algorithms.go | 9 +++---- internal/proxy/proxy.go | 9 ++----- internal/ssl/ssl.go | 8 ++---- internal/variable/variable.go | 13 +++------- 5 files changed, 14 insertions(+), 64 deletions(-) diff --git a/internal/cache/file_cache.go b/internal/cache/file_cache.go index 6343f80..4ab4386 100644 --- a/internal/cache/file_cache.go +++ b/internal/cache/file_cache.go @@ -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() diff --git a/internal/loadbalance/algorithms.go b/internal/loadbalance/algorithms.go index aefa5c7..1c875b4 100644 --- a/internal/loadbalance/algorithms.go +++ b/internal/loadbalance/algorithms.go @@ -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) } diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index ea5243f..5193bed 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -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 { // 释放缓存锁 diff --git a/internal/ssl/ssl.go b/internal/ssl/ssl.go index 894e00e..ddf97c9 100644 --- a/internal/ssl/ssl.go +++ b/internal/ssl/ssl.go @@ -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 推荐的加密套件。 diff --git a/internal/variable/variable.go b/internal/variable/variable.go index 88c0b94..9b2c63d 100644 --- a/internal/variable/variable.go +++ b/internal/variable/variable.go @@ -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 }