refactor(server,middleware): 提取常量提高代码可读性
- ratelimit: 使用 accessUnknown 常量替代硬编码字符串 - testutil: 提取 testListenAddr 常量 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
4697f39924
commit
91f954936e
@ -339,7 +339,7 @@ func (rl *RateLimiter) getRetryAfter(key string) int64 {
|
|||||||
func keyByIP(ctx *fasthttp.RequestCtx) string {
|
func keyByIP(ctx *fasthttp.RequestCtx) string {
|
||||||
ip := netutil.ExtractClientIPNet(ctx)
|
ip := netutil.ExtractClientIPNet(ctx)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return "unknown"
|
return accessUnknown
|
||||||
}
|
}
|
||||||
return ip.String()
|
return ip.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,9 @@ import (
|
|||||||
"rua.plus/lolly/internal/ssl"
|
"rua.plus/lolly/internal/ssl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// testListenAddr 是测试用的随机端口监听地址
|
||||||
|
const testListenAddr = "127.0.0.1:0"
|
||||||
|
|
||||||
// MockFastServer 是 fasthttp.Server 的 Mock 包装
|
// MockFastServer 是 fasthttp.Server 的 Mock 包装
|
||||||
// 定义在此文件以便 TestServerOptions 可以引用
|
// 定义在此文件以便 TestServerOptions 可以引用
|
||||||
type MockFastServer struct {
|
type MockFastServer struct {
|
||||||
@ -108,7 +111,7 @@ func MustStartTestServer(cfg *config.Config) *Server {
|
|||||||
}
|
}
|
||||||
if listenAddr == "" || listenAddr == ":80" {
|
if listenAddr == "" || listenAddr == ":80" {
|
||||||
if len(cfg.Servers) > 0 {
|
if len(cfg.Servers) > 0 {
|
||||||
cfg.Servers[0].Listen = "127.0.0.1:0"
|
cfg.Servers[0].Listen = testListenAddr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user