From bb77fa6a98296f292ad5257d937101d38283d502 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 13 Apr 2026 09:51:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=20handler=20?= =?UTF-8?q?=E5=92=8C=20http2=20=E6=A8=A1=E5=9D=97=20lint=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 nolint:errcheck 注释到 defer Close 调用 - 修复 websocket.go 中重复的 nolint 注释格式 - 添加 staticcheck SA1019 nolint 注释到 deprecated WriteScheduler - 移除 sendfile_linux.go 中未使用的 platformLinux 常量 - 添加文件末尾换行符 Co-Authored-By: Claude Opus 4.6 --- internal/handler/sendfile.go | 2 +- internal/handler/sendfile_linux.go | 9 +++------ internal/handler/sendfile_other_test.go | 2 +- internal/handler/sendfile_test.go | 2 +- internal/http2/server.go | 5 +++-- internal/proxy/websocket.go | 8 ++++---- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/internal/handler/sendfile.go b/internal/handler/sendfile.go index 7860868..cbf0adc 100644 --- a/internal/handler/sendfile.go +++ b/internal/handler/sendfile.go @@ -125,4 +125,4 @@ func platformSendfile(conn net.Conn, file *os.File, offset, length int64) error // macOS sendfile 签名复杂,简化使用 fallback // Windows TransmitFile 需要特殊 API return syscall.ENOTSUP -} \ No newline at end of file +} diff --git a/internal/handler/sendfile_linux.go b/internal/handler/sendfile_linux.go index 79a6797..0bb3ee4 100644 --- a/internal/handler/sendfile_linux.go +++ b/internal/handler/sendfile_linux.go @@ -22,9 +22,6 @@ const ( MinSendfileSize = 8 * 1024 ) -// platformLinux Linux 平台标识符。 -const platformLinux = "linux" - // SendFile 零拷贝文件传输。 // // 大文件使用系统调用直接从文件传输到 socket,避免用户空间拷贝, @@ -120,16 +117,16 @@ func getSocketFd(conn net.Conn) (uintptr, error) { if err != nil { return 0, err } - defer func() { _ = file.Close() }() + defer func() { _ = file.Close() }() //nolint:errcheck return file.Fd(), nil case *net.UnixConn: file, err := c.File() if err != nil { return 0, err } - defer func() { _ = file.Close() }() + defer func() { _ = file.Close() }() //nolint:errcheck return file.Fd(), nil default: return 0, syscall.ENOTSUP } -} \ No newline at end of file +} diff --git a/internal/handler/sendfile_other_test.go b/internal/handler/sendfile_other_test.go index 9a5f7dd..68a622e 100644 --- a/internal/handler/sendfile_other_test.go +++ b/internal/handler/sendfile_other_test.go @@ -245,4 +245,4 @@ func TestCopyFile_Error(t *testing.T) { if err == nil { t.Error("Expected error for offset beyond file size") } -} \ No newline at end of file +} diff --git a/internal/handler/sendfile_test.go b/internal/handler/sendfile_test.go index 1fd1c9e..ba11279 100644 --- a/internal/handler/sendfile_test.go +++ b/internal/handler/sendfile_test.go @@ -277,4 +277,4 @@ func TestLinuxSendfile_NilConn(t *testing.T) { if err == nil { t.Error("Expected error for nil connection") } -} \ No newline at end of file +} diff --git a/internal/http2/server.go b/internal/http2/server.go index 43bf24e..b80d152 100644 --- a/internal/http2/server.go +++ b/internal/http2/server.go @@ -101,8 +101,9 @@ func NewServer(cfg *config.HTTP2Config, handler fasthttp.RequestHandler, tlsConf MaxConcurrentStreams: uint32(maxConcurrentStreams), IdleTimeout: idleTimeout, MaxReadFrameSize: uint32(maxHeaderListSize), - NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) }, - CountError: func(_ string) {}, + //nolint:staticcheck // SA1019: NewWriteScheduler deprecated + NewWriteScheduler: func() http2.WriteScheduler { return http2.NewPriorityWriteScheduler(nil) }, + CountError: func(_ string) {}, } return &Server{ diff --git a/internal/proxy/websocket.go b/internal/proxy/websocket.go index ada085d..5ae1ebe 100644 --- a/internal/proxy/websocket.go +++ b/internal/proxy/websocket.go @@ -227,11 +227,11 @@ func dialTarget(targetURL string, timeout time.Duration) (net.Conn, error) { ServerName: strings.Split(addr, ":")[0], }) if err := tlsConn.SetDeadline(time.Now().Add(timeout)); err != nil { - _ = conn.Close() //nolint:errcheck //nolint:errcheck + _ = conn.Close() //nolint:errcheck // 错误处理中关闭连接 return nil, fmt.Errorf("failed to set TLS deadline: %w", err) } if err := tlsConn.Handshake(); err != nil { - _ = conn.Close() //nolint:errcheck //nolint:errcheck + _ = conn.Close() //nolint:errcheck // 错误处理中关闭连接 return nil, fmt.Errorf("TLS handshake failed: %w", err) } return tlsConn, nil @@ -396,14 +396,14 @@ func WebSocket(ctx *fasthttp.RequestCtx, target *loadbalance.Target, timeout tim // 步骤5: 检查响应状态码(期望 101 Switching Protocols) if resp.StatusCode != http.StatusSwitchingProtocols { // 关闭响应 body(升级失败时) - _ = resp.Body.Close() //nolint:errcheck //nolint:errcheck + _ = resp.Body.Close() //nolint:errcheck // 错误处理中关闭响应体 return fmt.Errorf("backend rejected WebSocket upgrade: %s", resp.Status) } // 步骤6: 将升级响应发送回客户端 if err := writeUpgradeResponse(clientConn, resp); err != nil { // 关闭响应 body(写入失败时) - _ = resp.Body.Close() //nolint:errcheck //nolint:errcheck + _ = resp.Body.Close() //nolint:errcheck // 错误处理中关闭响应体 return fmt.Errorf("failed to send upgrade response to client: %w", err) }