From 1a41b817e576ae667efe0d6b0ee57a0a35833789 Mon Sep 17 00:00:00 2001 From: JBD Date: Mon, 11 Sep 2017 19:50:16 -0700 Subject: [PATCH] agent: don't close on interrupt by default (#49) --- agent/agent.go | 14 ++++++-------- agent/agent_test.go | 6 +++--- examples/hello/main.go | 4 +++- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 5708391..563c4ba 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -43,10 +43,11 @@ type Options struct { // Optional. Addr string - // NoShutdownCleanup tells the agent not to automatically cleanup - // resources if the running process receives an interrupt. + // ShutdownCleanup automatically cleans up resources if the + // running process receives an interrupt. Otherwise, users + // can call Close before shutting down. // Optional. - NoShutdownCleanup bool + ShutdownCleanup bool } // Listen starts the gops agent on a host process. Once agent started, users @@ -58,13 +59,10 @@ type Options struct { // Note: The agent exposes an endpoint via a TCP connection that can be used by // any program on the system. Review your security requirements before starting // the agent. -func Listen(opts *Options) error { +func Listen(opts Options) error { mu.Lock() defer mu.Unlock() - if opts == nil { - opts = &Options{} - } if portfile != "" { return fmt.Errorf("gops: agent already listening at: %v", listener.Addr()) } @@ -77,7 +75,7 @@ func Listen(opts *Options) error { if err != nil { return err } - if !opts.NoShutdownCleanup { + if opts.ShutdownCleanup { gracefulShutdown() } diff --git a/agent/agent_test.go b/agent/agent_test.go index cc2a440..a416322 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -10,7 +10,7 @@ import ( ) func TestListen(t *testing.T) { - err := Listen(nil) + err := Listen(Options{}) if err != nil { t.Fatal(err) } @@ -18,7 +18,7 @@ func TestListen(t *testing.T) { } func TestAgentClose(t *testing.T) { - err := Listen(nil) + err := Listen(Options{}) if err != nil { t.Fatal(err) } @@ -33,7 +33,7 @@ func TestAgentClose(t *testing.T) { } func TestAgentListenMultipleClose(t *testing.T) { - err := Listen(nil) + err := Listen(Options{}) if err != nil { t.Fatal(err) } diff --git a/examples/hello/main.go b/examples/hello/main.go index 2ee3479..2225684 100644 --- a/examples/hello/main.go +++ b/examples/hello/main.go @@ -12,7 +12,9 @@ import ( ) func main() { - if err := agent.Listen(nil); err != nil { + if err := agent.Listen(agent.Options{ + ShutdownCleanup: true, // automatically closes on os.Interrupt + }); err != nil { log.Fatal(err) } time.Sleep(time.Hour)