im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Live updates: a change counter per topic, and the server-sent event stream that carries it to |
| 2 | //! the browser. |
| 3 | //! |
| 4 | //! A write that changes what a page shows calls [`LiveHub::bump`] after it commits. A stream that |
| 5 | //! subscribed to that topic wakes and sends one `changed` event without data. The browser then |
| 6 | //! fetches the page again (`nav.js`), so an event carries no state and a missed one cannot leave the |
| 7 | //! page wrong for long. Nothing polls, and a topic nobody listens to is dropped. |
| 8 | //! |
| 9 | //! The hub is not a queue and not an authorisation check. Each route that opens a stream |
| 10 | //! authenticates and authorises first, then calls [`stream_changes`]. |
| 11 | //! |
| 12 | //! Every stream ends when the process stops (the stop flag in [`AppState`]), so a shutdown does not wait |
| 13 | //! for a browser to close its connection. No stream lives longer than [`MAX_STREAM_LIFE`]: the browser |
| 14 | //! reconnects, and each new connection authenticates again. |
| 15 | |
| 16 | use HashMap; |
| 17 | use Future; |
| 18 | use ; |
| 19 | use Duration; |
| 20 | |
| 21 | use ; |
| 22 | use Store; |
| 23 | use ; |
| 24 | use Result; |
| 25 | use ; |
| 26 | use ; |
| 27 | use ; |
| 28 | use ; |
| 29 | |
| 30 | use crateapi_error; |
| 31 | use crate; |
| 32 | use cratecurrent_user; |
| 33 | use crateAppState; |
| 34 | |
| 35 | /// How long a client waits before it reconnects when a stream ends. Sent once, at the start. |
| 36 | pub const STREAM_RETRY: Duration = from_millis; |
| 37 | |
| 38 | /// Silence after which the server sends a keep-alive comment, so proxies keep the stream open. |
| 39 | pub const KEEP_ALIVE: Duration = from_secs; |
| 40 | |
| 41 | /// Longest a stream lives, whatever happens on it. The browser reconnects after it, and a reconnect |
| 42 | /// authenticates and authorises again, so a session that ended or lost its role is not kept by a stream. |
| 43 | pub const MAX_STREAM_LIFE: Duration = from_secs; |
| 44 | /// How often an idle `changed` stream runs its fingerprint anyway, so a viewer who signed out or lost |
| 45 | /// access is noticed even when no topic of the stream changes. |
| 46 | pub const RECHECK: Duration = from_secs; |
| 47 | |
| 48 | /// Anonymous `/-/alive` streams open at once, across the whole process. A further one gets `429`. |
| 49 | pub const MAX_ANONYMOUS_STREAMS: usize = 256; |
| 50 | |
| 51 | /// Signed-in `/-/alive` streams one user may hold open at once, across all their browsers and tabs. A further |
| 52 | /// one gets `429`. |
| 53 | pub const MAX_ALIVE_STREAMS_PER_USER: usize = 16; |
| 54 | |
| 55 | /// What a live page follows. |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | /// The change counters of all open topics. Cheap to share: every clone of the app state holds the |
| 66 | /// same `Arc<LiveHub>`. |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | /// The state a stream carries between wakes. |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | /// Opens the `changed` stream for `topics`. The stream sends an event only when `fingerprint` |
| 123 | /// differs from the value it last sent, so a bump that changes nothing the viewer sees sends |
| 124 | /// nothing. Nothing is sent on connect: the page already shows the state that `fingerprint` read. |
| 125 | /// |
| 126 | /// `fingerprint` must be cheap and must describe everything the page shows. It runs on every wake, before |
| 127 | /// anything is sent, so it is also the place to check the viewer again: an error (a read that fails, or a |
| 128 | /// viewer who may no longer see the page) ends the stream, and the browser reconnects and is refused. |
| 129 | pub async |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | /// The data-less `changed` event. |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | /// Resolves when the stop flag is set. A flag whose sender is gone never resolves, so the stream is not |
| 183 | /// ended by mistake. |
| 184 | pub async |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | /// Resolves when one of `wakes` changes. False when that topic is gone, which ends the stream. |
| 191 | /// With no topics at all it never resolves: the stream then only sends keep-alives. |
| 192 | async |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | /// The response of a live stream: `retry` once, then the events, with keep-alives while idle. |
| 202 | /// `X-Accel-Buffering: no` stops a reverse proxy from holding the events back. |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | /// The state of a viewer's `/-/alive` stream between two wakes. |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | /// `GET /-/alive`. Every page holds this stream, so the browser learns when the server stops and |
| 236 | /// starts again. |
| 237 | /// |
| 238 | /// An anonymous viewer gets keep-alives only. A signed-in viewer also gets an `account` event each |
| 239 | /// time their account changes (the page then refreshes), and a `signed_out` event when their session |
| 240 | /// is gone. The session is checked every [`RECHECK`] too, so an idle stream notices an expiry. After `signed_out` the browser goes to the sign-in page, and the stream ends. |
| 241 | |
| 242 | async |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 |