From 4e826925acdd378b3b8bbefbd32a6db8db1e3b48 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 09:41:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=80=82=E9=85=8D=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E6=A8=A1=E5=9D=97=E7=B1=BB=E5=9E=8B=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配 variable.NewContext/ReleaseContext 适配 resolver.DNSCacheEntry 适配 logging/formatJSON 常量 Co-Authored-By: Claude Opus 4.6 --- internal/app/app.go | 1 + internal/app/app_test.go | 14 +++++------ internal/benchmark/tools/benchmark_context.go | 2 +- internal/benchmark/tools/mock_backend.go | 4 ++++ internal/handler/sendfile.go | 2 +- internal/handler/sendfile_test.go | 18 +++++++------- internal/handler/static_test.go | 24 +++++++++---------- internal/integration/variable_test.go | 22 ++++++++--------- internal/netutil/ip_test.go | 2 +- 9 files changed, 47 insertions(+), 42 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 48fd1ca..1db10b0 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -478,6 +478,7 @@ func (a *App) gracefulUpgrade() { // sigName 返回信号名称(用于日志输出)。 func sigName(sig syscall.Signal) string { + //nolint:exhaustive switch sig { case syscall.SIGTERM: return "SIGTERM" diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 366ef24..5a5589d 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -483,7 +483,7 @@ func TestHandleSignal_Unknown(t *testing.T) { } // TestShutdownHTTP3_NilServer 测试 HTTP/3 服务器为 nil 时关闭 -func TestShutdownHTTP3_NilServer(t *testing.T) { +func TestShutdownHTTP3_NilServer(_ *testing.T) { app := NewApp("") app.logger = logging.NewAppLogger(&config.LoggingConfig{}) @@ -492,7 +492,7 @@ func TestShutdownHTTP3_NilServer(t *testing.T) { } // TestReopenLogs 测试重开日志 -func TestReopenLogs(t *testing.T) { +func TestReopenLogs(_ *testing.T) { app := NewApp("") app.cfg = &config.Config{ Logging: config.LoggingConfig{ @@ -508,7 +508,7 @@ func TestReopenLogs(t *testing.T) { } // TestReloadConfig_FileNotFound 测试重载不存在的配置 -func TestReloadConfig_FileNotFound(t *testing.T) { +func TestReloadConfig_FileNotFound(_ *testing.T) { app := NewApp("/nonexistent/config.yaml") app.logger = logging.NewAppLogger(&config.LoggingConfig{}) @@ -549,7 +549,7 @@ logging: } // TestSetupSignalHandlers 测试信号处理设置 -func TestSetupSignalHandlers(t *testing.T) { +func TestSetupSignalHandlers(_ *testing.T) { app := NewApp("") app.cfg = &config.Config{ Server: config.ServerConfig{ @@ -585,7 +585,7 @@ func TestHandleSignal_SIGUSR2(t *testing.T) { } // TestGracefulUpgrade_NoListener 测试无监听器时的热升级 -func TestGracefulUpgrade_NoListener(t *testing.T) { +func TestGracefulUpgrade_NoListener(_ *testing.T) { app := NewApp("") app.cfg = &config.Config{ Server: config.ServerConfig{ @@ -652,7 +652,7 @@ func TestAppFields(t *testing.T) { } // TestShutdownHTTP3_WithServer 测试有 HTTP3 服务器时的关闭 -func TestShutdownHTTP3_WithServer(t *testing.T) { +func TestShutdownHTTP3_WithServer(_ *testing.T) { app := NewApp("") app.cfg = &config.Config{ Server: config.ServerConfig{ @@ -673,7 +673,7 @@ func TestShutdownHTTP3_WithServer(t *testing.T) { } // TestReopenLogs_WithNilConfig 测试配置为 nil 时重开日志 -func TestReopenLogs_WithNilConfig(t *testing.T) { +func TestReopenLogs_WithNilConfig(_ *testing.T) { app := NewApp("") app.logger = logging.NewAppLogger(&config.LoggingConfig{}) diff --git a/internal/benchmark/tools/benchmark_context.go b/internal/benchmark/tools/benchmark_context.go index 53a3a2c..8b5c635 100644 --- a/internal/benchmark/tools/benchmark_context.go +++ b/internal/benchmark/tools/benchmark_context.go @@ -165,7 +165,7 @@ func (bc *BenchmarkContext) MockResponse() []byte { // // 返回值: // - []byte: 模拟的响应数据 -func (bc *BenchmarkContext) MockResponseWithContentType(statusCode int, contentType string) []byte { +func (bc *BenchmarkContext) MockResponseWithContentType(_, _ string) []byte { // 返回响应体,调用者负责设置状态码和 Content-Type return GenerateTestData(bc.ResponseSize) } diff --git a/internal/benchmark/tools/mock_backend.go b/internal/benchmark/tools/mock_backend.go index 055790c..ff5ac43 100644 --- a/internal/benchmark/tools/mock_backend.go +++ b/internal/benchmark/tools/mock_backend.go @@ -76,6 +76,10 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) { mb.mu.RUnlock() switch config.Mode { + case ModeFixed: + ctx.SetStatusCode(config.StatusCode) + _, _ = ctx.Write(config.Body) + case ModeDelay: time.Sleep(config.Delay) ctx.SetStatusCode(config.StatusCode) diff --git a/internal/handler/sendfile.go b/internal/handler/sendfile.go index 4528a87..037e33c 100644 --- a/internal/handler/sendfile.go +++ b/internal/handler/sendfile.go @@ -160,7 +160,7 @@ func platformSendfile(conn net.Conn, file *os.File, offset, length int64) error // // 返回值: // - error: 系统调用错误 -func linuxSendfile(conn net.Conn, fileFd uintptr, offset, length int64) error { +func linuxSendfile(conn net.Conn, fileFd uintptr, _, length int64) error { socketFd, err := getSocketFd(conn) if err != nil { return err diff --git a/internal/handler/sendfile_test.go b/internal/handler/sendfile_test.go index 3a1602e..b87fe27 100644 --- a/internal/handler/sendfile_test.go +++ b/internal/handler/sendfile_test.go @@ -187,14 +187,14 @@ func TestGetSocketFd_UnsupportedType(t *testing.T) { // mockConn 是一个不实现 TCPConn/UnixConn 的连接 type mockConn struct{} -func (m *mockConn) Read(b []byte) (n int, err error) { return 0, nil } -func (m *mockConn) Write(b []byte) (n int, err error) { return 0, nil } -func (m *mockConn) Close() error { return nil } -func (m *mockConn) LocalAddr() net.Addr { return nil } -func (m *mockConn) RemoteAddr() net.Addr { return nil } -func (m *mockConn) SetDeadline(t time.Time) error { return nil } -func (m *mockConn) SetReadDeadline(t time.Time) error { return nil } -func (m *mockConn) SetWriteDeadline(t time.Time) error { return nil } +func (m *mockConn) Read([]byte) (n int, err error) { return 0, nil } +func (m *mockConn) Write([]byte) (n int, err error) { return 0, nil } +func (m *mockConn) Close() error { return nil } +func (m *mockConn) LocalAddr() net.Addr { return nil } +func (m *mockConn) RemoteAddr() net.Addr { return nil } +func (m *mockConn) SetDeadline(time.Time) error { return nil } +func (m *mockConn) SetReadDeadline(time.Time) error { return nil } +func (m *mockConn) SetWriteDeadline(time.Time) error { return nil } // TestSendFile_SmallFile 测试小文件发送(使用 fallback) func TestSendFile_SmallFile(t *testing.T) { @@ -287,7 +287,7 @@ func TestSendFile_ZeroLength(t *testing.T) { } // TestGetNetConn 测试获取底层连接 -func TestGetNetConn(t *testing.T) { +func TestGetNetConn(_ *testing.T) { ctx := &fasthttp.RequestCtx{} ctx.Init(&fasthttp.Request{}, nil, nil) diff --git a/internal/handler/static_test.go b/internal/handler/static_test.go index 8d954a2..f8fe4e1 100644 --- a/internal/handler/static_test.go +++ b/internal/handler/static_test.go @@ -111,7 +111,7 @@ func TestStaticHandlerHandle(t *testing.T) { }, { name: "文件不存在", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() // 不创建任何文件 }, @@ -121,7 +121,7 @@ func TestStaticHandlerHandle(t *testing.T) { }, { name: "空路径访问根目录无索引", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() // root 目录没有索引文件 }, @@ -188,7 +188,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }{ { name: "文件名包含双点 - 安全检查拦截", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() // 不创建任何文件 }, @@ -198,7 +198,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "路径末尾双点 - 安全检查拦截", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() }, path: "/foo/..", @@ -207,7 +207,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "隐藏文件 .hidden - 文件不存在", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() }, path: "/.hidden", @@ -216,7 +216,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "文件名包含多点 ...txt - 安全检查拦截", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() }, path: "/file...txt", @@ -225,7 +225,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "fasthttp 规范化后的路径 - 文件不存在", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() // fasthttp 将 /../secret.txt 规范化为 /secret.txt }, @@ -235,7 +235,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "URL 编码路径遍历 - fasthttp 规范化", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() // fasthttp 解码 %2e%2e 为 .. 并规范化路径 }, @@ -245,7 +245,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) { }, { name: "混合 URL 编码 - fasthttp 规范化", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { t.Helper() }, path: "/%2e%2e%2fsecret.txt", @@ -764,7 +764,7 @@ func TestStaticHandler_handleTryFiles(t *testing.T) { }, { name: "所有 try_files 都未找到", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { // 不创建任何文件 }, tryFiles: []string{"$uri", "$uri/", "/index.html"}, @@ -879,7 +879,7 @@ func TestStaticHandler_handleInternalRedirect(t *testing.T) { }, { name: "内部重定向目标不存在", - setup: func(t *testing.T, root string) { + setup: func(_ *testing.T, _ string) { // 不创建 fallback 文件 }, tryFiles: []string{"$uri", "/fallback.html"}, @@ -1156,7 +1156,7 @@ func TestStaticHandler_Alias(t *testing.T) { }, { name: "alias 文件不存在", - setup: func(t *testing.T, aliasDir string) { + setup: func(_ *testing.T, _ string) { // 不创建任何文件 }, alias: "/alias/images/", diff --git a/internal/integration/variable_test.go b/internal/integration/variable_test.go index 542bf6b..761e1a7 100644 --- a/internal/integration/variable_test.go +++ b/internal/integration/variable_test.go @@ -57,8 +57,8 @@ func TestVariableInProxyHeaders(t *testing.T) { ctx.Request.Header.Set("X-Custom-Header", "original") // 测试变量展开 - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) // 模拟代理配置中的 header 设置 tests := []struct { @@ -126,7 +126,7 @@ func TestVariableInRewriteRules(t *testing.T) { ctx.Request.SetRequestURI("/redirect/old-page") ctx.Request.Header.SetMethod("GET") - handler := mw.Process(func(c *fasthttp.RequestCtx) { + handler := mw.Process(func(_ *fasthttp.RequestCtx) { t.Error("should not reach next handler for redirect") }) handler(ctx) @@ -150,8 +150,8 @@ func TestVariableCompatibilityWithNginx(t *testing.T) { // 设置响应信息 variable.SetResponseInfoInContext(ctx, 201, 2048, 100000000) // 100ms - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) // 设置 HTTP 头变量(logging 中会自动设置这些) vc.Set("http_referer", "http://referrer.com") @@ -190,8 +190,8 @@ func TestVariablePerformance(t *testing.T) { ctx.Request.Header.SetMethod("GET") ctx.Request.Header.SetHost("api.example.com") - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) // 常见日志格式模板 template := "$remote_addr - $remote_user [$time_local] \"$request_method $request_uri $scheme\" $status $body_bytes_sent \"$http_user_agent\"" @@ -271,8 +271,8 @@ func TestVariableEdgeCases(t *testing.T) { ctx := &fasthttp.RequestCtx{} tt.setup(ctx) - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) result := vc.Expand(tt.template) if result != tt.expected { @@ -292,8 +292,8 @@ func TestVariableAllBuiltins(t *testing.T) { // 设置响应信息 variable.SetResponseInfoInContext(ctx, 200, 1024, 50000000) // 50ms - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) // 测试所有内置变量 builtinVars := []string{ diff --git a/internal/netutil/ip_test.go b/internal/netutil/ip_test.go index 908f23a..a2e76fe 100644 --- a/internal/netutil/ip_test.go +++ b/internal/netutil/ip_test.go @@ -120,7 +120,7 @@ func TestExtractClientIPNet(t *testing.T) { } } -func TestGetRemoteAddrIP(t *testing.T) { +func TestGetRemoteAddrIP(_ *testing.T) { ctx := &fasthttp.RequestCtx{} ctx.Init(&fasthttp.Request{}, nil, nil)