Skip to content

Commit

Permalink
Add pprof with buildflags, not runtime flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 29, 2024
1 parent 18c739b commit 81bda39
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ MODULE_NAME = github.com/bahner/go-ma-actor
export VERSION = "v0.0.2"

GO ?= go
GOOPTS ?= -ldflags="-s -w"
BUILDFLAGS ?= -ldflags="-s -w"
PREFIX ?= /usr/local
KEYSET = go-ma-create-keyset
FETCH = go-ma-fetch-document
ALL = $(FETCH) $(KEYSET) $(NAME)
KEYSET = $(NAME)-create-keyset
FETCH = $(NAME)-fetch-document
DEBUG = $(NAME)-debug
ALL = $(FETCH) $(KEYSET) $(NAME) $(DEBUG)
BIN = $(PREFIX)/bin

ifneq (,$(wildcard ./.env))
Expand All @@ -25,14 +26,18 @@ $(BIN): $(ALL)
test -d $(BIN)
sudo install -m755 $(ALL) $(DESTDIR)$(BIN)

$(DEBUG): BUILDFLAGS = -tags=debug
$(DEBUG): tidy
$(GO) build -o $(DEBUG) $(BUILDFLAGS) ./cmd/actor

$(NAME): tidy
$(GO) build -o $(NAME) $(GOOPTS) ./cmd/actor
$(GO) build -o $(NAME) $(BUILDFLAGS) ./cmd/actor

$(FETCH): tidy
$(GO) build -o $(FETCH) $(GOOPTS) ./cmd/fetch_document
$(GO) build -o $(FETCH) $(BUILDFLAGS) ./cmd/fetch_document

$(KEYSET): tidy
$(GO) build -o $(KEYSET) $(GOOPTS) ./cmd/create_keyset
$(GO) build -o $(KEYSET) $(BUILDFLAGS) ./cmd/create_keyset

init: go.mod tidy

Expand Down
26 changes: 26 additions & 0 deletions cmd/actor/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build debug

package main

import (
"net/http"
_ "net/http/pprof"
"runtime"
)

func init() {
// Assume you have a function setupDebugHandlers to register debug routes
setupDebugHandlers()
}

func setupDebugHandlers() {
// Register your pprof handlers or other debug routes here
// Since "net/http/pprof" is imported above, its init function automatically registers its routes with the default mux
go http.ListenAndServe("localhost:6060", nil)

http.HandleFunc("/force-gc", func(w http.ResponseWriter, r *http.Request) {
// Force a garbage collection
runtime.GC()
w.Write([]byte("Garbage collection triggered"))
})
}
6 changes: 0 additions & 6 deletions cmd/actor/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"net/http"
"net/http/pprof"
"os"

"github.com/bahner/go-ma-actor/config"
Expand Down Expand Up @@ -39,11 +38,6 @@ func startWebServer(p *p2p.P2P, a *actor.Actor) {
mux.HandleFunc("/", h.WebHandler)
}

// Add pprof handlers when debug mode is set
if config.DebugMode() {
mux.HandleFunc("/debug/pprof", pprof.Profile)
}

log.Infof("Listening on %s", config.GetHttpSocket())

// IN relay mode we want to stop here.
Expand Down
1 change: 0 additions & 1 deletion config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func generateConfigFile(actor string, node string) {
"discovery-timeout": defaultDiscoveryTimeout,
},
"mode": map[string]interface{}{
"debug": defaultDebugMode,
"relay": defaultRelayMode,
"pong": map[string]interface{}{
"reply": DefaultPongReply,
Expand Down
8 changes: 0 additions & 8 deletions config/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const (

defaultPongMode = false
defaultRelayMode = false
defaultDebugMode = false

defaultMode = "actor"
)
Expand All @@ -23,9 +22,6 @@ func init() {
pflag.Bool("relay", defaultRelayMode, "Relay mode with no actor, to just listen and relay messages.")
viper.BindPFlag("mode.relay", pflag.Lookup("relay"))

pflag.Bool("debug", defaultDebugMode, "Enable debug mode. Adds pprof and other debug endpoints to the webserver.")
viper.BindPFlag("mode.debug", pflag.Lookup("debug"))

}

// Returns the mode of the actor as as a string, eg. "actor", "pong", "relay".
Expand All @@ -51,10 +47,6 @@ func InitMode() string {
return defaultMode
}

func DebugMode() bool {
return viper.GetBool("mode.debug")
}

// If actor.home is set to pong, then we are in pong mode.
// THIs means that we don't render the ui and reply automatically to messages.
func PongMode() bool {
Expand Down
4 changes: 3 additions & 1 deletion ui/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ui

func (ui *ChatUI) triggerDiscovery() {

ui.p.DiscoverPeers()
ui.displaySystemMessage("Discovery process started...")
ui.p.DiscoverPeers()
ui.displaySystemMessage("Discovery process complete.")

}

func (ui *ChatUI) handleHelpDiscoverCommand(args []string) {
Expand Down

0 comments on commit 81bda39

Please sign in to comment.