Skip to content

Commit 6ef6a40

Browse files
committed
Provide a Close mechanism to clean up the default transport goroutine
Fixes #48 By default when you import rollbar we create an async transport as part of a default client so that the top level functions for reporting items just work. However, that leads to a goroutine that dangles if you don't actually use anything or don't want to use that standard client. This PR adds `Close` to the top level to call Close on the standard client. We also make `Close` close the underlying channel which is used for communication to the goroutine used to process items. As a consequence, once all items have been sent this goroutine will shutdown. So if you never use it, Close will effectively just shut the goroutine down.
1 parent eb32617 commit 6ef6a40

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

async_transport.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (t *AsyncTransport) Wait() {
107107

108108
// Close is an alias for Wait for the asynchronous transport
109109
func (t *AsyncTransport) Close() error {
110+
close(t.bodyChannel)
110111
t.Wait()
111112
return nil
112113
}

rollbar.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ func Wait() {
506506
std.Wait()
507507
}
508508

509+
// Close will block until the queue of errors / messages is empty and terminate the goroutine used
510+
// for sending items.
511+
func Close() {
512+
std.Close()
513+
}
514+
509515
// Wrap calls f and then recovers and reports a panic to Rollbar if it occurs.
510516
// If an error is captured it is subsequently returned.
511517
func Wrap(f func()) interface{} {

rollbar_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestEverythingGeneric(t *testing.T) {
139139
"hello": "request",
140140
})
141141

142-
Wait()
142+
Close()
143143
}
144144

145145
func TestBuildBody(t *testing.T) {

stack_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func TestBuildStack(t *testing.T) {
99
frame := buildStack(getCallersFrames(0))[0]
1010

11-
if !strings.HasSuffix(frame.Filename,"rollbar-go/stack_test.go") {
11+
if !strings.HasSuffix(frame.Filename, "rollbar-go/stack_test.go") {
1212
t.Errorf("got: %s", frame.Filename)
1313
}
1414
if frame.Method != "rollbar-go.TestBuildStack" {

transforms.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func errorBody(configuration configuration, err error, skip int) (map[string]int
196196
traceChain := []map[string]interface{}{}
197197
fingerprint := ""
198198
for {
199-
stack := buildStack(getOrBuildFrames(err, parent, 1 + skip))
199+
stack := buildStack(getOrBuildFrames(err, parent, 1+skip))
200200
traceChain = append(traceChain, buildTrace(err, stack))
201201
if configuration.fingerprint {
202202
fingerprint = fingerprint + stack.Fingerprint()
@@ -247,7 +247,7 @@ func getOrBuildFrames(err error, parent error, skip int) []runtime.Frame {
247247

248248
func getCallersFrames(skip int) []runtime.Frame {
249249
pc := make([]uintptr, 100)
250-
runtime.Callers(2 + skip, pc)
250+
runtime.Callers(2+skip, pc)
251251
fr := runtime.CallersFrames(pc)
252252
frames := make([]runtime.Frame, 0)
253253

0 commit comments

Comments
 (0)