Merge remote-tracking branch 'origin/main' into wt/t_0d3b0f80

This commit is contained in:
Sirius DevOps 2026-09-12 00:20:26 -04:00
commit 494a0e4099
No known key found for this signature in database
7 changed files with 1178 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};
@ -32,6 +34,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;
@ -179,6 +188,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 {

View file

@ -487,12 +487,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())?