[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
40
src/file.rs
40
src/file.rs
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -486,12 +486,13 @@ impl Node {
|
|||
}
|
||||
|
||||
/// One-shot file to a friend. Fail closed; no outbox, no resume.
|
||||
/// Chunks are paced (`file::CHUNK_PACE`) with bounded per-chunk retry so
|
||||
/// a 1 MiB transfer fits the receiver's rendezvous token bucket.
|
||||
pub async fn send_file(&self, friend_pk: &[u8], path: &Path) -> Result<String, String> {
|
||||
let (name, bytes) = file::read_limited(path).map_err(|e| e.to_string())?;
|
||||
let chunks = file::chunks(&name, &bytes).map_err(|e| e.to_string())?;
|
||||
for chunk in &chunks {
|
||||
self.send_once(friend_pk, &file::encode(chunk)).await?;
|
||||
}
|
||||
let frames: Vec<Vec<u8>> = chunks.iter().map(file::encode).collect();
|
||||
file::send_chunks(frames.len(), |i| self.send_once(friend_pk, &frames[i])).await?;
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
|
|
|
|||
Loading…
Reference in a new issue