| 1 | package docs |
| 2 | |
| 3 | import "sync" |
| 4 | |
| 5 | // hub is this instance's in-process fan-out: one document's accepted |
| 6 | // update, announced to whoever currently has that document open. It |
| 7 | // is the "broadcast primitive" rastrillo's own SSE handler |
| 8 | // (jobs/events.go) says it deliberately did without — that one polls |
| 9 | // its store once a second because it had nothing to wait on, and |
| 10 | // this exists precisely so the stream handler here can wait instead |
| 11 | // of poll. |
| 12 | // |
| 13 | // Scope, deliberately: one process. Docs is one instance per team |
| 14 | // (the design spec's opening line, and its §3 "One SQLite file per |
| 15 | // instance"), so there is no second process to reach and nothing here |
| 16 | // needs a broker, a database channel, or a network hop. If Docs ever |
| 17 | // runs more than one process per team, this type is the seam that has |
| 18 | // to change, and nothing else does. |
| 19 | // |
| 20 | // What travels is a seq, and it is ADVISORY. It says "there is |
| 21 | // something at least this new"; it never says "here is what changed" |
| 22 | // and a subscriber must not treat it as a cursor assignment. That is |
| 23 | // what makes the lossy one-slot channel below correct rather than |
| 24 | // merely convenient: a subscriber that misses ten notifications still |
| 25 | // fetches ?since=<its own cursor> and receives all ten updates. |
| 26 | type hub struct { |
| 27 | mu sync.Mutex |
| 28 | subs map[int64]map[chan int64]struct{} |
| 29 | |
| 30 | // done is closed by drain, once, when the process is going away. |
| 31 | done chan struct{} |
| 32 | doneOnce sync.Once |
| 33 | } |
| 34 | |
| 35 | func newHub() *hub { |
| 36 | return &hub{subs: make(map[int64]map[chan int64]struct{}), done: make(chan struct{})} |
| 37 | } |
| 38 | |
| 39 | // drain ends every live stream, and refuses new ones, because the |
| 40 | // process is shutting down. net/http's graceful shutdown waits for every |
| 41 | // request in progress and cancels none of their contexts, so a stream — |
| 42 | // which ends only when its client leaves or its five-minute TTL runs out — |
| 43 | // held the shutdown for the whole of rastrillo's ten-second budget, and |
| 44 | // the process then exited 1 with "context deadline exceeded" (2026-10-26 |
| 45 | // 12:49, the platform stopping every instance on the box; Docs, with one |
| 46 | // editor open, was the only app that failed to stop cleanly). The |
| 47 | // listener closes as the shutdown begins, so for those ten seconds the |
| 48 | // instance took no new request either, and a save made then could not |
| 49 | // reach it, for a stop that should take milliseconds. A client |
| 50 | // whose stream ends this way reopens it on its next sign of activity |
| 51 | // (fixtures/live-updates.mjs), from wherever the instance is by then. |
| 52 | func (h *hub) drain() { |
| 53 | h.doneOnce.Do(func() { close(h.done) }) |
| 54 | } |
| 55 | |
| 56 | // draining is closed once drain has been called. |
| 57 | func (h *hub) draining() <-chan struct{} { return h.done } |
| 58 | |
| 59 | // isDraining reports whether drain has been called. |
| 60 | func (h *hub) isDraining() bool { |
| 61 | select { |
| 62 | case <-h.done: |
| 63 | return true |
| 64 | default: |
| 65 | return false |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // subscribe registers interest in docID and returns the channel to |
| 70 | // wait on plus the function that unregisters it. The channel is |
| 71 | // buffered one deep: it carries "something happened", not a queue of |
| 72 | // everything that did. |
| 73 | // |
| 74 | // The returned stop is idempotent, because its caller (the stream |
| 75 | // handler) defers it and may also have reason to stop early. Calling |
| 76 | // it twice, or after the hub has forgotten the document, is a no-op |
| 77 | // rather than a panic on a closed channel. |
| 78 | // |
| 79 | // stop does not close the channel at all, and the honest reason is |
| 80 | // narrower than "otherwise it would race a broadcast": it would not, |
| 81 | // today. broadcast holds h.mu from the map lookup through the send |
| 82 | // and stop takes the same mutex, so a send to a channel this function |
| 83 | // has already removed cannot happen, and closing under the lock would |
| 84 | // be safe as the code stands. Not closing is still the better choice, |
| 85 | // because it stays correct if those sends ever move out from under |
| 86 | // the lock — a plausible future, since holding a mutex across a send |
| 87 | // is exactly what a later reader will want to remove. Leaving the |
| 88 | // channel to the garbage collector costs nothing: nothing holds a |
| 89 | // reference once it is out of the map. |
| 90 | // |
| 91 | // (The first version of this comment asserted the race as fact. |
| 92 | // AGENTS.md's own phrase for that is a comment-shaped lie, and this |
| 93 | // file would rather be correct about why it is right than sound more |
| 94 | // certain.) |
| 95 | func (h *hub) subscribe(docID int64) (<-chan int64, func()) { |
| 96 | ch := make(chan int64, 1) |
| 97 | |
| 98 | h.mu.Lock() |
| 99 | if h.subs[docID] == nil { |
| 100 | h.subs[docID] = make(map[chan int64]struct{}) |
| 101 | } |
| 102 | h.subs[docID][ch] = struct{}{} |
| 103 | h.mu.Unlock() |
| 104 | |
| 105 | var once sync.Once |
| 106 | return ch, func() { |
| 107 | once.Do(func() { |
| 108 | h.mu.Lock() |
| 109 | defer h.mu.Unlock() |
| 110 | subs, ok := h.subs[docID] |
| 111 | if !ok { |
| 112 | return |
| 113 | } |
| 114 | delete(subs, ch) |
| 115 | // Drop the inner map with its last subscriber, or this |
| 116 | // map accumulates one empty entry per document ever |
| 117 | // opened for the life of the process. |
| 118 | if len(subs) == 0 { |
| 119 | delete(h.subs, docID) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // broadcast tells docID's subscribers that seq exists. |
| 126 | // |
| 127 | // It runs on the HTTP request goroutine that just accepted an update |
| 128 | // (handlers.go's updatesPost), so it must never block: a browser tab |
| 129 | // on a wedged connection would otherwise hold a stranger's POST open. |
| 130 | // Hence the default case — a full slot already means "go and fetch", |
| 131 | // and a second notification would tell the subscriber nothing the |
| 132 | // first one does not. |
| 133 | func (h *hub) broadcast(docID, seq int64) { |
| 134 | h.mu.Lock() |
| 135 | defer h.mu.Unlock() |
| 136 | |
| 137 | for ch := range h.subs[docID] { |
| 138 | select { |
| 139 | case ch <- seq: |
| 140 | default: |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // documentCount reports how many documents have at least one |
| 146 | // subscriber. It exists for the tests that assert this map does not |
| 147 | // grow forever; nothing in the app reads it. |
| 148 | func (h *hub) documentCount() int { |
| 149 | h.mu.Lock() |
| 150 | defer h.mu.Unlock() |
| 151 | return len(h.subs) |
| 152 | } |
| 153 | |