im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! The people directory of `im` (`docs/contracts.md` section 3): the full listing, and the change stream |
| 2 | //! that follows it. |
| 3 | //! |
| 4 | //! The listing and its cursor are read in one step (`Store::directory_snapshot`), so the cursor is exactly |
| 5 | //! the state of the members. The stream resumes from `Last-Event-ID` (or `?cursor=`). A cursor that is not |
| 6 | //! a non-negative integer, that is ahead of the feed, or that is older than the oldest kept row gets one |
| 7 | //! `resync` event and the stream closes: the client then reads the listing again. Otherwise every row after |
| 8 | //! the cursor is sent (`member` with the current record, or `deleted`), and the stream then waits on the |
| 9 | //! feed's watch channel. It ends when the token expires (or after 50 minutes), when the calling client is |
| 10 | //! disabled or removed, or when the client's stream is cut at the per-client cap (four at once, 429 beyond). |
| 11 | |
| 12 | use ; |
| 13 | use ; |
| 14 | use Duration; |
| 15 | |
| 16 | use ; |
| 17 | use HeaderValue; |
| 18 | use User; |
| 19 | use Store; |
| 20 | use ; |
| 21 | use unix_now; |
| 22 | use ; |
| 23 | use watch; |
| 24 | use ; |
| 25 | use Result; |
| 26 | use ; |
| 27 | use ; |
| 28 | use uri; |
| 29 | use ; |
| 30 | use ; |
| 31 | |
| 32 | use ; |
| 33 | use ; |
| 34 | use crateparse_pairs; |
| 35 | use crateAppState; |
| 36 | |
| 37 | /// Most streams one client may hold open at once. A further one is refused with `429`. |
| 38 | pub const MAX_STREAMS_PER_CLIENT: usize = 4; |
| 39 | |
| 40 | /// Longest a stream lives, in seconds of wall time, whatever the token's expiry says (contracts 3.2). |
| 41 | pub const STREAM_MAX_LIFE: Duration = from_secs; |
| 42 | |
| 43 | /// Silence after which a keep-alive comment is sent (contracts 3.2 says every 30 s; 25 s keeps a margin). |
| 44 | pub const KEEP_ALIVE: Duration = from_secs; |
| 45 | |
| 46 | /// Rows read from the feed in one batch. |
| 47 | const BATCH: i64 = 200; |
| 48 | |
| 49 | /// How long a `Retry-After` the refused client is given, in seconds. |
| 50 | const RETRY_AFTER_SECS: &str = "5"; |
| 51 | |
| 52 | /// The open streams of each key: a machine client for the directory feed, a user for `/-/alive`. Shared by |
| 53 | /// every request, so each cap holds across connections. |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | /// One open stream of a key. Dropping it frees the slot, so a stream that ends for any reason gives its |
| 60 | /// slot back. |
| 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 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | /// The wire form of one member (contracts 3.1). |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | /// `GET /api/v1/directory`: every account, disabled ones included, and the cursor of this state. |
| 114 | |
| 115 | async |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | /// `GET /api/v1/directory/stream`: the change stream, as `text/event-stream`. |
| 131 | |
| 132 | async |
| 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 state of one stream between two events. |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// The events of a stream from its cursor on. It ends on a store failure, a disabled client, the |
| 198 | /// deadline, or a closed feed; the browser or consumer reconnects with its last cursor. |
| 199 | + Send + 'static |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | /// Resolves when the stop flag is set. A flag whose sender is gone never resolves, so a stream is not |
| 249 | /// ended by a test state that has no server behind it. |
| 250 | async |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | /// The event of one change row, with the row's sequence number as its id. A member row is sent with the |
| 257 | /// person's current record; if the person is gone by then, the event is `deleted`. |
| 258 | async |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | /// The one `resync` event: the cursor is unusable, so the consumer reads the listing again. |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | /// The cursor the client asked to resume from: `Last-Event-ID`, else `?cursor=`. A repeated `cursor` |
| 280 | /// parameter is treated as garbage, which means resync. |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | /// Where a stream starts, or `None` when it must resync. No cursor starts at the head (the consumer reads |
| 295 | /// the listing first). A cursor must be digits only, not ahead of the head, and not older than the oldest |
| 296 | /// kept row: the rows between it and that row were pruned, so they cannot be replayed. |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | /// How long a stream of this caller may live: until the token expires, and at most [`STREAM_MAX_LIFE`]. |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | /// The response of a stream: the events, with a keep-alive comment while idle. `X-Accel-Buffering: no` |
| 323 | /// stops a reverse proxy from holding the events back. |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 |