im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! The health probe worker (contracts.md 4): every minute it asks each listed service for `GET /healthz` |
| 2 | //! and records the answer in that service's row of the `services` table. |
| 3 | //! |
| 4 | //! - A probe is a plain GET of `<origin>/healthz`. Redirects are not followed, so a `301` from a service is |
| 5 | //! a failure. The answer is healthy only when its status is `200`. At most [`BODY_LIMIT`] bytes of the |
| 6 | //! body are read, and they are discarded. |
| 7 | //! - A probe has [`PROBE_TIMEOUT`] to finish, from connect to the end of the body. A service that does not |
| 8 | //! answer in time is down. |
| 9 | //! - Only `http` and `https` origins are probed. Any other origin is recorded as down. |
| 10 | //! - im's own row (key `im`) is not probed: im is answering this very round, so it is recorded as healthy. |
| 11 | //! - The rows are probed a few at a time ([`PROBE_CONCURRENCY`]), so one slow service does not hold up the |
| 12 | //! others. Each row is recorded as soon as its own probe is done. |
| 13 | //! |
| 14 | //! The list is read again at every round, so a service the admin adds or removes is picked up within a minute. |
| 15 | //! Nothing here fails the server: a store error or a failed probe is logged, and the next round tries again. |
| 16 | |
| 17 | use Duration; |
| 18 | |
| 19 | use ; |
| 20 | use Service; |
| 21 | use Store; |
| 22 | use now_stamp; |
| 23 | use ; |
| 24 | use watch; |
| 25 | use JoinHandle; |
| 26 | |
| 27 | /// The key of im's own row. im is never probed over HTTP: its answer is known. |
| 28 | const HOME_KEY: &str = "im"; |
| 29 | |
| 30 | /// The time between two rounds (contracts.md 4: every 60 seconds). |
| 31 | const EVERY: Duration = from_secs; |
| 32 | |
| 33 | /// How long a service has to answer one probe (contracts.md 4: 2 seconds). |
| 34 | pub const PROBE_TIMEOUT: Duration = from_secs; |
| 35 | |
| 36 | /// The most body bytes read from a probe answer. Only the status decides, so the body is read to drain it |
| 37 | /// and then discarded. |
| 38 | const BODY_LIMIT: usize = 256; |
| 39 | |
| 40 | /// How many probes run at once. |
| 41 | const PROBE_CONCURRENCY: usize = 8; |
| 42 | |
| 43 | /// Starts the worker. It runs one round at boot and then every [`EVERY`], until `stop` is set. |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | /// The HTTP client of the probes: two-second timeouts and no redirects. |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | /// One round: im's own row is recorded as healthy, and every other row is probed and recorded. |
| 68 | /// Failures are logged per row. |
| 69 | pub async |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | /// Probes one row and records the answer with the time it was recorded. |
| 94 | async |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | /// True when `origin` is an http or https origin whose `/healthz` answers `200` within the timeout. |
| 105 | async |
| 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 |