Add pprof HTTP server

This enables production debugging of the bouncer.

Closes: https://todo.sr.ht/~emersion/soju/155
This commit is contained in:
Simon Ser 2021-11-17 16:15:27 +01:00
parent ea98ac042e
commit d722f56000
2 changed files with 24 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import (
"log"
"net"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
@ -286,6 +287,26 @@ func main() {
log.Fatalf("serving %q: %v", listen, err)
}
}()
case "http+pprof":
// Only allow localhost as listening host for security reasons.
// Users can always explicitly setup reverse proxies if desirable.
hostname, _, err := net.SplitHostPort(u.Host)
if err != nil {
log.Fatalf("invalid host in URI %q: %v", listen, err)
} else if hostname != "localhost" {
log.Fatalf("pprof listening host must be localhost")
}
// net/http/pprof registers its handlers in http.DefaultServeMux
httpSrv := http.Server{
Addr: u.Host,
Handler: http.DefaultServeMux,
}
go func() {
if err := httpSrv.ListenAndServe(); err != nil {
log.Fatalf("serving %q: %v", listen, err)
}
}()
default:
log.Fatalf("failed to listen on %q: unsupported scheme", listen)
}

View file

@ -100,6 +100,9 @@ The following directives are supported:
port: 113)
- _http+prometheus://localhost:<port>_ listens for plain-text HTTP
connections and serves Prometheus metrics (host must be "localhost")
- _http+pprof://localhost:<port>_ listens for plain-text HTTP connections
and serves pprof runtime profiling data (host must be "localhost"). For
more information, see: <https://pkg.go.dev/net/http/pprof>.
If the scheme is omitted, "ircs" is assumed. If multiple *listen*
directives are specified, soju will listen on each of them.