Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ middleware/pprof: improve performance #2709

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions middleware/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func New(config ...Config) fiber.Handler {
pprofThreadcreate = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("threadcreate").ServeHTTP)
)

// Construct actual prefix
prefix := cfg.Prefix + "/debug/pprof"

// Return new handler
return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true
Expand All @@ -38,43 +41,55 @@ func New(config ...Config) fiber.Handler {

path := c.Path()
// We are only interested in /debug/pprof routes
if len(path) < 12 || !strings.HasPrefix(path, cfg.Prefix+"/debug/pprof") {
path, found := cutPrefix(path, prefix)
if !found {
return c.Next()
}
// Switch to original path without stripped slashes
// Switch on trimmed path against constant strings
switch path {
case cfg.Prefix + "/debug/pprof/":
case "/":
pprofIndex(c.Context())
case cfg.Prefix + "/debug/pprof/cmdline":
case "/cmdline":
pprofCmdline(c.Context())
case cfg.Prefix + "/debug/pprof/profile":
case "/profile":
pprofProfile(c.Context())
case cfg.Prefix + "/debug/pprof/symbol":
case "/symbol":
pprofSymbol(c.Context())
case cfg.Prefix + "/debug/pprof/trace":
case "/trace":
pprofTrace(c.Context())
case cfg.Prefix + "/debug/pprof/allocs":
case "/allocs":
pprofAllocs(c.Context())
case cfg.Prefix + "/debug/pprof/block":
case "/block":
pprofBlock(c.Context())
case cfg.Prefix + "/debug/pprof/goroutine":
case "/goroutine":
pprofGoroutine(c.Context())
case cfg.Prefix + "/debug/pprof/heap":
case "/heap":
pprofHeap(c.Context())
case cfg.Prefix + "/debug/pprof/mutex":
case "/mutex":
pprofMutex(c.Context())
case cfg.Prefix + "/debug/pprof/threadcreate":
case "/threadcreate":
pprofThreadcreate(c.Context())
default:
// pprof index only works with trailing slash
if strings.HasSuffix(path, "/") {
path = strings.TrimRight(path, "/")
} else {
path = cfg.Prefix + "/debug/pprof/"
path = prefix + "/"
}

return c.Redirect(path, fiber.StatusFound)
}
return nil
}
}

// cutPrefix is a copy of [strings.CutPrefix] added in Go 1.20.
// Remove this function when we drop support for Go 1.19.
//
//nolint:nonamedreturns // Align with its original form in std.
func cutPrefix(s, prefix string) (after string, found bool) {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasPrefix(s, prefix) {
return s, false
}
return s[len(prefix):], true
}