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())
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()
})
}
}

View File

@ -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}
}
}