use super::{Inner, locked};
use crate::wire::{Envelope, Request, Response};
use anyhow::{Result, bail};
use iroh::EndpointId;
use std::collections::HashSet;
use std::sync::{Arc, Weak};
use std::time::{Duration, Instant};
const KEEP_FOR: Duration = Duration::from_hours(24);
const MAX_WAITING: usize = 200;
const RETRY_EVERY: Duration = Duration::from_secs(30);
const RESYNC_EVERY: Duration = Duration::from_hours(6);
pub(super) struct Queued {
to: EndpointId,
envelope: Envelope,
since: Instant,
}
impl Inner {
pub(super) fn queue(&self, to: EndpointId, envelope: Envelope) -> Result<()> {
let mut outbox = locked(&self.outbox);
if outbox.len() >= MAX_WAITING {
bail!("too many letters are waiting for computers that are away");
}
outbox.push(Queued { to, envelope, since: Instant::now() });
Ok(())
}
pub(super) fn flush(self: Arc<Self>, id: EndpointId) {
let waiting: Vec<Queued> = {
let mut outbox = locked(&self.outbox);
let (theirs, rest) = std::mem::take(&mut *outbox).into_iter().partition(|q| q.to == id);
*outbox = rest;
theirs
};
if waiting.is_empty() {
return;
}
tokio::spawn(async move {
let mut left = waiting.into_iter();
while let Some(q) = left.next() {
match self.call(id, &Request::Deliver { envelope: q.envelope.clone() }).await {
Ok(Response::Refused { reason }) => {
tracing::warn!(id = %q.envelope.id, reason, "a waiting letter was refused");
}
Ok(Response::Later { .. }) | Err(_) => {
let mut outbox = locked(&self.outbox);
outbox.push(q);
outbox.extend(left);
return;
}
Ok(_) => {}
}
}
});
}
pub(super) async fn retry(self: &Arc<Self>) {
let mut due: HashSet<EndpointId> = {
let mut outbox = locked(&self.outbox);
outbox.retain(|q| q.since.elapsed() < KEEP_FOR);
outbox.iter().map(|q| q.to).collect()
};
due.extend(self.unconnected());
self.hello_each(due).await;
}
}
pub(super) async fn retry_loop(inner: Weak<Inner>) {
let mut synced = Instant::now();
loop {
tokio::time::sleep(RETRY_EVERY).await;
let Some(inner) = inner.upgrade() else { return };
if synced.elapsed() >= RESYNC_EVERY {
synced = Instant::now();
inner.clone().hello_all();
}
inner.retry().await;
}
}