1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
//! Fiber pool primitive supporting live resize via a dynamic
//! concurrency control (SRD 23 ยง"Fiber executor").
//!
//! The pool owns one stop-flag per fiber. A fiber checks its
//! flag at every cycle boundary and exits cooperatively when
//! the flag is set โ no mid-op termination, ever. The pool
//! tracks the count of *intended-active* fibers; the applier
//! responds to a write on the `concurrency` control by either
//! spawning new fibers (scale-up) or flagging the most recently
//! spawned ones for exit (scale-down).
//!
//! Pool ownership lives in the activity executor; the applier
//! is registered on the activity's `concurrency` control. The
//! cooperative-exit window is bounded by one cycle of work โ
//! the longest a scaled-down fiber stays alive is the time
//! its current op takes to complete plus one acquire from
//! the rate limiter.
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use nmbrs_metrics::controls::ControlApplier;
/// Per-fiber cooperative-exit flag. Each fiber owns one of
/// these and checks it at the top of its cycle loop.
pub type StopFlag = Arc<AtomicBool>;
/// Spawner closure: builds and `tokio::spawn`s a fiber given
/// its stop-flag. Returns the join handle so the pool can wait
/// for the fiber to exit if needed.
pub type FiberSpawner = Box<dyn Fn(StopFlag) -> tokio::task::JoinHandle<()> + Send + Sync>;
/// Owner of the running set of fibers for one activity. The
/// pool exposes `resize(target)` for the applier and
/// `register_applier(...)` to wire the control's write surface.
pub struct FiberPool {
/// FIFO of (stop_flag, join_handle) โ newest spawn at the
/// back. Scale-down signals from the back; scale-up appends.
fibers: Mutex<Vec<(StopFlag, tokio::task::JoinHandle<()>)>>,
spawner: FiberSpawner,
}
impl FiberPool {
/// Create a pool with no fibers. Use [`Self::spawn_initial`]
/// to seed N fibers at activity start.
pub fn new(spawner: FiberSpawner) -> Self {
Self {
fibers: Mutex::new(Vec::new()),
spawner,
}
}
/// Seed the pool with `count` fibers. Call once at activity
/// start, after the rate limiter and other shared state is
/// in place. Subsequent changes go through [`Self::resize`].
pub fn spawn_initial(&self, count: usize) {
for _ in 0..count {
self.spawn_one();
}
}
/// Live count of intended-active fibers (those whose
/// stop-flag has not been set). May briefly diverge from
/// the actual running count while a flagged fiber is still
/// completing its current op.
pub fn active_count(&self) -> usize {
let g = self.fibers.lock().unwrap_or_else(|e| e.into_inner());
g.iter()
.filter(|(flag, _)| !flag.load(Ordering::Relaxed))
.count()
}
/// Total count including fibers that have been flagged for
/// exit but haven't drained yet. Useful for diagnostics.
pub fn tracked_count(&self) -> usize {
let g = self.fibers.lock().unwrap_or_else(|e| e.into_inner());
g.len()
}
/// Reconcile the pool to `target` intended-active fibers.
///
/// - If `target > active_count()`: spawn `target - active`
/// new fibers via the registered spawner.
/// - If `target < active_count()`: signal
/// `active - target` of the most recently spawned fibers
/// to exit. They wind down at their next cycle boundary.
/// - Otherwise: no-op.
///
/// Returns the new active count (may be the same as before
/// if no change was needed).
pub fn resize(&self, target: usize) -> usize {
let active = self.active_count();
if target == active {
return active;
}
if target > active {
for _ in 0..(target - active) {
self.spawn_one();
}
target
} else {
// Signal the most-recently-spawned `active - target`
// fibers to exit. We walk from the back, skipping
// any that are already flagged.
let mut g = self.fibers.lock().unwrap_or_else(|e| e.into_inner());
let mut to_signal = active - target;
for (flag, _) in g.iter_mut().rev() {
if to_signal == 0 {
break;
}
if !flag.load(Ordering::Relaxed) {
flag.store(true, Ordering::Release);
to_signal -= 1;
}
}
target
}
}
/// Reap any fibers whose join handles report `is_finished()`.
/// Safe to call periodically for diagnostics; not required
/// for correctness.
pub fn reap_finished(&self) {
let mut g = self.fibers.lock().unwrap_or_else(|e| e.into_inner());
g.retain(|(_flag, handle)| !handle.is_finished());
}
fn spawn_one(&self) {
let flag: StopFlag = Arc::new(AtomicBool::new(false));
let handle = (self.spawner)(flag.clone());
let mut g = self.fibers.lock().unwrap_or_else(|e| e.into_inner());
g.push((flag, handle));
}
}
/// Applier that reconciles a [`FiberPool`] to a `Control<u32>`
/// concurrency write. Register one on the activity's
/// `concurrency` control:
///
/// ```ignore
/// concurrency_control.register_applier(
/// ConcurrencyApplier::new(pool.clone()),
/// );
/// ```
pub struct ConcurrencyApplier {
pool: Arc<FiberPool>,
}
impl ConcurrencyApplier {
pub fn new(pool: Arc<FiberPool>) -> Self {
Self { pool }
}
}
impl ControlApplier<u32> for ConcurrencyApplier {
fn apply(&self, value: u32) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + '_>> {
let pool = self.pool.clone();
Box::pin(async move {
// resize itself is sync; wrap in async to satisfy
// the trait. Spawning new tasks happens inside the
// resize call, which is fine to do from any tokio
// context.
let actual = pool.resize(value as usize);
if actual == value as usize {
Ok(())
} else {
Err(format!(
"concurrency reconcile reached {actual}, target was {value}",
))
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
use std::time::Duration;
fn worker_spawner(ops_counter: Arc<AtomicUsize>) -> FiberSpawner {
Box::new(move |stop: StopFlag| {
let counter = ops_counter.clone();
tokio::spawn(async move {
while !stop.load(Ordering::Acquire) {
counter.fetch_add(1, Ordering::Relaxed);
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
})
}
#[tokio::test]
async fn spawn_initial_seeds_target_count() {
let counter = Arc::new(AtomicUsize::new(0));
let pool = FiberPool::new(worker_spawner(counter.clone()));
pool.spawn_initial(4);
// Give the workers a tick to start incrementing.
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.active_count(), 4);
assert!(counter.load(Ordering::Relaxed) >= 4);
// Cleanup.
pool.resize(0);
tokio::time::sleep(Duration::from_millis(20)).await;
}
#[tokio::test]
async fn resize_up_spawns_additional_fibers() {
let counter = Arc::new(AtomicUsize::new(0));
let pool = FiberPool::new(worker_spawner(counter.clone()));
pool.spawn_initial(2);
let new_count = pool.resize(5);
assert_eq!(new_count, 5);
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.active_count(), 5);
pool.resize(0);
tokio::time::sleep(Duration::from_millis(20)).await;
}
#[tokio::test]
async fn resize_down_flags_newest_fibers_to_exit() {
let counter = Arc::new(AtomicUsize::new(0));
let pool = FiberPool::new(worker_spawner(counter.clone()));
pool.spawn_initial(5);
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.active_count(), 5);
let new_count = pool.resize(2);
assert_eq!(new_count, 2);
// Active count drops immediately because the flag is
// set on three of them.
assert_eq!(pool.active_count(), 2);
// Wait for the flagged fibers to drain.
for _ in 0..40 {
tokio::time::sleep(Duration::from_millis(5)).await;
pool.reap_finished();
if pool.tracked_count() == 2 {
break;
}
}
assert_eq!(pool.tracked_count(), 2);
pool.resize(0);
tokio::time::sleep(Duration::from_millis(20)).await;
}
#[tokio::test]
async fn resize_to_same_count_is_noop() {
let counter = Arc::new(AtomicUsize::new(0));
let pool = FiberPool::new(worker_spawner(counter.clone()));
pool.spawn_initial(3);
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.resize(3), 3);
assert_eq!(pool.tracked_count(), 3);
pool.resize(0);
tokio::time::sleep(Duration::from_millis(20)).await;
}
#[tokio::test]
async fn applier_drives_pool_through_control() {
use nmbrs_metrics::controls::{ControlBuilder, ControlOrigin};
let counter = Arc::new(AtomicUsize::new(0));
let pool = Arc::new(FiberPool::new(worker_spawner(counter.clone())));
pool.spawn_initial(2);
tokio::time::sleep(Duration::from_millis(20)).await;
let control: nmbrs_metrics::controls::Control<u32> =
ControlBuilder::new("concurrency", 2u32)
.reify_as_gauge(|v| Some(*v as f64))
.from_f64(|v| Ok(v as u32))
.build();
control.register_applier(ConcurrencyApplier::new(pool.clone()));
// Bump concurrency through the control surface.
control.set(6, ControlOrigin::Test).await.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.active_count(), 6);
// Drop back down.
control.set(1, ControlOrigin::Test).await.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(pool.active_count(), 1);
pool.resize(0);
tokio::time::sleep(Duration::from_millis(20)).await;
}
}