forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits
| 1 | //! Live pages: change signals keyed by [`Topic`], and the event stream that carries them. |
| 2 | //! |
| 3 | //! A write that changes what a topic shows calls [`LiveHub::bump`]. Every open stream subscribed |
| 4 | //! to that topic wakes and sends one `changed` event. The signal carries no data: the page reads |
| 5 | //! its state again on the client (a soft re-fetch of the URL, see `nav.js`), so a bump that |
| 6 | //! lands during another one is never lost. Nothing polls. A topic with no subscriber is not kept. |
| 7 | //! |
| 8 | //! # Adding a topic |
| 9 | //! 1. Add a variant to [`Topic`], keyed by what the page is about (a repo id, a user id, ...). |
| 10 | //! 2. Call `state.live.bump(&Topic::Variant(..))` after the write that changes the page. |
| 11 | //! 3. In the page's own route (its auth check first), return |
| 12 | //! `live::stream_changes(cx, &state.live, &[topics], read)`. `read` returns a short fingerprint |
| 13 | //! of the state the page shows; a `changed` event is sent only when it differs from the last |
| 14 | //! one, so a bump that changes nothing visible sends nothing. |
| 15 | //! 4. Render `data-live="<the route URL>"` on the page's element that the client follows. |
| 16 | //! |
| 17 | //! A page whose state is cheap to compare can use `stream_changes` (the fingerprint). A page |
| 18 | //! whose every bump is a visible change (the issue pages) uses [`stream_wakes`] instead: one |
| 19 | //! `changed` per wake, several bumps in a row coalesced into one. |
| 20 | //! A user's topics are keyed by the user id and streamed only to that user: the route takes the |
| 21 | //! viewer from the session, never from the URL, so one user cannot follow another user's inbox. |
| 22 | //! |
| 23 | //! The hub is in-process only, like [`crate::notify_hub::NotifyHub`]. [`crate::ci::CiLive`] keeps |
| 24 | //! its own counters for CI and is not on this hub. |
| 25 | |
| 26 | use HashMap; |
| 27 | use Future; |
| 28 | use ; |
| 29 | use Duration; |
| 30 | |
| 31 | use select_all; |
| 32 | use ; |
| 33 | use watch; |
| 34 | use Result; |
| 35 | use Cx; |
| 36 | use HeaderValue; |
| 37 | use ; |
| 38 | use ; |
| 39 | |
| 40 | use crateAppState; |
| 41 | |
| 42 | /// What a live page follows. Add a variant when a page needs a new signal. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 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 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 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 | /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the |
| 140 | /// audit writer has no app state, and every audit entry wakes the admin pages from there. |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// The open topics, each with the counter its subscribers wait on. |
| 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 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | /// The state a stream follows between wakes: the receivers of its topics, the read, and the |
| 209 | /// fingerprint last sent or seen. |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | /// The `changed` event of a live page. The data is not read by the client. |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | /// A `name` event with no data the client reads, sent on each change of a page's state. |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | /// Waits until one of `wakes` is bumped. False when a topic is gone, which ends the stream. |
| 227 | async |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | /// The response of a live stream for any of `topics`. It sends `changed` after each wake, with no |
| 234 | /// fingerprint: the caller bumps only after a write that changes the page. Nothing is sent on |
| 235 | /// connect. The stream ends when a topic is gone, which cannot happen while the stream holds it. |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// The response of a live stream for `topics`. It sends `changed` after each wake whose |
| 248 | /// fingerprint differs from the last one. Nothing is sent on connect: the page already shows the |
| 249 | /// state the first read saw. The stream ends if the read fails. |
| 250 | pub async |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | /// `stream_changes` that also wakes on `extra`, signals from another hub such as the CI one. They |
| 264 | /// must be subscribed before this is called, so a change in between still wakes the stream. |
| 265 | pub async |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | /// The events of [`stream_changes_with`] as a stream, each named `name`, for a caller that merges |
| 281 | /// several streams into one response (see `pages::shell_events`). |
| 282 | pub async |
| 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 | /// Wakes the settings pages of `user_id`. Called after a write that changes what they show. |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | /// Bumps the settings topic of a repo, after a write that changes one of its settings pages. |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | /// A write changed the wiki of `repo_id`: a page was saved, or a push moved its main branch. |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | // Bumps. Each one wakes the streams of the topics it names; none of them reads more than the |
| 338 | // ids it needs. A failed lookup is logged and leaves the streams as they were. |
| 339 | |
| 340 | /// A write changed one pull request: its page and the list both follow it. |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | /// A pull request was opened, or the list changed for another reason. Only the list follows. |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | /// A change that may touch any pull of the repo (a push, a label or milestone edit, a merge run |
| 352 | /// by auto-merge): the list and every pull page of the repo. |
| 353 | pub async |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | /// A commit status was written for `sha`: the pulls whose head is that commit, and the list. |
| 366 | pub async |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | /// How often an idle event stream sends a `: keepalive` comment, so a reverse proxy does not drop |
| 381 | /// the connection. This is not a poll: events follow the change signals. |
| 382 | const STREAM_KEEP_ALIVE: Duration = from_secs; |
| 383 | |
| 384 | /// A run's or job's status word and whether it is terminal. `None` when the row is gone. |
| 385 | pub type Status = ; |
| 386 | |
| 387 | /// One `changed` event carrying the status word. |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | /// The events of a CI page, each named `name` (`changed` on a page's own stream). The first event |
| 393 | /// carries the status now. Each later one follows a wake of `wake`: with `only_on_status`, only when |
| 394 | /// the status differs from the last one sent. The stream ends after the terminal status is sent, |
| 395 | /// when the row is gone, or when the signal closes. Subscribe `wake` before the first read, so a |
| 396 | /// change in between is not lost. |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | + Send + 'static |
| 403 | where |
| 404 | F: Fn + Send + 'static, |
| 405 | Fut: + Send, |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | /// Open event streams one client address (IPv6 by /64) may hold. A page holds one or two, so this |
| 457 | /// is many tabs; past it a script that opens streams in a loop is refused. |
| 458 | const MAX_STREAMS_PER_CLIENT: usize = 64; |
| 459 | /// Open event streams server-wide. Each is a task and a socket that stay until the client leaves. |
| 460 | const MAX_STREAMS: usize = 4096; |
| 461 | |
| 462 | /// Open streams per client address, and their sum. |
| 463 | static OPEN_STREAMS: = |
| 464 | new; |
| 465 | |
| 466 | /// One open event stream, counted against its client's and the server's cap until dropped. |
| 467 | /// `ip` is `None` for an uncounted stream. |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | /// How long the browser waits before it reconnects a dropped event stream. The browser's default is |
| 515 | /// a few seconds; one second makes a page come back as soon as a server restart is done. |
| 516 | pub const STREAM_RETRY: Duration = from_secs; |
| 517 | |
| 518 | /// The response of an event stream: keep-alive comments while idle, no buffering by a reverse |
| 519 | /// proxy, and a `retry` field first so every stream reconnects after `STREAM_RETRY`. |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 |