feat(core): 统一 SQL 驱动诊断与 HTTP 启停分析能力
Verify / verify (push) Canceled after 0s

This commit is contained in:
zhangdeman committed 2026-11-28 02:06:19 -08:00
1 parent dcd47bf6e7
commit 351defec7b
8 files changed
+362 -6

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
// Package pprof 将标准库分析 Handler 显式注册到框架服务器,不改变业务认证。
// 只有导入本子包才依赖 net/http/pprof,普通 http 包不会隐式加载分析 Handler。
package pprof
import (
"net"
"net/http"
native "net/http/pprof"
"strings"
framework "git.zhangdeman.cn/zhangdeman/ginframework/http"
"github.com/gin-gonic/gin"
)
// Register 原子注册 /debug/pprof 的标准库分析入口,应用须显式调用才会挂载 Gin 路由。
// 入参:server 为应用已创建的框架服务,authorize 为可选鉴权;nil 仅允许 TCP 回环对端。
// 返回:框架路由冲突或冻结错误;不会部分注册、不创建监听器或依赖 DefaultServeMux 提供服务。
// 隐式逻辑:鉴权回调替代默认回环限制,失败必须 Abort;转发头不参与默认对端判断。
func Register(server *framework.Server, authorize gin.HandlerFunc) error {
if authorize == nil {
authorize = loopbackOnly
}
routes := []framework.NativeRoute{}
for _, item := range []struct {
name string `dc:"标准库分析路由后缀"`
handler http.HandlerFunc `dc:"标准库分析请求处理器"`
}{
{"", native.Index}, {"cmdline", native.Cmdline}, {"profile", native.Profile}, {"symbol", native.Symbol}, {"trace", native.Trace},
} {
routes = append(routes, framework.NativeRoute{Metadata: framework.Metadata{Method: http.MethodGet, Path: "/debug/pprof/" + item.name}, Handlers: []gin.HandlerFunc{authorize, gin.WrapF(item.handler)}})
}
for _, name := range []string{"allocs", "block", "goroutine", "heap", "mutex", "threadcreate"} {
routes = append(routes, framework.NativeRoute{Metadata: framework.Metadata{Method: http.MethodGet, Path: "/debug/pprof/" + name}, Handlers: []gin.HandlerFunc{authorize, gin.WrapH(native.Handler(name))}})
}
routes = append(routes, framework.NativeRoute{Metadata: framework.Metadata{Method: http.MethodPost, Path: "/debug/pprof/symbol"}, Handlers: []gin.HandlerFunc{authorize, gin.WrapF(native.Symbol)}})
return server.HandleBatch(routes...)
}
// loopbackOnly 只信任 Request.RemoteAddr 的回环对端;入参为当前请求,拒绝时终止处理。
func loopbackOnly(ctx *gin.Context) {
host, _, err := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr))
if err != nil {
host = ctx.Request.RemoteAddr
}
ip := net.ParseIP(strings.Trim(host, "[]"))
if ip == nil || !ip.IsLoopback() {
ctx.AbortWithStatus(http.StatusForbidden)
return
}
ctx.Next()
}
+61
View File
@@ -0,0 +1,61 @@
package pprof
import (
"net/http"
"net/http/httptest"
"testing"
framework "git.zhangdeman.cn/zhangdeman/ginframework/http"
"github.com/gin-gonic/gin"
)
// TestRegisterUsesRealPeerAndExplicitAuth 覆盖 TCP 回环、伪造代理头和显式认证。
func TestRegisterUsesRealPeerAndExplicitAuth(t *testing.T) {
for _, item := range []struct {
name, peer string
auth gin.HandlerFunc
want int
}{
{"remote", "203.0.113.10:1234", nil, http.StatusForbidden},
{"IPv4", "127.0.0.1:1234", nil, http.StatusOK},
{"IPv6", "[::1]:1234", nil, http.StatusOK},
{"explicit auth", "203.0.113.10:1234", func(ctx *gin.Context) { ctx.AbortWithStatus(http.StatusUnauthorized) }, http.StatusUnauthorized},
} {
t.Run(item.name, func(t *testing.T) {
server, err := framework.NewServer(framework.Config{}, framework.Options{})
if err != nil {
t.Fatal(err)
}
if err = Register(server, item.auth); err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodGet, "/debug/pprof/", nil)
request.RemoteAddr = item.peer
request.Header.Set("X-Forwarded-For", "127.0.0.1")
recorder := httptest.NewRecorder()
server.Handler().ServeHTTP(recorder, request)
if recorder.Code != item.want {
t.Fatalf("status=%d, want %d", recorder.Code, item.want)
}
})
}
}
// TestRegisterRejectsWholeBatchOnConflict 确认标准分析入口仍服从框架原子注册和冻结。
func TestRegisterRejectsWholeBatchOnConflict(t *testing.T) {
server, _ := framework.NewServer(framework.Config{}, framework.Options{})
if err := server.Handle(http.MethodGet, "/debug/pprof/heap", func(*gin.Context) {}); err != nil {
t.Fatal(err)
}
if err := Register(server, nil); err == nil {
t.Fatal("expected route conflict")
}
if len(server.Routes()) != 1 {
t.Fatal("pprof batch was partially registered")
}
frozen, _ := framework.NewServer(framework.Config{}, framework.Options{})
frozen.Handler()
if err := Register(frozen, nil); err == nil {
t.Fatal("registered pprof after freeze")
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.