From edd64fd9a09c27d2a839bdb8bdcfe832eecb98d6 Mon Sep 17 00:00:00 2001 From: Tolya Korniltsev Date: Thu, 15 Aug 2024 13:01:55 +0200 Subject: [PATCH] fix: avoid panic in init godeltaprof/http/pprof for go 1.23 (#119) * fix: avoid panic in init godeltaprof/http/pprof * simplify: just use "GET " all the time on go 1.22 --- godeltaprof/http/pprof/pprof.go | 7 ++++--- godeltaprof/http/pprof/pprof_go21.go | 7 +++++++ godeltaprof/http/pprof/pprof_go22.go | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 godeltaprof/http/pprof/pprof_go21.go create mode 100644 godeltaprof/http/pprof/pprof_go22.go diff --git a/godeltaprof/http/pprof/pprof.go b/godeltaprof/http/pprof/pprof.go index 81331dff..5adaf005 100644 --- a/godeltaprof/http/pprof/pprof.go +++ b/godeltaprof/http/pprof/pprof.go @@ -21,9 +21,10 @@ type deltaProfiler interface { } func init() { - http.HandleFunc("/debug/pprof/delta_heap", Heap) - http.HandleFunc("/debug/pprof/delta_block", Block) - http.HandleFunc("/debug/pprof/delta_mutex", Mutex) + prefix := routePrefix() + http.HandleFunc(prefix+"/debug/pprof/delta_heap", Heap) + http.HandleFunc(prefix+"/debug/pprof/delta_block", Block) + http.HandleFunc(prefix+"/debug/pprof/delta_mutex", Mutex) } func Heap(w http.ResponseWriter, r *http.Request) { diff --git a/godeltaprof/http/pprof/pprof_go21.go b/godeltaprof/http/pprof/pprof_go21.go new file mode 100644 index 00000000..dff3250e --- /dev/null +++ b/godeltaprof/http/pprof/pprof_go21.go @@ -0,0 +1,7 @@ +//go:build !go1.22 + +package pprof + +func routePrefix() string { + return "" +} diff --git a/godeltaprof/http/pprof/pprof_go22.go b/godeltaprof/http/pprof/pprof_go22.go new file mode 100644 index 00000000..069ec98e --- /dev/null +++ b/godeltaprof/http/pprof/pprof_go22.go @@ -0,0 +1,10 @@ +//go:build go1.22 + +package pprof + +func routePrefix() string { + // As of go 1.23 we will panic if we don't prefix with "GET " + // https://github.com/golang/go/blob/9fcffc53593c5cd103630d0d24ef8bd91e17246d/src/net/http/pprof/pprof.go#L98-L97 + // https://github.com/golang/go/commit/9fcffc53593c5cd103630d0d24ef8bd91e17246d + return "GET " +}