6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | collections::BTreeMap, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | fs, io, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | os::unix::net::{UnixListener, UnixStream}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | path::PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | process, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | sync::{Arc, Mutex, MutexGuard}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | thread, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | time::{Duration, Instant}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | use crate::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | client, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | config::Config, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | paths, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | proto::{self, Acquire, Event, LeaseId, LeaseInfo, Reader, Request, Status}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | sched::{self, Capacity, Disk, FsId, Need, Queued, Reason, Running, Verdict}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | sys, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | units::Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | struct Run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | started: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | rss: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | reminded: Option<Instant>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | struct Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | req: Acquire, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | fs: Vec<FsId>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | queued_at: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | run: Option<Run>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | out: UnixStream, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | waiting: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | last_pos: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | watchers: Vec<UnixStream>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | impl Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | fn elapsed(&self) -> Duration { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | self.run |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | .as_ref() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | .map_or(self.queued_at, |r| r.started) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | .elapsed() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | fn overdue(&self, grace: f64) -> bool { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | self.run.is_some() && self.elapsed() > self.req.need.time.mul_f64(grace) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | struct State { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | cfg: Config, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | next: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | leases: BTreeMap<LeaseId, Lease>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | cap: Capacity, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | baselines: BTreeMap<FsId, Bytes>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | conns: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | idle_since: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | type Shared = Arc<Mutex<State>>; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | fn lock(shared: &Shared) -> MutexGuard<'_, State> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | shared |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | .lock() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | .unwrap_or_else(std::sync::PoisonError::into_inner) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | impl State { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | fn new(cfg: Config) -> Self { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | let (total, live) = sys::memory(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | let cap = Capacity { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | ram_budget: cfg.budget.ram.resolve(total), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | ram_live: live, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | ram_reserve: cfg.budget.ram_reserve, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 78 | cores: cfg.budget.cores.resolve(sys::cores()), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 79 | disks: BTreeMap::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 80 | pressure: false, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 81 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 82 | Self { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 83 | cfg, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 84 | next: 1, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | leases: BTreeMap::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | cap, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | baselines: BTreeMap::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 88 | conns: 0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | idle_since: Instant::now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 90 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 91 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 92 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 93 | fn heavy(&self, need: &Need) -> bool { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 94 | let h = &self.cfg.heavy; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 95 | need.ram >= h.ram || need.disk >= h.disk || need.cores >= h.cores.resolve(sys::cores()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 96 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 97 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 98 | /// capped at a tenth of the fs, or a 16G tmpfs /tmp with a 20G reserve |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 99 | /// admits nothing ever |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 100 | fn reserve(&self, size: Bytes) -> Bytes { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 101 | self.cfg.budget.disk_reserve.min(Bytes(size.0 / 10)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 102 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 103 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 104 | fn fs_for(&mut self, cwd: &PathBuf) -> Vec<FsId> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 105 | let mut ids = Vec::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 106 | for path in std::iter::once(cwd).chain(&self.cfg.budget.disk_paths) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 107 | if let Some(fs) = sys::fs(path) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 108 | if !ids.contains(&fs.id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 109 | ids.push(fs.id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 110 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 111 | let reserve = self.reserve(fs.size); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 112 | self.cap.disks.entry(fs.id).or_insert(Disk { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 113 | free: fs.free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 114 | reserve, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 115 | consumed: Bytes::ZERO, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 116 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 117 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 118 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 119 | ids |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 120 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 121 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 122 | fn refresh(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 123 | let (_, live) = sys::memory(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 124 | self.cap.ram_live = live; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 125 | self.cap.pressure = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 126 | .cfg |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 127 | .budget |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 128 | .psi_max |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 129 | .is_some_and(|max| sys::pressure().is_some_and(|p| p > max)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 130 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 131 | let mut paths: Vec<PathBuf> = self.leases.values().map(|l| l.req.cwd.clone()).collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 132 | paths.extend(self.cfg.budget.disk_paths.iter().cloned()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 133 | let mut disks = BTreeMap::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 134 | for path in paths { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 135 | if let Some(fs) = sys::fs(&path) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 136 | disks.insert(fs.id, (fs.free, self.reserve(fs.size))); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 137 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 138 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 139 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 140 | let busy: Vec<FsId> = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 141 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 142 | .values() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 143 | .filter(|l| l.run.is_some()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 144 | .flat_map(|l| l.fs.iter().copied()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 145 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 146 | self.baselines.retain(|id, _| busy.contains(id)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 147 | self.cap.disks = disks |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 148 | .into_iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 149 | .map(|(id, (free, reserve))| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 150 | let consumed = if busy.contains(&id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 151 | self.baselines |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 152 | .entry(id) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 153 | .or_insert(free) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 154 | .saturating_sub(free) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 155 | } else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 156 | Bytes::ZERO |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 157 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 158 | ( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 159 | id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 160 | Disk { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 161 | free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 162 | reserve, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 163 | consumed, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 164 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 165 | ) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 166 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 167 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 168 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 169 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 170 | fn running(&self) -> Vec<Running> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 171 | self.leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 172 | .values() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 173 | .filter_map(|l| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 174 | let run = l.run.as_ref()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 175 | Some(Running { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 176 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 177 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 178 | fs: l.fs.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 179 | rss: run.rss, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 180 | remaining: l.req.need.time.saturating_sub(run.started.elapsed()), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 181 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 182 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 183 | .collect() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 184 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 185 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 186 | fn reschedule(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 187 | let queue: Vec<Queued> = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 188 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 189 | .iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 190 | .filter(|(_, l)| l.run.is_none()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 191 | .map(|(id, l)| Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 192 | id: *id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 193 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 194 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 195 | fs: l.fs.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 196 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 197 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 198 | if queue.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 199 | return; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 200 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 201 | let verdicts = sched::plan( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 202 | &queue, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 203 | &self.running(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 204 | &self.cap, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 205 | self.cfg.queue.backfill_max_time.0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 206 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 207 | let cores = self.cap.cores; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 208 | let mut position = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 209 | for (id, verdict) in verdicts { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 210 | let Some(lease) = self.leases.get_mut(&id) else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 211 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 212 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 213 | match verdict { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 214 | Verdict::Admit => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 215 | lease.run = Some(Run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 216 | started: Instant::now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 217 | rss: Bytes::ZERO, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 218 | reminded: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 219 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 220 | let _ = proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 221 | &mut lease.out, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 222 | &Event::Granted { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 223 | lease: id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 224 | cores: lease.req.need.cores.clamp(1, cores.max(1)), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 225 | heavy: lease.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 226 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 227 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 228 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 229 | Verdict::Wait(reason) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 230 | position += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 231 | lease.waiting = Some(reason); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 232 | if lease.last_pos != position { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 233 | lease.last_pos = position; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 234 | let _ = proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 235 | &mut lease.out, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 236 | &Event::Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 237 | lease: id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 238 | position, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 239 | reason: Some(reason), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 240 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 241 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 242 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 243 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 244 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 245 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 246 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 247 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 248 | fn remove(&mut self, id: LeaseId) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 249 | if let Some(lease) = self.leases.remove(&id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 250 | for mut w in lease.watchers { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 251 | let _ = proto::send(&mut w, &Event::Fired { overdue: false }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 252 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 253 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 254 | if self.leases.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 255 | self.idle_since = Instant::now(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 256 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 257 | self.refresh(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 258 | self.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 259 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 260 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 261 | fn fire_overdue(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 262 | let grace = self.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 263 | for lease in self.leases.values_mut() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 264 | if !lease.watchers.is_empty() && lease.overdue(grace) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 265 | for mut w in lease.watchers.drain(..) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 266 | let _ = proto::send(&mut w, &Event::Fired { overdue: true }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 267 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 268 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 269 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 270 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 271 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 272 | fn info(&self) -> Vec<LeaseInfo> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 273 | let grace = self.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 274 | let mut position = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 275 | self.leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 276 | .iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 277 | .map(|(id, l)| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 278 | if l.run.is_none() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 279 | position += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 280 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 281 | LeaseInfo { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 282 | id: *id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 283 | cmd: l.req.cmd.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 284 | cwd: l.req.cwd.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 285 | session: l.req.session.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 286 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 287 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 288 | running: l.run.is_some(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 289 | elapsed: l.elapsed().as_secs(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 290 | rss: l.run.as_ref().map_or(Bytes::ZERO, |r| r.rss), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 291 | overdue: l.overdue(grace), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 292 | waiting: l.waiting.filter(|_| l.run.is_none()), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 293 | position: if l.run.is_none() { position } else { 0 }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 294 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 295 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 296 | .collect() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 297 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 298 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 299 | fn status(&self) -> Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 300 | let (ram_free, cores_free) = sched::headroom(&self.running(), &self.cap); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 301 | Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 302 | leases: self.info(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 303 | ram_free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 304 | ram_budget: self.cap.ram_budget, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 305 | cores_free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 306 | cores: self.cap.cores, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 307 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 308 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 309 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 310 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 311 | fn serve(shared: &Shared, mut stream: UnixStream) -> io::Result<()> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 312 | let mut reader = Reader::new(&stream)?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 313 | let Some(req) = reader.recv::<Request>()? else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 314 | return Ok(()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 315 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 316 | match req { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 317 | Request::Acquire(req) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 318 | let id = { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 319 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 320 | let id = st.next; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 321 | st.next += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 322 | let fs = st.fs_for(&req.cwd); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 323 | let heavy = st.heavy(&req.need); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 324 | st.leases.insert( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 325 | id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 326 | Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 327 | req, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 328 | heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 329 | fs, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 330 | queued_at: Instant::now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 331 | run: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 332 | out: stream.try_clone()?, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 333 | waiting: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 334 | last_pos: 0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 335 | watchers: Vec::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 336 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 337 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 338 | st.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 339 | id |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 340 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 341 | hold(shared, id, &mut reader); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 342 | lock(shared).remove(id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 343 | Ok(()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 344 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 345 | Request::Status => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 346 | let status = lock(shared).status(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 347 | proto::send(&mut stream, &Event::Status(status)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 348 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 349 | Request::Kill { lease } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 350 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 351 | let reply = match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 352 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 353 | let _ = proto::send(&mut l.out, &Event::Kill); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 354 | Event::Ok |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 355 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 356 | None => Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 357 | msg: format!("no lease {lease}"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 358 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 359 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 360 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 361 | proto::send(&mut stream, &reply) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 362 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 363 | Request::Extend { lease, by } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 364 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 365 | let reply = match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 366 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 367 | l.req.need.time += by.0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 368 | if let Some(run) = &mut l.run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 369 | run.reminded = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 370 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 371 | Event::Ok |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 372 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 373 | None => Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 374 | msg: format!("no lease {lease}"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 375 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 376 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 377 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 378 | proto::send(&mut stream, &reply) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 379 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 380 | Request::Watch { lease } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 381 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 382 | let grace = st.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 383 | match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 384 | Some(l) if l.overdue(grace) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 385 | proto::send(&mut stream, &Event::Fired { overdue: true }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 386 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 387 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 388 | l.watchers.push(stream); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 389 | Ok(()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 390 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 391 | None => proto::send(&mut stream, &Event::Fired { overdue: false }), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 392 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 393 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 394 | Request::Overdue { session } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 395 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 396 | let grace = st.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 397 | let every = st.cfg.overrun.remind_every.0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 398 | let mut due = Vec::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 399 | for (id, l) in &mut st.leases { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 400 | if l.req.session.as_deref() != Some(&session) || !l.overdue(grace) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 401 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 402 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 403 | let Some(run) = &mut l.run else { continue }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 404 | if run.reminded.is_some_and(|t| t.elapsed() < every) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 405 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 406 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 407 | run.reminded = Some(Instant::now()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 408 | due.push(*id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 409 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 410 | let leases = st |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 411 | .info() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 412 | .into_iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 413 | .filter(|i| due.contains(&i.id)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 414 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 415 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 416 | proto::send(&mut stream, &Event::Overdue { leases }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 417 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 418 | Request::Started { .. } | Request::Usage { .. } | Request::Done => proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 419 | &mut stream, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 420 | &Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 421 | msg: "no lease on this connection".into(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 422 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 423 | ), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 424 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 425 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 426 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 427 | /// a client that vanishes without saying done got sigkilled or similar, and |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 428 | /// pdeathsig only reaches its direct child. take the rest of the group down |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 429 | /// so a headless build doesn't keep eating ram off the books |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 430 | fn hold(shared: &Shared, id: LeaseId, reader: &mut Reader) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 431 | let mut pgid = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 432 | let clean = loop { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 433 | match reader.recv::<Request>() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 434 | Ok(Some(Request::Usage { rss })) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 435 | if let Some(run) = lock(shared) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 436 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 437 | .get_mut(&id) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 438 | .and_then(|l| l.run.as_mut()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 439 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 440 | run.rss = rss; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 441 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 442 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 443 | Ok(Some(Request::Started { pid })) => pgid = Some(pid), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 444 | Ok(Some(Request::Done)) => break true, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 445 | Ok(Some(_)) => {} |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 446 | Ok(None) | Err(_) => break false, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 447 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 448 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 449 | if !clean && let Some(pgid) = pgid.filter(|p| *p > 1) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 450 | unsafe { libc::kill(-pgid, libc::SIGTERM) }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 451 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 452 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 453 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 454 | fn tick(shared: &Shared) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 455 | loop { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 456 | thread::sleep(Duration::from_secs(1)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 457 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 458 | st.refresh(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 459 | st.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 460 | st.fire_overdue(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 461 | if st.conns == 0 && st.leases.is_empty() && st.idle_since.elapsed() >= st.cfg.queue.linger.0 |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 462 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 463 | // a spawner holds this while it clears a stale socket, so taking it |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 464 | // here stops us unlinking our successor's socket on the way out |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 465 | if let Some(_guard) = try_spawn_lock() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 466 | let _ = fs::remove_file(paths::socket()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 467 | process::exit(0); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 468 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 469 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 470 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 471 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 472 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 473 | fn try_spawn_lock() -> Option<client::Flock> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 474 | let file = fs::OpenOptions::new() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 475 | .write(true) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 476 | .open(paths::spawn_lock()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 477 | .ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 478 | client::Flock::try_exclusive(file) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 479 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 480 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 481 | pub fn main(cfg: Config) -> ! { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 482 | // fork once more so the client that spawned us can reap its child now |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 483 | // instead of carrying a zombie for the length of its build |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 484 | match unsafe { libc::fork() } { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 485 | -1 => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 486 | eprintln!("roundabout: fork failed: {}", io::Error::last_os_error()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 487 | process::exit(1); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 488 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 489 | 0 => {} |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 490 | _ => process::exit(0), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 491 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 492 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 493 | let listener = match UnixListener::bind(paths::socket()) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 494 | Ok(l) => l, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 495 | Err(e) if e.kind() == io::ErrorKind::AddrInUse && client::connect().is_some() => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 496 | process::exit(0) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 497 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 498 | Err(e) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 499 | eprintln!("roundabout: bind: {e}"); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 500 | process::exit(1); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 501 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 502 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 503 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 504 | let shared: Shared = Arc::new(Mutex::new(State::new(cfg))); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 505 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 506 | let shared = shared.clone(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 507 | thread::spawn(move || tick(&shared)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 508 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 509 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 510 | for stream in listener.incoming() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 511 | let Ok(stream) = stream else { continue }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 512 | lock(&shared).conns += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 513 | let shared = shared.clone(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 514 | thread::spawn(move || { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 515 | if let Err(e) = serve(&shared, stream) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 516 | eprintln!("roundabout: {e}"); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 517 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 518 | let mut st = lock(&shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 519 | st.conns -= 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 520 | if st.leases.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 521 | st.idle_since = Instant::now(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 522 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 523 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 524 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 525 | process::exit(0) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 526 | } |