Browse Source

Merge pull request #210 from slingamn/pprof.2

add an option to expose a pprof http endpoint
tags/v0.11.0-beta
Shivaram Lingamneni 6 years ago
parent
commit
c6124f9d8a
No account linked to committer's email address
3 changed files with 39 additions and 1 deletions
  1. 2
    1
      irc/config.go
  2. 31
    0
      irc/server.go
  3. 6
    0
      oragono.yaml

+ 2
- 1
irc/config.go View File

@@ -237,7 +237,8 @@ type Config struct {
237 237
 	Logging []logger.LoggingConfig
238 238
 
239 239
 	Debug struct {
240
-		RecoverFromErrors *bool `yaml:"recover-from-errors"`
240
+		RecoverFromErrors *bool   `yaml:"recover-from-errors"`
241
+		PprofListener     *string `yaml:"pprof-listener"`
241 242
 		StackImpact       StackImpactConfig
242 243
 	}
243 244
 

+ 31
- 0
irc/server.go View File

@@ -13,6 +13,8 @@ import (
13 13
 	"log"
14 14
 	"math/rand"
15 15
 	"net"
16
+	"net/http"
17
+	_ "net/http/pprof"
16 18
 	"os"
17 19
 	"os/signal"
18 20
 	"strconv"
@@ -120,6 +122,7 @@ type Server struct {
120 122
 	recoverFromErrors          bool
121 123
 	rehashMutex                sync.Mutex // tier 4
122 124
 	rehashSignal               chan os.Signal
125
+	pprofServer                *http.Server
123 126
 	proxyAllowedFrom           []string
124 127
 	signals                    chan os.Signal
125 128
 	snomasks                   *SnoManager
@@ -968,6 +971,8 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
968 971
 		}
969 972
 	}
970 973
 
974
+	server.setupPprofListener(config)
975
+
971 976
 	// we are now open for business
972 977
 	server.setupListeners(config)
973 978
 
@@ -987,6 +992,32 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
987 992
 	return nil
988 993
 }
989 994
 
995
+func (server *Server) setupPprofListener(config *Config) {
996
+	pprofListener := ""
997
+	if config.Debug.PprofListener != nil {
998
+		pprofListener = *config.Debug.PprofListener
999
+	}
1000
+	if server.pprofServer != nil {
1001
+		if pprofListener == "" || (pprofListener != server.pprofServer.Addr) {
1002
+			server.logger.Info("rehash", "Stopping pprof listener", server.pprofServer.Addr)
1003
+			server.pprofServer.Close()
1004
+			server.pprofServer = nil
1005
+		}
1006
+	}
1007
+	if pprofListener != "" && server.pprofServer == nil {
1008
+		ps := http.Server{
1009
+			Addr: pprofListener,
1010
+		}
1011
+		go func() {
1012
+			if err := ps.ListenAndServe(); err != nil {
1013
+				server.logger.Error("rehash", fmt.Sprintf("pprof listener failed: %v", err))
1014
+			}
1015
+		}()
1016
+		server.pprofServer = &ps
1017
+		server.logger.Info("rehash", "Started pprof listener", server.pprofServer.Addr)
1018
+	}
1019
+}
1020
+
990 1021
 func (server *Server) loadMOTD(motdPath string, useFormatting bool) error {
991 1022
 	server.logger.Info("rehash", "Using MOTD", motdPath)
992 1023
 	motdLines := make([]string, 0)

+ 6
- 0
oragono.yaml View File

@@ -316,6 +316,12 @@ debug:
316 316
     # this to false.
317 317
     recover-from-errors: true
318 318
 
319
+    # optionally expose a pprof http endpoint: https://golang.org/pkg/net/http/pprof/
320
+    # it is strongly recommended that you don't expose this on a public interface;
321
+    # if you need to access it remotely, you can use an SSH tunnel.
322
+    # set to `null`, "", leave blank, or omit to disable
323
+    # pprof-listener: "localhost:6060"
324
+
319 325
     # enabling StackImpact profiling
320 326
     stackimpact:
321 327
         # whether to use StackImpact

Loading…
Cancel
Save