Skip to content

Commit

Permalink
Move starting delivery goroutine to configure, remove subscribing to …
Browse files Browse the repository at this point in the history
…signals
  • Loading branch information
DariaKunoichi committed Oct 15, 2024
1 parent f6b701e commit b642b65
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
3 changes: 3 additions & 0 deletions v2/bugsnag.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func Configure(config Configuration) {
readEnvConfigOnce.Do(Config.loadEnv)
Config.update(&config)
updateSessionConfig()

// start delivery goroutine later than the module import time
go publisher.delivery()
// Only do once in case the user overrides the default panichandler, and
// configures multiple times.
panicHandlerOnce.Do(Config.PanicHandler)
Expand Down
3 changes: 3 additions & 0 deletions v2/bugsnag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (tp *testPublisher) publishReport(p *payload) error {
func (tp *testPublisher) setMainProgramContext(context.Context) {
}

func (tp *testPublisher) delivery() {
}

func TestNotifySyncThenAsync(t *testing.T) {
ts, _ := setup()
defer ts.Close()
Expand Down
20 changes: 5 additions & 15 deletions v2/report_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@ package bugsnag
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
)

type reportPublisher interface {
publishReport(*payload) error
setMainProgramContext(context.Context)
delivery()
}

func (defPub *defaultReportPublisher) delivery() {
signalsCh := make(chan os.Signal, 1)
signal.Notify(signalsCh, syscall.SIGINT, syscall.SIGTERM)

waitForEnd:
for {
select {
case <-signalsCh:
defPub.isClosing = true
break waitForEnd
case <-defPub.mainProgramCtx.Done():
defPub.isClosing = true
break waitForEnd
Expand All @@ -42,10 +34,10 @@ waitForEnd:
// Send remaining elements from the queue
close(defPub.eventsChan)
for p := range defPub.eventsChan {
if err := p.deliver(); err != nil {
// Ensure that any errors are logged if they occur in a goroutine.
p.logf("bugsnag/defaultReportPublisher.publishReport: %v", err)
}
if err := p.deliver(); err != nil {
// Ensure that any errors are logged if they occur in a goroutine.
p.logf("bugsnag/defaultReportPublisher.publishReport: %v", err)
}
}
}

Expand All @@ -59,8 +51,6 @@ func newPublisher() reportPublisher {
defPub := defaultReportPublisher{isClosing: false, mainProgramCtx: context.TODO()}
defPub.eventsChan = make(chan *payload, 100)

go defPub.delivery()

return &defPub
}

Expand Down

0 comments on commit b642b65

Please sign in to comment.