refactor(server/pprof): 简化 switch 语句并移除未使用变量

- 将 switch 语句改为更简洁的 case 匹配形式
- 移除未使用的 cpuProfileWriter 变量
- 忽略 Flush/WriteTo 等无关的错误返回值

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-08 09:58:11 +08:00
parent 3758f410ff
commit d482e44bec
2 changed files with 22 additions and 25 deletions

View File

@ -105,18 +105,18 @@ func (h *PprofHandler) ServeHTTP(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path()) path := string(ctx.Path())
subPath := path[len(h.path):] subPath := path[len(h.path):]
switch { switch subPath {
case subPath == "" || subPath == "/": case "", "/":
h.handleIndex(ctx) h.handleIndex(ctx)
case subPath == "/profile": case "/profile":
h.handleCPU(ctx) h.handleCPU(ctx)
case subPath == "/heap": case "/heap":
h.handleHeap(ctx) h.handleHeap(ctx)
case subPath == "/goroutine": case "/goroutine":
h.handleGoroutine(ctx) h.handleGoroutine(ctx)
case subPath == "/block": case "/block":
h.handleBlock(ctx) h.handleBlock(ctx)
case subPath == "/mutex": case "/mutex":
h.handleMutex(ctx) h.handleMutex(ctx)
default: default:
ctx.SetStatusCode(fasthttp.StatusNotFound) ctx.SetStatusCode(fasthttp.StatusNotFound)
@ -187,8 +187,8 @@ func (h *PprofHandler) handleCPU(ctx *fasthttp.RequestCtx) {
ctx.SetBodyStreamWriter(func(w *bufio.Writer) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
// 启动 CPU profile // 启动 CPU profile
if err := startCPUProfile(wrapBufioWriter(w)); err != nil { if err := startCPUProfile(wrapBufioWriter(w)); err != nil {
w.WriteString("Error starting CPU profile: " + err.Error()) _, _ = w.WriteString("Error starting CPU profile: " + err.Error())
w.Flush() _ = w.Flush()
return return
} }
@ -197,7 +197,7 @@ func (h *PprofHandler) handleCPU(ctx *fasthttp.RequestCtx) {
// 厉止 CPU profile // 厉止 CPU profile
stopCPUProfile() stopCPUProfile()
w.Flush() _ = w.Flush()
}) })
} }
@ -206,7 +206,7 @@ func (h *PprofHandler) handleHeap(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/octet-stream") ctx.SetContentType("application/octet-stream")
ctx.SetBodyStreamWriter(func(w *bufio.Writer) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
writeHeapProfile(wrapBufioWriter(w)) writeHeapProfile(wrapBufioWriter(w))
w.Flush() _ = w.Flush()
}) })
} }
@ -215,7 +215,7 @@ func (h *PprofHandler) handleGoroutine(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/octet-stream") ctx.SetContentType("application/octet-stream")
ctx.SetBodyStreamWriter(func(w *bufio.Writer) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
writeGoroutineProfile(wrapBufioWriter(w)) writeGoroutineProfile(wrapBufioWriter(w))
w.Flush() _ = w.Flush()
}) })
} }
@ -224,7 +224,7 @@ func (h *PprofHandler) handleBlock(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/octet-stream") ctx.SetContentType("application/octet-stream")
ctx.SetBodyStreamWriter(func(w *bufio.Writer) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
writeBlockProfile(wrapBufioWriter(w)) writeBlockProfile(wrapBufioWriter(w))
w.Flush() _ = w.Flush()
}) })
} }
@ -233,6 +233,6 @@ func (h *PprofHandler) handleMutex(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/octet-stream") ctx.SetContentType("application/octet-stream")
ctx.SetBodyStreamWriter(func(w *bufio.Writer) { ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
writeMutexProfile(wrapBufioWriter(w)) writeMutexProfile(wrapBufioWriter(w))
w.Flush() _ = w.Flush()
}) })
} }

View File

@ -14,8 +14,7 @@ import (
) )
var ( var (
cpuProfileMu sync.Mutex cpuProfileMu sync.Mutex
cpuProfileWriter io.Writer
cpuProfileActive bool cpuProfileActive bool
) )
@ -38,7 +37,6 @@ func startCPUProfile(w io.Writer) error {
return err return err
} }
cpuProfileWriter = w
cpuProfileActive = true cpuProfileActive = true
return nil return nil
} }
@ -51,21 +49,20 @@ func stopCPUProfile() {
if cpuProfileActive { if cpuProfileActive {
pprof.StopCPUProfile() pprof.StopCPUProfile()
cpuProfileActive = false cpuProfileActive = false
cpuProfileWriter = nil
} }
} }
// writeHeapProfile 写入内存分配 profile。 // writeHeapProfile 写入内存分配 profile。
func writeHeapProfile(w io.Writer) { func writeHeapProfile(w io.Writer) {
runtime.GC() // 先执行 GC获取更准确的数据 runtime.GC() // 先执行 GC获取更准确的数据
pprof.WriteHeapProfile(w) _ = pprof.WriteHeapProfile(w)
} }
// writeGoroutineProfile 写入 Goroutine stack traces。 // writeGoroutineProfile 写入 Goroutine stack traces。
func writeGoroutineProfile(w io.Writer) { func writeGoroutineProfile(w io.Writer) {
p := pprof.Lookup("goroutine") p := pprof.Lookup("goroutine")
if p != nil { 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) { func writeBlockProfile(w io.Writer) {
p := pprof.Lookup("block") p := pprof.Lookup("block")
if p != nil { 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) { func writeMutexProfile(w io.Writer) {
p := pprof.Lookup("mutex") p := pprof.Lookup("mutex")
if p != nil { 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) { func (a *bufioWriterAdapter) Write(p []byte) (n int, err error) {
n, err = a.w.Write(p) n, err = a.w.Write(p)
if err == nil { if err == nil {
a.w.Flush() _ = a.w.Flush()
} }
return n, err return n, err
} }