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:
parent
78c49f7288
commit
95b6119e34
39
internal/cache/file_cache.go
vendored
39
internal/cache/file_cache.go
vendored
@ -19,6 +19,7 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查状态码
|
// 检查状态码
|
||||||
if len(rule.Statuses) > 0 && !containsInt(rule.Statuses, status) {
|
if len(rule.Statuses) > 0 && !slices.Contains(rule.Statuses, status) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,40 +420,6 @@ func (c *ProxyCache) MatchRule(path, method string, status int) *ProxyCacheRule
|
|||||||
return nil
|
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 删除缓存条目。
|
// Delete 删除缓存条目。
|
||||||
func (c *ProxyCache) Delete(hashKey uint64) {
|
func (c *ProxyCache) Delete(hashKey uint64) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
|||||||
@ -14,6 +14,8 @@
|
|||||||
// 作者:xfy
|
// 作者:xfy
|
||||||
package loadbalance
|
package loadbalance
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
// ValidAlgorithms 是支持的负载均衡算法列表。
|
// ValidAlgorithms 是支持的负载均衡算法列表。
|
||||||
var ValidAlgorithms = []string{
|
var ValidAlgorithms = []string{
|
||||||
"round_robin",
|
"round_robin",
|
||||||
@ -28,10 +30,5 @@ func IsValidAlgorithm(alg string) bool {
|
|||||||
if alg == "" {
|
if alg == "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for _, a := range ValidAlgorithms {
|
return slices.Contains(ValidAlgorithms, alg)
|
||||||
if a == alg {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -465,13 +466,7 @@ func (p *Proxy) ServeHTTP(ctx *fasthttp.RequestCtx) {
|
|||||||
statusCode := ctx.Response.StatusCode()
|
statusCode := ctx.Response.StatusCode()
|
||||||
upstreamStatus = statusCode
|
upstreamStatus = statusCode
|
||||||
|
|
||||||
shouldRetry := false
|
shouldRetry := slices.Contains(httpCodes, statusCode)
|
||||||
for _, code := range httpCodes {
|
|
||||||
if statusCode == code {
|
|
||||||
shouldRetry = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if shouldRetry {
|
if shouldRetry {
|
||||||
// 释放缓存锁
|
// 释放缓存锁
|
||||||
|
|||||||
@ -43,6 +43,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"rua.plus/lolly/internal/config"
|
"rua.plus/lolly/internal/config"
|
||||||
@ -692,12 +693,7 @@ func isInsecureCipher(id uint16) bool {
|
|||||||
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, insecure := range insecureCiphers {
|
return slices.Contains(insecureCiphers, id)
|
||||||
if id == insecure {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultCipherSuites 返回 TLS 1.2 推荐的加密套件。
|
// defaultCipherSuites 返回 TLS 1.2 推荐的加密套件。
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
package variable
|
package variable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"maps"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -78,9 +79,7 @@ func SetGlobalVariables(vars map[string]string) {
|
|||||||
globalVariablesLock.Lock()
|
globalVariablesLock.Lock()
|
||||||
defer globalVariablesLock.Unlock()
|
defer globalVariablesLock.Unlock()
|
||||||
globalVariables = make(map[string]string, len(vars))
|
globalVariables = make(map[string]string, len(vars))
|
||||||
for k, v := range vars {
|
maps.Copy(globalVariables, vars)
|
||||||
globalVariables[k] = v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGlobalVariable 获取全局变量值。
|
// GetGlobalVariable 获取全局变量值。
|
||||||
@ -104,9 +103,7 @@ func GetAllGlobalVariables() map[string]string {
|
|||||||
}
|
}
|
||||||
// 返回副本,避免外部修改影响全局存储
|
// 返回副本,避免外部修改影响全局存储
|
||||||
result := make(map[string]string, len(globalVariables))
|
result := make(map[string]string, len(globalVariables))
|
||||||
for k, v := range globalVariables {
|
maps.Copy(result, globalVariables)
|
||||||
result[k] = v
|
|
||||||
}
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,9 +143,7 @@ func NewContext(ctx *fasthttp.RequestCtx) *Context {
|
|||||||
}
|
}
|
||||||
// 注入全局变量
|
// 注入全局变量
|
||||||
globals := GetAllGlobalVariables()
|
globals := GetAllGlobalVariables()
|
||||||
for name, value := range globals {
|
maps.Copy(vc.store, globals)
|
||||||
vc.store[name] = value
|
|
||||||
}
|
|
||||||
return vc
|
return vc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user