Log notification subscription lifecycle and websocket rejections
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -17,6 +20,30 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
func TestAppNotificationSubscriptionLogsLifecycle(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
f := newAppNotificationFixture(t, ctx)
|
||||
var output bytes.Buffer
|
||||
previous := log.Writer()
|
||||
log.SetOutput(&output)
|
||||
t.Cleanup(func() { log.SetOutput(previous) })
|
||||
|
||||
phone := dialAckClient(t, ctx, f.url)
|
||||
subscribeNotifications(t, ctx, phone, "cursor-1")
|
||||
if !strings.Contains(output.String(), `notification subscription started request_id="sub" cursor="cursor-1"`) {
|
||||
t.Fatalf("subscription start log = %q", output.String())
|
||||
}
|
||||
phone.CloseNow()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) && time.Now().Before(deadline) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) || !strings.Contains(output.String(), "reason=client closed") {
|
||||
t.Fatalf("subscription end log = %q", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
// appNotificationFixture is a handler over a temp store with one pipeline,
|
||||
// plus helpers to make a run end with a durable notification intent.
|
||||
type appNotificationFixture struct {
|
||||
|
||||
Reference in New Issue
Block a user