1
0
mirror of https://github.com/google/gops.git synced 2024-11-19 20:31:58 +02:00

agent: don't close on interrupt by default (#49)

This commit is contained in:
JBD 2017-09-11 19:50:16 -07:00 committed by GitHub
parent d38055cdcb
commit 1a41b817e5
3 changed files with 12 additions and 12 deletions

View File

@ -43,10 +43,11 @@ type Options struct {
// Optional. // Optional.
Addr string Addr string
// NoShutdownCleanup tells the agent not to automatically cleanup // ShutdownCleanup automatically cleans up resources if the
// resources if the running process receives an interrupt. // running process receives an interrupt. Otherwise, users
// can call Close before shutting down.
// Optional. // Optional.
NoShutdownCleanup bool ShutdownCleanup bool
} }
// Listen starts the gops agent on a host process. Once agent started, users // 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 // 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 // any program on the system. Review your security requirements before starting
// the agent. // the agent.
func Listen(opts *Options) error { func Listen(opts Options) error {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if opts == nil {
opts = &Options{}
}
if portfile != "" { if portfile != "" {
return fmt.Errorf("gops: agent already listening at: %v", listener.Addr()) return fmt.Errorf("gops: agent already listening at: %v", listener.Addr())
} }
@ -77,7 +75,7 @@ func Listen(opts *Options) error {
if err != nil { if err != nil {
return err return err
} }
if !opts.NoShutdownCleanup { if opts.ShutdownCleanup {
gracefulShutdown() gracefulShutdown()
} }

View File

@ -10,7 +10,7 @@ import (
) )
func TestListen(t *testing.T) { func TestListen(t *testing.T) {
err := Listen(nil) err := Listen(Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -18,7 +18,7 @@ func TestListen(t *testing.T) {
} }
func TestAgentClose(t *testing.T) { func TestAgentClose(t *testing.T) {
err := Listen(nil) err := Listen(Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -33,7 +33,7 @@ func TestAgentClose(t *testing.T) {
} }
func TestAgentListenMultipleClose(t *testing.T) { func TestAgentListenMultipleClose(t *testing.T) {
err := Listen(nil) err := Listen(Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -12,7 +12,9 @@ import (
) )
func main() { 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) log.Fatal(err)
} }
time.Sleep(time.Hour) time.Sleep(time.Hour)