openrtc-file-transfer 2.9.2

Optional, transport-independent file transfer protocol for OpenRTC streams.
Documentation
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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
457
458
459
460
461
462
463
464
465
466
467
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
515
516
517
518
519
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
//! Shared pacing of application body bytes. One controller serves all bulk
//! executors in a runtime; admission, ACKs and lifecycle control bypass it.
//! Call `acquire` before starting transport I/O deadlines. Dropping the future
//! cancels a queued slice without leaving a peer slot or timer behind.
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex};
use tokio::sync::{oneshot, Notify};
use tokio::time::{Duration, Instant};

pub const MAX_BULK_BURST_BYTES: u64 = 64 * 1024;
const MAX_PENDING: usize = 4096;
const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BulkDirection {
    Upload,
    Download,
}
impl BulkDirection {
    fn index(self) -> usize {
        match self {
            Self::Upload => 0,
            Self::Download => 1,
        }
    }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BulkRates {
    pub upload_bytes_per_second: Option<u64>,
    pub download_bytes_per_second: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BulkFlowError {
    InvalidRate,
    InvalidSlice,
    QueueFull,
    OwnerClosed,
}
impl std::fmt::Display for BulkFlowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::InvalidRate => "Bulk rate must be unset or a positive safe integer",
            Self::InvalidSlice => "Invalid bulk body slice",
            Self::QueueFull => "receiver_busy: bulk flow queue is full",
            Self::OwnerClosed => "execution_suspended: bulk flow owner closed",
        })
    }
}
impl std::error::Error for BulkFlowError {}
struct Pending {
    id: u64,
    bytes: u64,
    scope: Option<String>,
    ready: oneshot::Sender<()>,
}
struct ScopedBudget {
    rate: u64,
    tokens: f64,
    updated: Instant,
}
impl ScopedBudget {
    fn refill(&mut self) {
        let now = Instant::now();
        self.tokens = (self.tokens
            + now.saturating_duration_since(self.updated).as_secs_f64() * self.rate as f64)
            .min(MAX_BULK_BURST_BYTES as f64);
        self.updated = now;
    }
}
struct DirectionState {
    rate: Option<u64>,
    tokens: f64,
    updated: Instant,
    peers: HashMap<String, VecDeque<Pending>>,
    order: VecDeque<String>,
    scopes: HashMap<String, ScopedBudget>,
}
impl DirectionState {
    fn new() -> Self {
        Self {
            rate: None,
            tokens: MAX_BULK_BURST_BYTES as f64,
            updated: Instant::now(),
            peers: HashMap::new(),
            order: VecDeque::new(),
            scopes: HashMap::new(),
        }
    }
    fn refill(&mut self) {
        let now = Instant::now();
        if let Some(rate) = self.rate {
            self.tokens = (self.tokens
                + now.saturating_duration_since(self.updated).as_secs_f64() * rate as f64)
                .min(MAX_BULK_BURST_BYTES as f64);
        }
        self.updated = now;
    }
    fn admit_scope(&mut self, key: &str, rate: u64) -> Result<(), BulkFlowError> {
        if let Some(scope) = self.scopes.get(key) {
            return if scope.rate == rate {
                Ok(())
            } else {
                Err(BulkFlowError::InvalidRate)
            };
        }
        // A queued slice owns its scope. Other scopes may retire only after
        // their full burst could refill, preserving debt across short retries.
        let active: HashSet<_> = self
            .peers
            .values()
            .flat_map(|queue| queue.iter())
            .filter_map(|slice| slice.scope.clone())
            .collect();
        self.scopes.retain(|key, scope| {
            active.contains(key)
                || scope.updated.elapsed().as_secs_f64() * (scope.rate as f64)
                    < MAX_BULK_BURST_BYTES as f64
        });
        if self.scopes.len() >= MAX_PENDING {
            return Err(BulkFlowError::QueueFull);
        }
        self.scopes.insert(
            key.into(),
            ScopedBudget {
                rate,
                tokens: MAX_BULK_BURST_BYTES as f64,
                updated: Instant::now(),
            },
        );
        Ok(())
    }
    fn pump(&mut self) -> Option<Duration> {
        self.refill();
        let mut blocked = 0;
        let mut wait: Option<Duration> = None;
        while !self.order.is_empty() && blocked < self.order.len() {
            let peer = self.order.pop_front().expect("flow peer order");
            let queue = self.peers.get_mut(&peer).expect("flow peer order");
            while queue.front().is_some_and(|slice| slice.ready.is_closed()) {
                queue.pop_front();
            }
            if queue.is_empty() {
                self.peers.remove(&peer);
                continue;
            }
            let front = queue.front().expect("pending flow slice");
            let bytes = front.bytes;
            let scope_key = front.scope.clone();
            let global_delay = self
                .rate
                .filter(|_| self.tokens < bytes as f64)
                .map(|rate| (bytes as f64 - self.tokens) / rate as f64);
            let mut scoped_delay = None;
            if let Some(key) = &scope_key {
                let scope = self.scopes.get_mut(key).expect("queued flow scope");
                scope.refill();
                if scope.tokens < bytes as f64 {
                    let scoped = (bytes as f64 - scope.tokens) / scope.rate as f64;
                    scoped_delay = Some(scoped);
                }
            }
            // An aggregate-ready peer keeps its turn while global tokens
            // refill. Skipping it would let smaller slices starve a large one.
            if scoped_delay.is_none() {
                if let Some(delay) = global_delay {
                    let delay = Duration::from_secs_f64(delay.max(0.000_000_001));
                    self.order.push_front(peer);
                    return Some(wait.map_or(delay, |prior| prior.min(delay)));
                }
            }
            let delay = match (global_delay, scoped_delay) {
                (Some(global), Some(scoped)) => Some(global.max(scoped)),
                (global, scoped) => global.or(scoped),
            };
            if let Some(delay) = delay {
                // Do not reserve aggregate tokens for a scope that cannot run.
                // Other ready peers retain their fair opportunity this turn.
                let delay = Duration::from_secs_f64(delay.max(0.000_000_001));
                wait = Some(wait.map_or(delay, |prior| prior.min(delay)));
                self.order.push_back(peer);
                blocked += 1;
                continue;
            }
            if self.rate.is_some() {
                self.tokens -= bytes as f64;
            }
            if let Some(key) = scope_key {
                self.scopes.get_mut(&key).expect("queued flow scope").tokens -= bytes as f64;
            }
            let slice = queue.pop_front().expect("pending flow slice");
            if queue.is_empty() {
                self.peers.remove(&peer);
            } else {
                self.order.push_back(peer);
            }
            let _ = slice.ready.send(());
            blocked = 0;
        }
        wait
    }
}
struct FlowState {
    directions: [DirectionState; 2],
    next_id: u64,
}
struct FlowInner {
    state: Mutex<FlowState>,
    changed: Notify,
}
/// Clone this handle, rather than constructing a controller per transfer.
#[derive(Clone)]
pub struct BulkFlowController {
    inner: Arc<FlowInner>,
}
struct WaitingSlice {
    owner: Arc<FlowInner>,
    direction: usize,
    peer: String,
    id: u64,
}
impl Drop for WaitingSlice {
    fn drop(&mut self) {
        let mut state = self
            .owner
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let direction = &mut state.directions[self.direction];
        if let Some(queue) = direction.peers.get_mut(&self.peer) {
            queue.retain(|slice| slice.id != self.id);
            if queue.is_empty() {
                direction.peers.remove(&self.peer);
                direction.order.retain(|peer| peer != &self.peer);
            }
        }
        direction.pump();
        drop(state);
        self.owner.changed.notify_waiters();
    }
}
impl BulkFlowController {
    pub fn new(rates: BulkRates) -> Result<Self, BulkFlowError> {
        let result = Self {
            inner: Arc::new(FlowInner {
                state: Mutex::new(FlowState {
                    directions: [DirectionState::new(), DirectionState::new()],
                    next_id: 0,
                }),
                changed: Notify::new(),
            }),
        };
        result.configure(rates)?;
        Ok(result)
    }
    pub fn configure(&self, rates: BulkRates) -> Result<(), BulkFlowError> {
        let rates = [
            rates.upload_bytes_per_second,
            rates.download_bytes_per_second,
        ];
        if rates
            .into_iter()
            .flatten()
            .any(|rate| rate == 0 || rate > MAX_SAFE_INTEGER)
        {
            return Err(BulkFlowError::InvalidRate);
        }
        let mut state = self
            .inner
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        for (direction, rate) in state.directions.iter_mut().zip(rates) {
            direction.refill();
            direction.rate = rate;
            direction.pump();
        }
        drop(state);
        self.inner.changed.notify_waiters();
        Ok(())
    }
    pub async fn acquire(
        &self,
        direction: BulkDirection,
        peer: &str,
        bytes: u64,
    ) -> Result<(), BulkFlowError> {
        self.acquire_inner(direction, peer, None, bytes).await
    }
    /// Admit a body slice against both aggregate and immutable scoped rates
    /// in one decision. Reuse a stable scope across attempts; controls bypass it.
    pub async fn acquire_scoped(
        &self,
        direction: BulkDirection,
        peer: &str,
        scope: &str,
        bytes_per_second: Option<u64>,
        bytes: u64,
    ) -> Result<(), BulkFlowError> {
        if scope.is_empty()
            || scope.len() > 128
            || scope.chars().any(char::is_control)
            || bytes_per_second.is_some_and(|rate| rate == 0 || rate > MAX_SAFE_INTEGER)
        {
            return Err(BulkFlowError::InvalidRate);
        }
        self.acquire_inner(
            direction,
            peer,
            bytes_per_second.map(|rate| (scope, rate)),
            bytes,
        )
        .await
    }
    async fn acquire_inner(
        &self,
        direction: BulkDirection,
        peer: &str,
        scope: Option<(&str, u64)>,
        bytes: u64,
    ) -> Result<(), BulkFlowError> {
        if peer.is_empty()
            || peer.len() > 512
            || peer.chars().any(char::is_control)
            || bytes > MAX_BULK_BURST_BYTES
        {
            return Err(BulkFlowError::InvalidSlice);
        }
        if bytes == 0 {
            return Ok(());
        }
        let direction = direction.index();
        let (send, mut receive) = oneshot::channel();
        let id = {
            let mut state = self
                .inner
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let pending: usize = state
                .directions
                .iter()
                .flat_map(|state| state.peers.values())
                .map(VecDeque::len)
                .sum();
            if pending >= MAX_PENDING {
                return Err(BulkFlowError::QueueFull);
            }
            if let Some((key, rate)) = scope {
                state.directions[direction].admit_scope(key, rate)?;
            }
            let id = state
                .next_id
                .checked_add(1)
                .ok_or(BulkFlowError::QueueFull)?;
            state.next_id = id;
            let state = &mut state.directions[direction];
            if !state.peers.contains_key(peer) {
                state.order.push_back(peer.into());
            }
            state
                .peers
                .entry(peer.into())
                .or_default()
                .push_back(Pending {
                    id,
                    bytes,
                    scope: scope.map(|(key, _)| key.into()),
                    ready: send,
                });
            state.pump();
            id
        };
        let _lease = WaitingSlice {
            owner: self.inner.clone(),
            direction,
            peer: peer.into(),
            id,
        };
        loop {
            // Register before inspecting state so policy updates/cancellation
            // cannot be missed between choosing a deadline and sleeping.
            let changed = self.inner.changed.notified();
            tokio::pin!(changed);
            changed.as_mut().enable();
            let wait = {
                let mut state = self
                    .inner
                    .state
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                state.directions[direction].pump()
            };
            tokio::select! {
                result = &mut receive => return result.map_err(|_| BulkFlowError::OwnerClosed),
                _ = &mut changed => {},
                _ = async { match wait { Some(delay) => tokio::time::sleep(delay).await,
                    None => std::future::pending().await } } => {},
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test(start_paused = true)]
    async fn aggregate_ready_large_slice_keeps_its_turn_ahead_of_later_small_slices() {
        let flow = BulkFlowController::new(BulkRates {
            upload_bytes_per_second: Some(1024),
            ..BulkRates::default()
        })
        .unwrap();
        flow.acquire(BulkDirection::Upload, "initial", MAX_BULK_BURST_BYTES)
            .await
            .unwrap();
        let large = {
            let flow = flow.clone();
            tokio::spawn(async move {
                flow.acquire(BulkDirection::Upload, "large", MAX_BULK_BURST_BYTES)
                    .await
            })
        };
        tokio::task::yield_now().await;
        let small = {
            let flow = flow.clone();
            tokio::spawn(async move { flow.acquire(BulkDirection::Upload, "small", 1024).await })
        };
        tokio::task::yield_now().await;
        tokio::time::advance(Duration::from_secs(1)).await;
        tokio::task::yield_now().await;
        tokio::task::yield_now().await;
        assert!(
            !small.is_finished(),
            "Later small slices consumed credit owed to an aggregate-ready large peer"
        );
        assert!(!large.is_finished());
        tokio::time::advance(Duration::from_secs(63)).await;
        tokio::task::yield_now().await;
        tokio::task::yield_now().await;
        assert!(large.is_finished());
        large.await.unwrap().unwrap();
        assert!(!small.is_finished());
        tokio::time::advance(Duration::from_secs(1)).await;
        small.await.unwrap().unwrap();
    }

    // Native timers and dropped futures have different ownership from the JS
    // controller. Assert public grants against independently measured time.
    #[tokio::test(start_paused = true)]
    async fn parallel_clones_share_one_burst_and_rotate_peers_before_second_slices() {
        let flow = BulkFlowController::new(BulkRates {
            upload_bytes_per_second: Some(1024),
            download_bytes_per_second: Some(2048),
        })
        .unwrap();
        let start = Instant::now();
        flow.acquire(BulkDirection::Upload, "a", MAX_BULK_BURST_BYTES)
            .await
            .unwrap();
        let observed = Arc::new(Mutex::new(Vec::new()));
        let mut tasks = Vec::new();
        for (name, peer) in [("a1", "a"), ("a2", "a"), ("b1", "b")] {
            let (flow, observed) = (flow.clone(), observed.clone());
            tasks.push(tokio::spawn(async move {
                flow.acquire(BulkDirection::Upload, peer, MAX_BULK_BURST_BYTES)
                    .await
                    .unwrap();
                observed
                    .lock()
                    .unwrap()
                    .push((name, Instant::now().duration_since(start)));
            }));
            tokio::task::yield_now().await;
        }
        assert!(
            observed.lock().unwrap().is_empty(),
            "clones must not each get a fresh burst"
        );
        // Upload consumption cannot consume the independent download burst.
        flow.acquire(BulkDirection::Download, "b", MAX_BULK_BURST_BYTES)
            .await
            .unwrap();
        for _ in 0..3 {
            tokio::time::advance(Duration::from_secs(64)).await;
            tokio::task::yield_now().await;
            tokio::task::yield_now().await;
        }
        for task in tasks {
            task.await.unwrap();
        }
        let events = observed.lock().unwrap();
        assert_eq!(
            events.iter().map(|(name, _)| *name).collect::<Vec<_>>(),
            ["a1", "b1", "a2"]
        );
        for (index, (_, elapsed)) in events.iter().enumerate() {
            let attempted = (index as u64 + 2) * MAX_BULK_BURST_BYTES;
            assert!(attempted <= MAX_BULK_BURST_BYTES + elapsed.as_secs() * 1024);
        }
    }

    #[tokio::test(start_paused = true)]
    async fn cancelled_head_releases_waiters_and_live_limits_wake_only_their_direction() {
        let flow = BulkFlowController::new(BulkRates {
            upload_bytes_per_second: Some(1024),
            download_bytes_per_second: Some(1024),
        })
        .unwrap();
        for direction in [BulkDirection::Upload, BulkDirection::Download] {
            flow.acquire(direction, "initial", MAX_BULK_BURST_BYTES)
                .await
                .unwrap();
        }
        let head = {
            let flow = flow.clone();
            tokio::spawn(async move {
                flow.acquire(BulkDirection::Upload, "cancelled", MAX_BULK_BURST_BYTES)
                    .await
            })
        };
        tokio::task::yield_now().await;
        let smaller = {
            let flow = flow.clone();
            tokio::spawn(async move { flow.acquire(BulkDirection::Upload, "other", 1024).await })
        };
        tokio::task::yield_now().await;
        head.abort();
        let _ = head.await;
        tokio::time::advance(Duration::from_secs(1)).await;
        tokio::task::yield_now().await;
        assert!(
            smaller.is_finished(),
            "cancelled head cannot leave a 64-second deadline behind"
        );
        smaller.await.unwrap().unwrap();
        let upload = {
            let flow = flow.clone();
            tokio::spawn(async move {
                flow.acquire(BulkDirection::Upload, "a", MAX_BULK_BURST_BYTES)
                    .await
            })
        };
        let download = {
            let flow = flow.clone();
            tokio::spawn(async move {
                flow.acquire(BulkDirection::Download, "a", MAX_BULK_BURST_BYTES)
                    .await
            })
        };
        tokio::task::yield_now().await;
        assert!(!upload.is_finished());
        assert!(!download.is_finished());
        flow.configure(BulkRates {
            upload_bytes_per_second: None,
            download_bytes_per_second: Some(1024),
        })
        .unwrap();
        tokio::task::yield_now().await;
        assert!(upload.is_finished());
        assert!(!download.is_finished());
        upload.await.unwrap().unwrap();
        download.abort();
        let _ = download.await;
        assert_eq!(
            flow.acquire(BulkDirection::Upload, "a", MAX_BULK_BURST_BYTES + 1)
                .await,
            Err(BulkFlowError::InvalidSlice)
        );
        assert_eq!(
            flow.configure(BulkRates {
                upload_bytes_per_second: Some(0),
                download_bytes_per_second: None
            }),
            Err(BulkFlowError::InvalidRate)
        );
    }
}