in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 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 | //! The topics of the pages are listed on [`Topic`]. Every write that changes what a page shows bumps its |
| 13 | //! topic after the commit. |
| 14 | |
| 15 | use HashMap; |
| 16 | use Future; |
| 17 | use ; |
| 18 | use Duration; |
| 19 | |
| 20 | use ; |
| 21 | use watch; |
| 22 | use Result; |
| 23 | use Cx; |
| 24 | use ; |
| 25 | use ; |
| 26 | use ; |
| 27 | |
| 28 | /// How long a client waits before it reconnects when a stream ends. Sent once, at the start. |
| 29 | pub const STREAM_RETRY: Duration = from_millis; |
| 30 | |
| 31 | /// Silence after which the server sends a keep-alive comment, so proxies keep the stream open. |
| 32 | pub const KEEP_ALIVE: Duration = from_secs; |
| 33 | |
| 34 | /// How often an idle `changed` stream checks its viewer anyway, so a viewer who signed out or lost access is |
| 35 | /// noticed even when nothing the stream follows changes. |
| 36 | const RECHECK: Duration = from_secs; |
| 37 | /// The longest a `changed` stream lives. The browser then reconnects and is checked again from the start. |
| 38 | const MAX_STREAM_LIFE: Duration = from_secs; |
| 39 | /// What a live page follows. |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | /// The change counters of all open topics. Cheap to share: every clone of the app state holds the |
| 56 | /// same `Arc<LiveHub>`. |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 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 | /// The state a stream carries between wakes. |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | /// Opens the `changed` stream for `topics`. The stream sends an event only when `fingerprint` |
| 102 | /// differs from the value it last sent, so a bump that changes nothing the viewer sees sends |
| 103 | /// nothing. Nothing is sent on connect: the page already shows the state that `fingerprint` read. |
| 104 | /// |
| 105 | /// `fingerprint` must be cheap and must describe everything the page shows. A read that fails ends |
| 106 | /// the stream, and the browser reconnects. |
| 107 | pub async |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 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 | /// The data-less `changed` event. |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | /// Resolves when one of `wakes` changes. False when that topic is gone, which ends the stream. |
| 160 | /// With no topics at all it never resolves: the stream then only sends keep-alives. |
| 161 | async |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | /// The response of a live stream: `retry` once, then the events, with keep-alives while idle. |
| 171 | /// `X-Accel-Buffering: no` stops a reverse proxy from holding the events back. |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | /// `GET /-/alive`. Every page holds this stream, so the browser learns when the server stops and |
| 187 | /// starts again. Anonymous viewers may hold it, and it reveals nothing: for now it sends only the |
| 188 | /// `retry` hint and keep-alives. |
| 189 | |
| 190 | async |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 |