Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move start of delivery goroutine to configure, don't wait on signals in delivery #250

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading