| 1 | package calls |
| 2 | |
| 3 | // Team mode's call stream (GET /m/{sqid}/stream): the signalling channel |
| 4 | // a participation's browser listens on, re-decided on standing before |
| 5 | // every delivery and at the end of every lease (Home teammode-package.md |
| 6 | // §5, "the stream hook"; §7.4: "the signaling socket closed at |
| 7 | // Access.Remaining() unless renewed"). |
| 8 | // |
| 9 | // What a participation stands on: the meeting's host, always (a call |
| 10 | // stands on its host, team_host.go), and for a member-kind participation |
| 11 | // the member's own standing as well. A guest's own standing is not asked: |
| 12 | // once admitted a guest stands on the participation (ruling 14), and the |
| 13 | // participation on the host. |
| 14 | // |
| 15 | // Three answers, as Slides' streams (oficina/slides #33): |
| 16 | // |
| 17 | // - Member: deliver, and re-decide at the lease's end (never later than |
| 18 | // the shortest Remaining, never sooner than a second). Before every |
| 19 | // delivery the decision's leases are checked again (Access.State, |
| 20 | // which never waits); one that has run out is re-decided first. |
| 21 | // - Unknown: hold. The connection stays open and carries nothing but |
| 22 | // heartbeat comments; the standing is asked again every |
| 23 | // teamStreamHoldRetry. What arrives meanwhile is kept, in order, and |
| 24 | // delivered only once a Member answer says it may be — or, past what |
| 25 | // the hub's own buffer would keep, the stream ends with lost{buffer} |
| 26 | // and the browser resumes, which replays from the ring on standing it |
| 27 | // must have again. A Home blip does not drop the call: the media, |
| 28 | // which never passes through Calls, carries on, and so does the |
| 29 | // browser's stream, which receives its hello and so does not count |
| 30 | // the round as a stall. |
| 31 | // - NotMember: the end. A host who has left ends the meeting (lost |
| 32 | // {ended}); a member who has left is ended by the hub, in its words |
| 33 | // (lost{standing}), and the others are told "gone" and close their |
| 34 | // connections to them. |
| 35 | // |
| 36 | // The terminal events the hub raises (lost, ended) are delivered whatever |
| 37 | // the answer: they carry no call's content, only that this stream is |
| 38 | // over and why. |
| 39 | // |
| 40 | // What this cannot do, said plainly: Calls signals; it does not carry |
| 41 | // media. A connection already established peer to peer, or through the |
| 42 | // platform relay on credentials /ice minted (valid for up to three |
| 43 | // hours), is not Calls' to cut. Ending a participation tells every other |
| 44 | // browser to close its connection to it, which is what stops the media |
| 45 | // in practice; a modified client that ignored "gone" could keep a |
| 46 | // connection to a colluding peer until its relay credentials expire. |
| 47 | |
| 48 | import ( |
| 49 | "context" |
| 50 | "net/http" |
| 51 | "time" |
| 52 | |
| 53 | "amadan.net/oficina/home/roster" |
| 54 | "amadan.net/oficina/home/teammode" |
| 55 | |
| 56 | "amadan.net/oficina/calls/internal/calls/hub" |
| 57 | ) |
| 58 | |
| 59 | var ( |
| 60 | // teamStreamHoldRetry is how often a held stream asks again: the |
| 61 | // roster's own retry interval. A variable only so a test can shorten |
| 62 | // it. |
| 63 | teamStreamHoldRetry = roster.RetryAfter |
| 64 | // teamStreamRecheckMax caps how long a delivering stream goes before |
| 65 | // it re-decides. A lease is never longer than roster.MaximumAge, so |
| 66 | // in production this changes nothing; a test shortens it so a lease |
| 67 | // measured on a stepped roster clock is re-decided in real time. |
| 68 | teamStreamRecheckMax = roster.MaximumAge |
| 69 | ) |
| 70 | |
| 71 | // teamStreamHeld bounds what a held stream keeps: the hub's own buffer |
| 72 | // for one subscriber. Past it the stream ends as an overflowing |
| 73 | // subscriber does (lost{buffer}), and the browser resumes. |
| 74 | const teamStreamHeld = 64 |
| 75 | |
| 76 | // streamVerdict is one decision's answer. |
| 77 | type streamVerdict int |
| 78 | |
| 79 | const ( |
| 80 | streamHolds streamVerdict = iota |
| 81 | streamDelivers |
| 82 | streamEnds |
| 83 | ) |
| 84 | |
| 85 | // streamDecision is a participation's standing, decided once: what to do, |
| 86 | // when to decide again, the leases a delivery rests on, and the event |
| 87 | // that ends the stream. |
| 88 | type streamDecision struct { |
| 89 | verdict streamVerdict |
| 90 | renew time.Duration |
| 91 | leases []*teammode.Access |
| 92 | end hub.Message |
| 93 | } |
| 94 | |
| 95 | // stands reports whether every lease the decision delivers on still |
| 96 | // holds. It never waits (Access.State's TryLock), and a lease that has |
| 97 | // run out latches, so the stream re-decides before it delivers. |
| 98 | func (d streamDecision) stands() bool { |
| 99 | if d.verdict != streamDelivers { |
| 100 | return false |
| 101 | } |
| 102 | for _, acc := range d.leases { |
| 103 | if acc.State() != roster.Member { |
| 104 | return false |
| 105 | } |
| 106 | } |
| 107 | return true |
| 108 | } |
| 109 | |
| 110 | // freshStanding masks the request's own Access, latched when the stream |
| 111 | // opened, so that whatever is asked about this person on the stream's |
| 112 | // behalf — by the hub's re-derivation included — is asked of the roster |
| 113 | // now (memberState falls through to standingOf). A long-lived stream |
| 114 | // that kept reading its door-time Access could never renew. |
| 115 | func freshStanding(ctx context.Context) context.Context { |
| 116 | return context.WithValue(ctx, teamReqKey{}, (*teamReq)(nil)) |
| 117 | } |
| 118 | |
| 119 | // teamStreamDecide decides one participation's stream now, on fresh |
| 120 | // Accesses. Standing is read before anything else, and a Member answer |
| 121 | // always carries a timer (the Slides and Sheets stream-order lesson, |
| 122 | // oficina/slides #33 and oficina/sheets #54): a lease that runs out |
| 123 | // after it is read gives a one-second timer, never "delivers, nothing to |
| 124 | // renew". |
| 125 | func (a *app) teamStreamDecide(ctx context.Context, m Meeting, person Person, sqid, session string, peer hub.PeerID) streamDecision { |
| 126 | host := a.hostAccess(ctx, m) |
| 127 | switch host.State() { |
| 128 | case roster.NotMember: |
| 129 | a.endForHost(ctx, m) |
| 130 | return streamDecision{verdict: streamEnds, end: hub.Message{Type: hub.TypeLost, Reason: hub.ReasonEnded}} |
| 131 | case roster.Unknown: |
| 132 | return streamDecision{verdict: streamHolds} |
| 133 | } |
| 134 | leases := []*teammode.Access{host} |
| 135 | id, kind, live := a.hub.LiveKindForSession(sqid, session) |
| 136 | if !live || id != peer { |
| 137 | // The participation has ended (removed, replaced, left): the hub |
| 138 | // has put its terminal event on this stream's subscriber, which |
| 139 | // is delivered whatever the answer. Until it is read, nothing |
| 140 | // else is. |
| 141 | return streamDecision{verdict: streamHolds} |
| 142 | } |
| 143 | if kind == hub.KindMember { |
| 144 | own := a.standingOf(ctx, person.ID) |
| 145 | switch own.State() { |
| 146 | case roster.Unknown: |
| 147 | return streamDecision{verdict: streamHolds} |
| 148 | case roster.NotMember: |
| 149 | ended, err := a.hub.Recheck(ctx, sqid, session, peer) |
| 150 | if err == nil && ended { |
| 151 | return streamDecision{verdict: streamEnds, end: hub.Message{Type: hub.TypeLost, Reason: hub.ReasonStanding}} |
| 152 | } |
| 153 | // Unknown by the time the hub asked, or re-derived as a |
| 154 | // guest still holding a grant (§2.4): asked again shortly, |
| 155 | // as what it now is. |
| 156 | return streamDecision{verdict: streamHolds} |
| 157 | } |
| 158 | leases = append(leases, own) |
| 159 | } |
| 160 | left := roster.MaximumAge |
| 161 | for _, acc := range leases { |
| 162 | left = min(left, acc.Remaining()) |
| 163 | } |
| 164 | renew := min(left, teamStreamRecheckMax) |
| 165 | if left < time.Second { |
| 166 | // Member when read, run out since: ask again in a second. |
| 167 | renew = time.Second |
| 168 | } |
| 169 | return streamDecision{verdict: streamDelivers, renew: renew, leases: leases} |
| 170 | } |
| 171 | |
| 172 | // serveTeamStream is serveStream in team mode: the same wire, the same |
| 173 | // heartbeat, TTL and shutdown, with every delivery decided on standing |
| 174 | // (above). first is what the stream owes before the subscriber's events: |
| 175 | // the hello is written at once — it names only this browser's own peer |
| 176 | // and the process — and the rest (a resume's roster and replay) waits on |
| 177 | // a Member answer like everything else. |
| 178 | func (a *app) serveTeamStream(w http.ResponseWriter, r *http.Request, sub *hub.Sub, first []hub.Message, m Meeting, person Person, sqid, session string, peer hub.PeerID) { |
| 179 | ctx := freshStanding(r.Context()) |
| 180 | rc := http.NewResponseController(w) |
| 181 | w.Header().Set("Content-Type", "text/event-stream") |
| 182 | w.Header().Set("Cache-Control", "no-store") |
| 183 | w.Header().Set("X-Accel-Buffering", "no") |
| 184 | w.WriteHeader(http.StatusOK) |
| 185 | if err := rc.Flush(); err != nil { |
| 186 | return |
| 187 | } |
| 188 | if len(first) > 0 && first[0].Type == hub.TypeHello { |
| 189 | if !writeEvent(w, rc, first[0]) { |
| 190 | return |
| 191 | } |
| 192 | first = first[1:] |
| 193 | } |
| 194 | held := append([]hub.Message(nil), first...) |
| 195 | |
| 196 | heartbeat := time.NewTicker(a.heartbeatEvery()) |
| 197 | defer heartbeat.Stop() |
| 198 | ttl := time.NewTimer(a.streamLifeFor(r)) |
| 199 | defer ttl.Stop() |
| 200 | // again fires when this decision is due to be asked again: at its |
| 201 | // lease's end while delivering, after teamStreamHoldRetry while held. |
| 202 | // It is set when a decision is made and at no other time, so a |
| 203 | // heartbeat or a delivery never postpones it. |
| 204 | again := time.NewTimer(time.Hour) |
| 205 | defer again.Stop() |
| 206 | var d streamDecision |
| 207 | decide := func() { |
| 208 | d = a.teamStreamDecide(ctx, m, person, sqid, session, peer) |
| 209 | wait := teamStreamHoldRetry |
| 210 | if d.verdict == streamDelivers { |
| 211 | wait = d.renew |
| 212 | } |
| 213 | // Go 1.23 timers: Reset drops any expiry not yet received. |
| 214 | again.Reset(wait) |
| 215 | } |
| 216 | decide() |
| 217 | |
| 218 | for { |
| 219 | // Deliver what is owed, one stand-check per event. |
| 220 | for d.verdict == streamDelivers && len(held) > 0 { |
| 221 | if !d.stands() { |
| 222 | decide() |
| 223 | continue |
| 224 | } |
| 225 | msg := held[0] |
| 226 | held = held[1:] |
| 227 | if !writeEvent(w, rc, msg) { |
| 228 | return |
| 229 | } |
| 230 | } |
| 231 | if d.verdict == streamEnds { |
| 232 | writeEvent(w, rc, d.end) |
| 233 | return |
| 234 | } |
| 235 | select { |
| 236 | case <-r.Context().Done(): |
| 237 | return |
| 238 | case <-a.done: |
| 239 | return |
| 240 | case <-ttl.C: |
| 241 | return |
| 242 | case <-heartbeat.C: |
| 243 | if !writeRaw(w, rc, ": hb\n\n") { |
| 244 | return |
| 245 | } |
| 246 | case <-again.C: |
| 247 | decide() |
| 248 | case msg, ok := <-sub.C(): |
| 249 | if !ok { |
| 250 | return |
| 251 | } |
| 252 | if msg.Type == hub.TypeLost || msg.Type == hub.TypeEnded { |
| 253 | // Terminal for this stream, and no call's content: said |
| 254 | // whatever the standing (§4.5's reasons). |
| 255 | writeEvent(w, rc, msg) |
| 256 | return |
| 257 | } |
| 258 | if len(held) == 0 && d.stands() { |
| 259 | if !writeEvent(w, rc, msg) { |
| 260 | return |
| 261 | } |
| 262 | continue |
| 263 | } |
| 264 | held = append(held, msg) |
| 265 | if len(held) > teamStreamHeld { |
| 266 | writeEvent(w, rc, hub.Message{Type: hub.TypeLost, Reason: hub.ReasonBuffer}) |
| 267 | return |
| 268 | } |
| 269 | if d.verdict == streamDelivers { |
| 270 | // A lease ran out between decisions: decide before |
| 271 | // anything more is delivered. |
| 272 | decide() |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |