atmos/os_lib/web_apis/
xhr_sched.rs1pub const MAX_WORKERS: usize = 3;
14
15pub const MAX_PER_HOST: usize = 2;
18
19pub fn host_key(abs_url: &str) -> alloc::string::String {
22 let rest = if let Some(r) = abs_url.strip_prefix("https://") {
23 r
24 } else if let Some(r) = abs_url.strip_prefix("http://") {
25 r
26 } else {
27 return alloc::string::String::new();
28 };
29 let end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
30 let auth = rest.get(..end).unwrap_or("");
31 let host = match auth.rsplit_once('@') {
32 Some((_, h)) => h,
33 None => auth,
34 };
35 host.to_ascii_lowercase()
36}
37
38pub fn pick_next<'a>(
46 queued: impl Iterator<Item = (&'a str, u8)>,
47 in_flight: &[(&str, u8)],
48 max_workers: usize,
49 max_per_host: usize,
50) -> Option<usize> {
51 if in_flight.len() >= max_workers {
52 return None;
53 }
54 let mut best: Option<(usize, usize)> = None; for (i, (host, group)) in queued.enumerate() {
56 let by_host = in_flight.iter().filter(|f| f.0 == host).count();
57 if by_host >= max_per_host {
58 continue;
59 }
60 let by_group = in_flight.iter().filter(|f| f.1 == group).count();
61 if best.is_none_or(|(b, _)| by_group < b) {
62 best = Some((by_group, i));
63 if by_group == 0 {
64 break;
66 }
67 }
68 }
69 best.map(|(_, i)| i)
70}
71
72pub fn need_worker(workers: usize, in_flight: usize, startable: bool, max_workers: usize) -> bool {
76 startable && workers < max_workers && workers <= in_flight
77}
78
79pub struct ClosedRing {
86 slots: [core::sync::atomic::AtomicU64; CLOSED_RING_LEN],
87 next: core::sync::atomic::AtomicUsize,
88}
89
90pub const CLOSED_RING_LEN: usize = 32;
92
93impl Default for ClosedRing {
94 fn default() -> Self {
95 Self::new()
96 }
97}
98
99impl ClosedRing {
100 pub const fn new() -> Self {
101 Self {
102 slots: [const { core::sync::atomic::AtomicU64::new(0) }; CLOSED_RING_LEN],
103 next: core::sync::atomic::AtomicUsize::new(0),
104 }
105 }
106
107 pub fn push(&self, id: u64) {
109 if id == 0 {
110 return;
111 }
112 let i = self.next.fetch_add(1, core::sync::atomic::Ordering::AcqRel) % CLOSED_RING_LEN;
113 if let Some(s) = self.slots.get(i) {
114 s.store(id, core::sync::atomic::Ordering::Release);
115 }
116 }
117
118 pub fn contains(&self, id: u64) -> bool {
120 id != 0 && self.slots.iter().any(|s| s.load(core::sync::atomic::Ordering::Acquire) == id)
121 }
122}