Skip to content

Commit 1a41b81

Browse files
author
JBD
authored
agent: don't close on interrupt by default (#49)
1 parent d38055c commit 1a41b81

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

agent/agent.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ type Options struct {
4343
// Optional.
4444
Addr string
4545

46-
// NoShutdownCleanup tells the agent not to automatically cleanup
47-
// resources if the running process receives an interrupt.
46+
// ShutdownCleanup automatically cleans up resources if the
47+
// running process receives an interrupt. Otherwise, users
48+
// can call Close before shutting down.
4849
// Optional.
49-
NoShutdownCleanup bool
50+
ShutdownCleanup bool
5051
}
5152

5253
// Listen starts the gops agent on a host process. Once agent started, users
@@ -58,13 +59,10 @@ type Options struct {
5859
// Note: The agent exposes an endpoint via a TCP connection that can be used by
5960
// any program on the system. Review your security requirements before starting
6061
// the agent.
61-
func Listen(opts *Options) error {
62+
func Listen(opts Options) error {
6263
mu.Lock()
6364
defer mu.Unlock()
6465

65-
if opts == nil {
66-
opts = &Options{}
67-
}
6866
if portfile != "" {
6967
return fmt.Errorf("gops: agent already listening at: %v", listener.Addr())
7068
}
@@ -77,7 +75,7 @@ func Listen(opts *Options) error {
7775
if err != nil {
7876
return err
7977
}
80-
if !opts.NoShutdownCleanup {
78+
if opts.ShutdownCleanup {
8179
gracefulShutdown()
8280
}
8381

agent/agent_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
)
1111

1212
func TestListen(t *testing.T) {
13-
err := Listen(nil)
13+
err := Listen(Options{})
1414
if err != nil {
1515
t.Fatal(err)
1616
}
1717
Close()
1818
}
1919

2020
func TestAgentClose(t *testing.T) {
21-
err := Listen(nil)
21+
err := Listen(Options{})
2222
if err != nil {
2323
t.Fatal(err)
2424
}
@@ -33,7 +33,7 @@ func TestAgentClose(t *testing.T) {
3333
}
3434

3535
func TestAgentListenMultipleClose(t *testing.T) {
36-
err := Listen(nil)
36+
err := Listen(Options{})
3737
if err != nil {
3838
t.Fatal(err)
3939
}

examples/hello/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
)
1313

1414
func main() {
15-
if err := agent.Listen(nil); err != nil {
15+
if err := agent.Listen(agent.Options{
16+
ShutdownCleanup: true, // automatically closes on os.Interrupt
17+
}); err != nil {
1618
log.Fatal(err)
1719
}
1820
time.Sleep(time.Hour)

0 commit comments

Comments
 (0)