[verified] fix(file): pace chunks to fit rendezvous rate limit
All checks were successful
ci / test (pull_request) Successful in 2m42s
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:
parent
cb91ba2b78
commit
4bbc2de9ab
5 changed files with 122 additions and 4 deletions
|
|
@ -1,7 +1,10 @@
|
|||
//! M10: fail-closed file transfer — frames, names, assemble, slash parse.
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use onionwire::dispatch::{Kind, classify};
|
||||
use onionwire::file::{self, MAX_BYTES};
|
||||
use onionwire::ratelimit::TokenBucket;
|
||||
use onionwire::tui::{SlashCmd, parse_cmd};
|
||||
|
||||
#[test]
|
||||
|
|
@ -131,3 +134,69 @@ fn dispatch_fil_is_file_not_chat() {
|
|||
assert_eq!(classify(b"fil abc"), Kind::File);
|
||||
assert_eq!(classify(b"hello wire"), Kind::Chat);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_mib_seventeen_chunks_fit_default_bucket_when_paced() {
|
||||
let body = vec![0u8; MAX_BYTES];
|
||||
let chunks = file::chunks("file.bin", &body).unwrap();
|
||||
assert_eq!(
|
||||
chunks.len(),
|
||||
17,
|
||||
"1 MiB with an 8-char name must be 17 one-shot sessions"
|
||||
);
|
||||
|
||||
let t0 = Instant::now();
|
||||
let mut unpaced = TokenBucket::default();
|
||||
let dropped = (0..chunks.len())
|
||||
.filter(|_| !unpaced.try_acquire_at(t0))
|
||||
.count();
|
||||
assert_eq!(
|
||||
dropped, 7,
|
||||
"the bug: 17 back-to-back accepts blow the burst of 10"
|
||||
);
|
||||
|
||||
let mut paced = TokenBucket::default();
|
||||
for i in 0..chunks.len() {
|
||||
let t = t0 + file::CHUNK_PACE * i as u32;
|
||||
assert!(
|
||||
paced.try_acquire_at(t),
|
||||
"chunk {i} dropped under CHUNK_PACE={:?}",
|
||||
file::CHUNK_PACE
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn transient_chunk_failure_is_retried_and_transfer_completes() {
|
||||
let mut attempts = 0u32;
|
||||
file::send_chunks(1, |_| {
|
||||
attempts += 1;
|
||||
let fail = attempts == 1;
|
||||
async move { if fail { Err("rate-limit drop") } else { Ok(()) } }
|
||||
})
|
||||
.await
|
||||
.expect("one transient failure must not kill the transfer");
|
||||
assert_eq!(attempts, 2, "second attempt must succeed");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn exhausted_chunk_retries_return_err_and_skip_later_chunks() {
|
||||
let mut attempts = 0u32;
|
||||
let mut recorded = false;
|
||||
let err = file::send_chunks(2, |i| {
|
||||
attempts += 1;
|
||||
async move { Err(format!("fail chunk {i}")) }
|
||||
})
|
||||
.await
|
||||
.expect_err("permanent failure must not look like success");
|
||||
assert!(err.contains("fail chunk 0"), "{err}");
|
||||
assert_eq!(
|
||||
attempts,
|
||||
file::CHUNK_ATTEMPTS,
|
||||
"must stop after the first chunk exhausts retries"
|
||||
);
|
||||
if err.is_empty() {
|
||||
recorded = true;
|
||||
}
|
||||
assert!(!recorded, "caller must not record a success report");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue