refactor(server,middleware): 提取常量提高代码可读性

- ratelimit: 使用 accessUnknown 常量替代硬编码字符串
- testutil: 提取 testListenAddr 常量

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-22 10:41:48 +08:00
parent 4697f39924
commit 91f954936e
2 changed files with 5 additions and 2 deletions

View File

@ -339,7 +339,7 @@ func (rl *RateLimiter) getRetryAfter(key string) int64 {
func keyByIP(ctx *fasthttp.RequestCtx) string {
ip := netutil.ExtractClientIPNet(ctx)
if ip == nil {
return "unknown"
return accessUnknown
}
return ip.String()
}

View File

@ -12,6 +12,9 @@ import (
"rua.plus/lolly/internal/ssl"
)
// testListenAddr 是测试用的随机端口监听地址
const testListenAddr = "127.0.0.1:0"
// MockFastServer 是 fasthttp.Server 的 Mock 包装
// 定义在此文件以便 TestServerOptions 可以引用
type MockFastServer struct {
@ -108,7 +111,7 @@ func MustStartTestServer(cfg *config.Config) *Server {
}
if listenAddr == "" || listenAddr == ":80" {
if len(cfg.Servers) > 0 {
cfg.Servers[0].Listen = "127.0.0.1:0"
cfg.Servers[0].Listen = testListenAddr
}
}