Chat and runs: tell a slow client to resync instead of dropping it silently; join queued deltas
A chat subscriber whose queue fills is detached and its writer ends the stream with a resync frame (the app re-attaches and re-reads history); a run subscriber gets a final runs_resync and the app subscribes again and re-reads the Runs list and open detail. Consecutive text/reasoning deltas of one part are joined in a chat subscriber's queue; joined numbered frames carry seq_from and delta_lens so the app trims exactly the prefix a history page already holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -90,6 +91,11 @@ type runSubscription struct {
|
||||
mu sync.Mutex
|
||||
snapshotting bool
|
||||
pending []protocol.Message
|
||||
|
||||
// overflowed is closed once the subscriber fell too far behind: it is
|
||||
// already detached, and its writer ends the stream with runs_resync.
|
||||
overflowed chan struct{}
|
||||
overOnce sync.Once
|
||||
}
|
||||
|
||||
func newRunEventHub() *runEventHub {
|
||||
@@ -129,6 +135,7 @@ func (h *runEventHub) subscribe(clientID, workspace string, runIDs []int64, writ
|
||||
workspace: workspace,
|
||||
ch: make(chan protocol.Message, runEventSubscriberBuffer),
|
||||
done: make(chan struct{}),
|
||||
overflowed: make(chan struct{}),
|
||||
write: write,
|
||||
snapshotting: true,
|
||||
}
|
||||
@@ -378,9 +385,19 @@ func (s *runSubscription) matches(workspace string, runID int64) bool {
|
||||
|
||||
func (s *runSubscription) run() {
|
||||
for {
|
||||
select {
|
||||
case <-s.overflowed:
|
||||
// The terminal frame: the frames still queued are discarded, so
|
||||
// the client must subscribe again and re-read the runs.
|
||||
_ = s.write(protocol.Message{Type: protocol.RunsResync})
|
||||
s.close()
|
||||
return
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-s.done:
|
||||
return
|
||||
case <-s.overflowed:
|
||||
case msg := <-s.ch:
|
||||
if err := s.write(msg); err != nil {
|
||||
s.close()
|
||||
@@ -450,7 +467,7 @@ func (s *runSubscription) sendMessage(msg protocol.Message) {
|
||||
// the snapshot and the ack are written directly by the subscriber.
|
||||
if len(s.pending) >= runEventSubscriberBuffer {
|
||||
s.mu.Unlock()
|
||||
s.close()
|
||||
s.overflow()
|
||||
return
|
||||
}
|
||||
s.pending = append(s.pending, msg)
|
||||
@@ -461,12 +478,24 @@ func (s *runSubscription) sendMessage(msg protocol.Message) {
|
||||
select {
|
||||
case s.ch <- msg:
|
||||
default:
|
||||
// The subscriber is not draining: drop it rather than block the
|
||||
// producer. Its next subscribe re-syncs from a fresh snapshot.
|
||||
s.close()
|
||||
// The subscriber is not draining: detach it rather than block the
|
||||
// producer. Its writer tells it to subscribe again for a fresh
|
||||
// snapshot.
|
||||
s.overflow()
|
||||
}
|
||||
}
|
||||
|
||||
// overflow detaches a subscriber that fell too far behind and lets its writer
|
||||
// (started by ready, if the snapshot is still being written) end the stream
|
||||
// with runs_resync.
|
||||
func (s *runSubscription) overflow() {
|
||||
s.overOnce.Do(func() {
|
||||
close(s.overflowed)
|
||||
log.Printf("run events: client %s fell %d frames behind; it is told to resync", s.clientID, runEventSubscriberBuffer)
|
||||
s.hub.remove(s)
|
||||
})
|
||||
}
|
||||
|
||||
// close unregisters the subscription and stops its writer goroutine exactly
|
||||
// once. It is safe to call from the writer goroutine, from unsubscribe and from
|
||||
// the disconnect path.
|
||||
|
||||
Reference in New Issue
Block a user