[verified] fix(file): pace chunks to fit rendezvous rate limit
All checks were successful
ci / test (pull_request) Successful in 2m42s

Sender-side only. First chunk attempt is immediate; later attempts
(retries and later chunks) wait 3s. Three attempts per chunk, then
fail closed. Token bucket 30/60s burst 10 is unchanged.

A 1 MiB file is 17 one-shot sessions; pacing floor is 16*3s = 48s
plus handshake time. Latency trade, not a security fix.
This commit is contained in:
Sirius DevOps 2026-09-11 23:49:29 -04:00
commit 4bbc2de9ab
No known key found for this signature in database
5 changed files with 122 additions and 4 deletions

View file

@ -2,9 +2,11 @@
use std::collections::HashMap;
use std::fs::{self, OpenOptions};
use std::future::Future;
use std::io::{Read, Write};
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::time::Duration;
use rand::RngCore;
use sha2::{Digest, Sha256};
@ -31,6 +33,13 @@ impl From<std::io::Error> for Error {
}
pub const MAX_BYTES: usize = 1024 * 1024;
/// Minimum interval between chunk send *attempts*. First attempt of the first
/// chunk fires immediately; every later attempt (retries and later chunks)
/// waits this long. 3s is a margin under the receiver's 0.5/s rendezvous
/// refill (30/60s, burst 10).
pub const CHUNK_PACE: Duration = Duration::from_secs(3);
/// Bounded attempts per chunk, counting the first try.
pub const CHUNK_ATTEMPTS: u32 = 3;
const PREFIX: &[u8] = b"fil ";
const NOISE_TAG: usize = 16;
const NAME_MAX: usize = 128;
@ -177,6 +186,37 @@ pub fn chat_line(name: &str, nbytes: usize) -> String {
format!("[file] {name} ({nbytes} bytes)")
}
/// Pace and retry `n` one-shot chunk sends. `send(i)` is one attempt at chunk
/// `i`. Fail closed: exhausting attempts on a chunk aborts the rest. No
/// outbox, no resume.
pub async fn send_chunks<E, F, Fut>(n: usize, mut send: F) -> std::result::Result<(), E>
where
F: FnMut(usize) -> Fut,
Fut: Future<Output = std::result::Result<(), E>>,
{
let mut first = true;
for i in 0..n {
let mut last = None;
for _ in 0..CHUNK_ATTEMPTS {
if !first {
tokio::time::sleep(CHUNK_PACE).await;
}
first = false;
match send(i).await {
Ok(()) => {
last = None;
break;
}
Err(e) => last = Some(e),
}
}
if let Some(e) = last {
return Err(e);
}
}
Ok(())
}
impl Inbox {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {