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

drop consumers if their out buffer fills up #417

Merged
merged 5 commits into from
Nov 9, 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
26 changes: 19 additions & 7 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"sync"
"time"

comatproto "github.com/bluesky-social/indigo/api/atproto"
label "github.com/bluesky-social/indigo/api/label"
Expand Down Expand Up @@ -73,11 +74,20 @@ func (em *EventManager) broadcastEvent(evt *XRPCStreamEvent) {
select {
case s.outgoing <- evt:
case <-s.done:
default:
log.Warnw("dropping slow consumer due to event overflow", "bufferSize", len(s.outgoing), "ident", s.ident)
go func(torem *Subscriber) {
em.rmSubscriber(torem)
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.cleanup()
ericvolp12 marked this conversation as resolved.
Show resolved Hide resolved
}(s)
default:
log.Warnf("event overflow (%d)", len(s.outgoing))
}
s.broadcastCounter.Inc()
}
Expand All @@ -100,6 +110,8 @@ type Subscriber struct {

done chan struct{}

cleanup func()

ident string
enqueuedCounter prometheus.Counter
broadcastCounter prometheus.Counter
Expand Down Expand Up @@ -164,15 +176,15 @@ func (em *EventManager) Subscribe(ctx context.Context, ident string, filter func
broadcastCounter: eventsBroadcast.WithLabelValues(ident),
}

cleanup := func() {
sub.cleanup = sync.OnceFunc(func() {
close(done)
em.rmSubscriber(sub)
close(sub.outgoing)
}
})

if since == nil {
em.addSubscriber(sub)
return sub.outgoing, cleanup, nil
return sub.outgoing, sub.cleanup, nil
}

out := make(chan *XRPCStreamEvent, em.bufferSize)
Expand Down Expand Up @@ -243,7 +255,7 @@ func (em *EventManager) Subscribe(ctx context.Context, ident string, filter func
}
}()

return out, cleanup, nil
return out, sub.cleanup, nil
}

func sequenceForEvent(evt *XRPCStreamEvent) int64 {
Expand Down
Loading