diff options
| -rw-r--r-- | main.go | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -4,6 +4,8 @@ import ( "flag" "fmt" "log" + "runtime/pprof" + "strings" "sync" "time" ) @@ -24,7 +26,9 @@ func main() { } // Launch the game! - game(*numSecs, *numPlayers) + getLeakProfile(func() { + game(*numSecs, *numPlayers) + }) } func game(numSecs, numPlayers int) { @@ -95,3 +99,24 @@ func player(id int, wg *sync.WaitGroup, input chan Ball) chan Ball { return out } + +// getLeakProfile runs a leaky program snippet, extracts the goroutine +// leak profile, and writes it to stdout. +func getLeakProfile(leakySnippet func()) { + prof := pprof.Lookup("goroutineleak") + defer func() { + time.Sleep(2 * time.Second) + var content strings.Builder + + prof.WriteTo(&content, 2) + // Ignore non leaked goroutines + leaks := strings.Split(content.String(), "\n\n") + for _, leak := range leaks { + if strings.Contains(leak, "(leaked)") { + fmt.Println(leak + "\n") + } + } + }() + + leakySnippet() +} |
