diff --git a/internal/server/pprof.go b/internal/server/pprof.go index 4316a82..d5ed468 100644 --- a/internal/server/pprof.go +++ b/internal/server/pprof.go @@ -105,18 +105,18 @@ func (h *PprofHandler) ServeHTTP(ctx *fasthttp.RequestCtx) { path := string(ctx.Path()) subPath := path[len(h.path):] - switch { - case subPath == "" || subPath == "/": + switch subPath { + case "", "/": h.handleIndex(ctx) - case subPath == "/profile": + case "/profile": h.handleCPU(ctx) - case subPath == "/heap": + case "/heap": h.handleHeap(ctx) - case subPath == "/goroutine": + case "/goroutine": h.handleGoroutine(ctx) - case subPath == "/block": + case "/block": h.handleBlock(ctx) - case subPath == "/mutex": + case "/mutex": h.handleMutex(ctx) default: ctx.SetStatusCode(fasthttp.StatusNotFound) @@ -187,8 +187,8 @@ func (h *PprofHandler) handleCPU(ctx *fasthttp.RequestCtx) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) { // 启动 CPU profile if err := startCPUProfile(wrapBufioWriter(w)); err != nil { - w.WriteString("Error starting CPU profile: " + err.Error()) - w.Flush() + _, _ = w.WriteString("Error starting CPU profile: " + err.Error()) + _ = w.Flush() return } @@ -197,7 +197,7 @@ func (h *PprofHandler) handleCPU(ctx *fasthttp.RequestCtx) { // 厉止 CPU profile stopCPUProfile() - w.Flush() + _ = w.Flush() }) } @@ -206,7 +206,7 @@ func (h *PprofHandler) handleHeap(ctx *fasthttp.RequestCtx) { ctx.SetContentType("application/octet-stream") ctx.SetBodyStreamWriter(func(w *bufio.Writer) { writeHeapProfile(wrapBufioWriter(w)) - w.Flush() + _ = w.Flush() }) } @@ -215,7 +215,7 @@ func (h *PprofHandler) handleGoroutine(ctx *fasthttp.RequestCtx) { ctx.SetContentType("application/octet-stream") ctx.SetBodyStreamWriter(func(w *bufio.Writer) { writeGoroutineProfile(wrapBufioWriter(w)) - w.Flush() + _ = w.Flush() }) } @@ -224,7 +224,7 @@ func (h *PprofHandler) handleBlock(ctx *fasthttp.RequestCtx) { ctx.SetContentType("application/octet-stream") ctx.SetBodyStreamWriter(func(w *bufio.Writer) { writeBlockProfile(wrapBufioWriter(w)) - w.Flush() + _ = w.Flush() }) } @@ -233,6 +233,6 @@ func (h *PprofHandler) handleMutex(ctx *fasthttp.RequestCtx) { ctx.SetContentType("application/octet-stream") ctx.SetBodyStreamWriter(func(w *bufio.Writer) { writeMutexProfile(wrapBufioWriter(w)) - w.Flush() + _ = w.Flush() }) -} \ No newline at end of file +} diff --git a/internal/server/pprof_impl.go b/internal/server/pprof_impl.go index 1d9b4df..99a5702 100644 --- a/internal/server/pprof_impl.go +++ b/internal/server/pprof_impl.go @@ -14,8 +14,7 @@ import ( ) var ( - cpuProfileMu sync.Mutex - cpuProfileWriter io.Writer + cpuProfileMu sync.Mutex cpuProfileActive bool ) @@ -38,7 +37,6 @@ func startCPUProfile(w io.Writer) error { return err } - cpuProfileWriter = w cpuProfileActive = true return nil } @@ -51,21 +49,20 @@ func stopCPUProfile() { if cpuProfileActive { pprof.StopCPUProfile() cpuProfileActive = false - cpuProfileWriter = nil } } // writeHeapProfile 写入内存分配 profile。 func writeHeapProfile(w io.Writer) { runtime.GC() // 先执行 GC,获取更准确的数据 - pprof.WriteHeapProfile(w) + _ = pprof.WriteHeapProfile(w) } // writeGoroutineProfile 写入 Goroutine stack traces。 func writeGoroutineProfile(w io.Writer) { p := pprof.Lookup("goroutine") if p != nil { - p.WriteTo(w, 0) + _ = p.WriteTo(w, 0) } } @@ -73,7 +70,7 @@ func writeGoroutineProfile(w io.Writer) { func writeBlockProfile(w io.Writer) { p := pprof.Lookup("block") if p != nil { - p.WriteTo(w, 0) + _ = p.WriteTo(w, 0) } } @@ -81,7 +78,7 @@ func writeBlockProfile(w io.Writer) { func writeMutexProfile(w io.Writer) { p := pprof.Lookup("mutex") if p != nil { - p.WriteTo(w, 0) + _ = p.WriteTo(w, 0) } } @@ -93,7 +90,7 @@ type bufioWriterAdapter struct { func (a *bufioWriterAdapter) Write(p []byte) (n int, err error) { n, err = a.w.Write(p) if err == nil { - a.w.Flush() + _ = a.w.Flush() } return n, err } @@ -101,4 +98,4 @@ func (a *bufioWriterAdapter) Write(p []byte) (n int, err error) { // wrapBufioWriter 将 bufio.Writer 包装为 io.Writer。 func wrapBufioWriter(w *bufio.Writer) io.Writer { return &bufioWriterAdapter{w: w} -} \ No newline at end of file +}