Log notification subscription lifecycle and websocket rejections

This commit is contained in:
2026-11-12 02:13:50 +00:00
parent e4f3ebdcab
commit 903fda5f32
4 changed files with 138 additions and 9 deletions
+29 -9
View File
@@ -39,25 +39,37 @@ type appNotifier struct {
}
type appNotificationSub struct {
queue chan protocol.Message
done chan struct{}
once sync.Once
ctx context.Context
requestID string
started time.Time
queue chan protocol.Message
done chan struct{}
once sync.Once
ended sync.Once
}
func newAppNotifier(store appNotificationStore) *appNotifier {
return &appNotifier{store: store, now: time.Now, subs: map[string]*appNotificationSub{}}
}
func (s *appNotificationSub) stop() { s.once.Do(func() { close(s.done) }) }
func (s *appNotificationSub) stop(reason string) {
s.once.Do(func() { close(s.done) })
s.ended.Do(func() {
log.Printf("notification subscription ended request_id=%q duration=%s reason=%s", s.requestID, time.Since(s.started), reason)
})
}
func (s *appNotificationSub) run(write func(protocol.Message) error) {
for {
select {
case <-s.done:
return
case <-s.ctx.Done():
s.stop("server stopping")
return
case msg := <-s.queue:
if write(msg) != nil {
s.stop()
s.stop("write error")
return
}
}
@@ -77,7 +89,7 @@ func (a *appNotifier) enqueueLocked(msg protocol.Message, exclude string) {
delete(a.subs, id)
default:
log.Printf("app notifications: dropping slow subscriber %s", id)
sub.stop()
sub.stop("write error")
delete(a.subs, id)
}
}
@@ -93,11 +105,15 @@ func (a *appNotifier) subscribe(ctx context.Context, clientID, requestID, after
return err
}
if old := a.subs[clientID]; old != nil {
old.stop()
old.stop("client closed")
}
sub := &appNotificationSub{
ctx: ctx, requestID: requestID, started: time.Now(),
queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{}),
}
sub := &appNotificationSub{queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{})}
sub.queue <- protocol.Message{Type: protocol.NotificationsSubscribed, ID: requestID, Notifications: active, IDs: cleared}
a.subs[clientID] = sub
log.Printf("notification subscription started request_id=%q cursor=%q", requestID, after)
go sub.run(write)
return nil
}
@@ -106,7 +122,11 @@ func (a *appNotifier) detach(clientID string) {
a.mu.Lock()
defer a.mu.Unlock()
if sub := a.subs[clientID]; sub != nil {
sub.stop()
reason := "client closed"
if sub.ctx.Err() != nil {
reason = "server stopping"
}
sub.stop(reason)
delete(a.subs, clientID)
}
}