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

Handle channel closure gracefully without panicing #427

Merged
merged 1 commit into from
Nov 10, 2023
Merged
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
30 changes: 22 additions & 8 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,19 @@ func (em *EventManager) broadcastEvent(evt *XRPCStreamEvent) {
default:
log.Warnw("dropping slow consumer due to event overflow", "bufferSize", len(s.outgoing), "ident", s.ident)
go func(torem *Subscriber) {
select {
case torem.outgoing <- &XRPCStreamEvent{
Error: &ErrorFrame{
Error: "ConsumerTooSlow",
},
}:
case <-time.After(time.Second * 5):
log.Warnw("failed to send error frame to backed up consumer", "ident", torem.ident)
torem.lk.Lock()
if !torem.cleanedUp {
select {
case torem.outgoing <- &XRPCStreamEvent{
Error: &ErrorFrame{
Error: "ConsumerTooSlow",
},
}:
case <-time.After(time.Second * 5):
log.Warnw("failed to send error frame to backed up consumer", "ident", torem.ident)
}
}
torem.lk.Unlock()
torem.cleanup()
}(s)
}
Expand All @@ -112,6 +116,9 @@ type Subscriber struct {

cleanup func()

lk sync.Mutex
cleanedUp bool

ident string
enqueuedCounter prometheus.Counter
broadcastCounter prometheus.Counter
Expand Down Expand Up @@ -177,9 +184,12 @@ func (em *EventManager) Subscribe(ctx context.Context, ident string, filter func
}

sub.cleanup = sync.OnceFunc(func() {
sub.lk.Lock()
defer sub.lk.Unlock()
close(done)
em.rmSubscriber(sub)
close(sub.outgoing)
sub.cleanedUp = true
})

if since == nil {
Expand Down Expand Up @@ -260,6 +270,8 @@ func (em *EventManager) Subscribe(ctx context.Context, ident string, filter func

func sequenceForEvent(evt *XRPCStreamEvent) int64 {
switch {
case evt == nil:
return -1
case evt.RepoCommit != nil:
return evt.RepoCommit.Seq
case evt.RepoHandle != nil:
Expand All @@ -270,6 +282,8 @@ func sequenceForEvent(evt *XRPCStreamEvent) int64 {
return evt.RepoTombstone.Seq
case evt.RepoInfo != nil:
return -1
case evt.Error != nil:
return -1
default:
return -1
}
Expand Down