message saved if client crashes on retreival

This commit is contained in:
yves committed 2026-11-20 22:00:54 +01:00
1 parent 6d3ea3e2a6
commit 006bedc728
11 files changed
+591 -72

No files matched your search

+38 -4
View File
@@ -39,6 +39,12 @@ pub struct Limits {
pub max_deposits: usize,
pub max_payload_bytes: usize,
pub max_mailboxes_per_pull: usize,
/// Messages returned by one pull (the client pulls again when `more` is set).
pub max_pull_messages: usize,
/// Payload bytes returned by one pull (at least one message is always returned).
pub max_pull_bytes: usize,
/// Acknowledged ids per mailbox in one pull.
pub max_acks: usize,
pub max_wait: Duration,
pub max_invitation_ttl: Duration,
pub replay_ttl: Duration,
@@ -51,6 +57,9 @@ impl Default for Limits {
max_deposits: 128,
max_payload_bytes: 8 * 1024 * 1024,
max_mailboxes_per_pull: 512,
max_pull_messages: 256,
max_pull_bytes: 16 * 1024 * 1024,
max_acks: 1024,
max_wait: Duration::from_secs(120),
max_invitation_ttl: Duration::from_secs(7 * 86_400),
replay_ttl: Duration::from_secs(2 * rpc::AUTH_WINDOW_SECS),
@@ -196,13 +205,23 @@ async fn pull(st: &AppState, request_id: &[u8], p: v2::Pull, now: u64) -> HResul
let sign = st.keys.sign.public();
let mut mailboxes = Vec::with_capacity(p.auths.len());
for a in &p.auths {
if a.acks.len() > st.limits.max_acks || a.acks.iter().any(|id| id.len() > 64) {
return Err((Code::BadRequest, "bad acks"));
}
let mb = rpc::verify_mailbox_auth(&sign, request_id, a, now)
.map_err(|_| (Code::Unauthorized, "bad mailbox auth"))?;
mailboxes.push(mb.0);
}
// Subscribe before draining, so a deposit landing in between is not missed.
// Acknowledged messages were persisted by the client: delete them first.
for (mb, a) in mailboxes.iter().zip(&p.auths) {
if !a.acks.is_empty() {
st.store.ack(*mb, &a.acks).await.map_err(store_err)?;
}
}
// Subscribe before reading, so a deposit landing in between is not missed.
let mut rx = st.notify.subscribe();
let mut messages = st.store.drain(&mailboxes).await.map_err(store_err)?;
let limit = st.limits.max_pull_messages;
let mut messages = st.store.fetch(&mailboxes, limit + 1).await.map_err(store_err)?;
let wait = Duration::from_secs(p.wait_seconds as u64).min(st.limits.max_wait);
if messages.is_empty() && !wait.is_zero() {
let wanted: HashSet<Mailbox> = mailboxes.iter().copied().collect();
@@ -217,9 +236,24 @@ async fn pull(st: &AppState, request_id: &[u8], p: v2::Pull, now: u64) -> HResul
}
})
.await;
messages = st.store.drain(&mailboxes).await.map_err(store_err)?;
messages = st.store.fetch(&mailboxes, limit + 1).await.map_err(store_err)?;
}
Ok(Resp::PullResult(v2::PullResult { messages }))
// Honour the count and size budgets; always return at least one message.
let mut more = messages.len() > limit;
messages.truncate(limit);
let mut bytes = 0usize;
let keep = messages
.iter()
.position(|m| {
bytes += m.payload.len();
bytes > st.limits.max_pull_bytes
})
.map_or(messages.len(), |i| i.max(1));
if keep < messages.len() {
messages.truncate(keep);
more = true;
}
Ok(Resp::PullResult(v2::PullResult { messages, more }))
}
/// Serve on an already bound listener until the future is dropped.