-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rate limiting for async delivery by using a goroutine with consum…
…er channel
- Loading branch information
1 parent
245bce0
commit 3125968
Showing
4 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,106 @@ | ||
package bugsnag | ||
|
||
import "fmt" | ||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
type reportPublisher interface { | ||
publishReport(*payload) error | ||
setMainProgramContext(context.Context) | ||
} | ||
|
||
type defaultReportPublisher struct{} | ||
var mainProgramDone = false | ||
|
||
func (*defaultReportPublisher) publishReport(p *payload) error { | ||
func (defPub *defaultReportPublisher) delivery() { | ||
signalsCh := make(chan os.Signal, 1) | ||
signal.Notify(signalsCh, syscall.SIGINT, syscall.SIGTERM) | ||
closeDelivery := false | ||
|
||
waitForEnd: | ||
for { | ||
select { | ||
case <-signalsCh: | ||
mainProgramDone = true | ||
break waitForEnd | ||
case <-defPub.mainProgramCtx.Done(): | ||
mainProgramDone = true | ||
break waitForEnd | ||
case p, ok := <-defPub.eventsChan: | ||
if ok { | ||
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) | ||
} | ||
} else { | ||
p.logf("Event channel closed") | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Send remaining elements from the queue | ||
for !closeDelivery { | ||
select { | ||
case p, ok := <-defPub.eventsChan: | ||
if ok { | ||
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) | ||
} | ||
} else { | ||
p.logf("Event channel closed") | ||
return | ||
} | ||
default: | ||
if mainProgramDone && len(defPub.eventsChan) == 0 { | ||
fmt.Println("Main program done and event channel empty") | ||
closeDelivery = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
type defaultReportPublisher struct{ | ||
eventsChan chan *payload | ||
mainProgramCtx context.Context | ||
} | ||
|
||
func NewPublisher() *defaultReportPublisher { | ||
defPub := defaultReportPublisher{} | ||
defPub.eventsChan = make(chan *payload, 100) | ||
defPub.mainProgramCtx = context.TODO() | ||
|
||
go defPub.delivery() | ||
|
||
return &defPub | ||
} | ||
|
||
func (defPub *defaultReportPublisher) setMainProgramContext(ctx context.Context) { | ||
defPub.mainProgramCtx = ctx | ||
} | ||
|
||
func (defPub *defaultReportPublisher) publishReport(p *payload) error { | ||
p.logf("notifying bugsnag: %s", p.Message) | ||
if !p.notifyInReleaseStage() { | ||
return fmt.Errorf("not notifying in %s", p.ReleaseStage) | ||
} | ||
if p.Synchronous { | ||
return p.deliver() | ||
} | ||
} else { | ||
if (mainProgramDone) { | ||
return fmt.Errorf("main program is stopping, new events won't be sent") | ||
} | ||
|
||
go func(p *payload) { | ||
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) | ||
select { | ||
case defPub.eventsChan <- p: | ||
default: | ||
p.logf("Events channel full. Discarding value") | ||
} | ||
}(p) | ||
} | ||
|
||
return nil | ||
} |