message saved if client crashes on retreival
This commit is contained in:
1 parent
6d3ea3e2a6
commit
006bedc728
11 files changed
+591
-72
No files matched your search
+154
-21
@@ -28,7 +28,8 @@ use meow_proto::v2::{self, request::Body as Req, response::Body as Resp};
|
||||
use meow_proto::Message;
|
||||
|
||||
pub use meow_core::contact::ContactState as State;
|
||||
pub use profile::{Content, HistoryEntry};
|
||||
use profile::RetiredMailbox;
|
||||
pub use profile::{Content, HistoryEntry, PendingAck};
|
||||
|
||||
/// Maximum file size sent in a single message (chunking: roadmap 5.7).
|
||||
pub const MAX_FILE_BYTES: usize = 4 * 1024 * 1024;
|
||||
@@ -109,6 +110,17 @@ pub struct SendReport {
|
||||
}
|
||||
|
||||
/// One identity, with its servers, contacts and history.
|
||||
/// Acknowledged ids sent per mailbox and pull (server limit: 1024).
|
||||
const MAX_ACKS_PER_MAILBOX: usize = 512;
|
||||
|
||||
#[derive(Default)]
|
||||
struct PullRound {
|
||||
/// Messages to process.
|
||||
new: Vec<v2::StoredMessage>,
|
||||
/// A server held messages back.
|
||||
more: bool,
|
||||
}
|
||||
|
||||
pub struct MeowClient {
|
||||
path: PathBuf,
|
||||
password: String,
|
||||
@@ -268,8 +280,10 @@ impl MeowClient {
|
||||
|
||||
pub async fn remove_contact(&mut self, name: &str) -> Result<()> {
|
||||
let i = self.contact_index(name)?;
|
||||
let before = self.contact_mailboxes();
|
||||
let c = self.profile.contacts.remove(i);
|
||||
self.profile.history.retain(|h| h.contact != c.id);
|
||||
self.retire_mailboxes(before);
|
||||
self.save().await
|
||||
}
|
||||
|
||||
@@ -521,45 +535,134 @@ impl MeowClient {
|
||||
/// Pull every mailbox on every server (one request per server), process what
|
||||
/// arrived and return the resulting events. With `wait > 0` the servers hold the
|
||||
/// request until something arrives or the delay expires (long-poll).
|
||||
///
|
||||
/// Delivery is at-least-once: servers keep messages until they are acknowledged,
|
||||
/// and a message is acknowledged (in the next pull) only once its effects are saved.
|
||||
/// After a round that brought messages, `sync` pulls again without waiting, which
|
||||
/// sends the acknowledgements and fetches what the servers held back.
|
||||
pub async fn sync(&mut self, wait: Duration) -> Result<Vec<Event>> {
|
||||
// server (by signing key) → (server, mailbox keys)
|
||||
let mut jobs: BTreeMap<SignPublic, (ServerInfo, Vec<SigningKeyPair>)> = BTreeMap::new();
|
||||
for c in &self.profile.contacts {
|
||||
for (kp, servers) in c.mailboxes() {
|
||||
for s in servers {
|
||||
jobs.entry(s.sign_key)
|
||||
.or_insert_with(|| (s.clone(), Vec::new()))
|
||||
.1
|
||||
.push(kp.clone());
|
||||
const MAX_ROUNDS: usize = 16;
|
||||
let mut events = Vec::new();
|
||||
let mut wait = wait;
|
||||
for round in 0..MAX_ROUNDS {
|
||||
let r = match self.pull_round(wait).await {
|
||||
Ok(r) => r,
|
||||
// A later round only flushes acks and backlog: keep what we already got.
|
||||
Err(e) if round > 0 => {
|
||||
tracing::warn!("follow-up pull failed: {e}");
|
||||
break;
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let got_new = !r.new.is_empty();
|
||||
if got_new {
|
||||
events.extend(self.process(r.new).await?);
|
||||
}
|
||||
// Pull again (without waiting) only to acknowledge what just arrived and to
|
||||
// fetch what servers held back. Redeliveries alone do not loop.
|
||||
if !got_new && !r.more {
|
||||
break;
|
||||
}
|
||||
wait = Duration::ZERO;
|
||||
}
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
/// One pull per server, carrying the pending acks. New (not yet processed) messages
|
||||
/// are returned and recorded as pending acks; they are saved by [`Self::process`].
|
||||
async fn pull_round(&mut self, wait: Duration) -> Result<PullRound> {
|
||||
// server (by signing key) → (server, mailbox keys)
|
||||
let pending = &self.profile.pending_acks;
|
||||
self.profile.retired_mailboxes.retain(|r| {
|
||||
pending
|
||||
.iter()
|
||||
.any(|a| a.mailbox.as_slice() == r.key.public().as_bytes().as_slice())
|
||||
});
|
||||
let mut jobs: BTreeMap<SignPublic, (ServerInfo, Vec<SigningKeyPair>)> = BTreeMap::new();
|
||||
let retired = self
|
||||
.profile
|
||||
.retired_mailboxes
|
||||
.iter()
|
||||
.map(|r| (r.key.clone(), r.servers.clone()));
|
||||
for (kp, servers) in self.contact_mailboxes().into_iter().chain(retired) {
|
||||
for s in servers {
|
||||
jobs.entry(s.sign_key)
|
||||
.or_insert_with(|| (s.clone(), Vec::new()))
|
||||
.1
|
||||
.push(kp.clone());
|
||||
}
|
||||
}
|
||||
// Drop acks nobody can send any more (contact or server removed).
|
||||
self.profile.pending_acks.retain(|a| {
|
||||
jobs.get(&a.server).is_some_and(|(_, kps)| {
|
||||
kps.iter()
|
||||
.any(|kp| kp.public().as_bytes().as_slice() == a.mailbox.as_slice())
|
||||
})
|
||||
});
|
||||
let mut round = PullRound::default();
|
||||
if jobs.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
return Ok(round);
|
||||
}
|
||||
let wait_s = wait.as_secs().min(u32::MAX as u64) as u32;
|
||||
let calls = jobs.values().map(|(server, kps)| {
|
||||
let pending = &self.profile.pending_acks;
|
||||
let mut sent_acks: Vec<Vec<PendingAck>> = Vec::with_capacity(jobs.len());
|
||||
let mut requests = Vec::with_capacity(jobs.len());
|
||||
for (server, kps) in jobs.values() {
|
||||
let rid = meow_crypto::random_bytes::<16>().to_vec();
|
||||
let ts = now();
|
||||
let auths = kps.iter().map(|kp| rpc::mailbox_auth(kp, server, &rid, ts)).collect();
|
||||
let req = v2::Request {
|
||||
let mut sent = Vec::new();
|
||||
let auths = kps
|
||||
.iter()
|
||||
.map(|kp| {
|
||||
let mb = kp.public();
|
||||
let acks: Vec<Vec<u8>> = pending
|
||||
.iter()
|
||||
.filter(|a| a.server == server.sign_key && a.mailbox.as_slice() == mb.as_bytes().as_slice())
|
||||
.take(MAX_ACKS_PER_MAILBOX)
|
||||
.inspect(|a| sent.push((*a).clone()))
|
||||
.map(|a| a.id.clone())
|
||||
.collect();
|
||||
rpc::mailbox_auth(kp, server, &rid, acks, ts)
|
||||
})
|
||||
.collect();
|
||||
sent_acks.push(sent);
|
||||
requests.push(v2::Request {
|
||||
request_id: rid,
|
||||
timestamp: ts,
|
||||
body: Some(Req::Pull(v2::Pull {
|
||||
auths,
|
||||
wait_seconds: wait_s,
|
||||
})),
|
||||
};
|
||||
self.transport.call_request(server, req, wait)
|
||||
});
|
||||
});
|
||||
}
|
||||
let calls = jobs
|
||||
.values()
|
||||
.zip(requests)
|
||||
.map(|((server, _), req)| self.transport.call_request(server, req, wait));
|
||||
let results = join_all(calls).await;
|
||||
|
||||
let mut stored = Vec::new();
|
||||
let mut failures = 0;
|
||||
let mut last_err = None;
|
||||
for ((server, _), r) in jobs.values().zip(results) {
|
||||
for (((server, _), r), sent) in jobs.values().zip(results).zip(sent_acks) {
|
||||
match r {
|
||||
Ok(Resp::PullResult(p)) => stored.extend(p.messages),
|
||||
Ok(Resp::PullResult(p)) => {
|
||||
// The server applied our acks before answering. Forgetting them
|
||||
// needs no save: at worst they are sent again (acks are idempotent).
|
||||
self.profile.pending_acks.retain(|a| !sent.contains(a));
|
||||
round.more |= p.more;
|
||||
for m in p.messages {
|
||||
let ack = PendingAck {
|
||||
server: server.sign_key,
|
||||
mailbox: m.mailbox.clone(),
|
||||
id: m.id.clone(),
|
||||
};
|
||||
// Already processed (its ack was lost): only re-acknowledge.
|
||||
if !self.profile.pending_acks.contains(&ack) {
|
||||
self.profile.pending_acks.push(ack);
|
||||
round.new.push(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(_) => failures += 1,
|
||||
Err(e) => {
|
||||
tracing::warn!("pull on {} failed: {e}", server.url);
|
||||
@@ -571,10 +674,39 @@ impl MeowClient {
|
||||
if failures == jobs.len() {
|
||||
return Err(last_err.unwrap_or_else(|| Error::Network("all servers failed".into())));
|
||||
}
|
||||
self.process(stored).await
|
||||
Ok(round)
|
||||
}
|
||||
|
||||
/// Every mailbox currently used by a contact, with its pull servers.
|
||||
fn contact_mailboxes(&self) -> Vec<(SigningKeyPair, Vec<ServerInfo>)> {
|
||||
self.profile
|
||||
.contacts
|
||||
.iter()
|
||||
.flat_map(|c| c.mailboxes())
|
||||
.map(|(kp, servers)| (kp.clone(), servers.to_vec()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Keep the keys of mailboxes that disappeared (`before` → now) while they still
|
||||
/// have pending acks, so the next pull can acknowledge their last messages.
|
||||
fn retire_mailboxes(&mut self, before: Vec<(SigningKeyPair, Vec<ServerInfo>)>) {
|
||||
let current: Vec<SignPublic> = self.contact_mailboxes().iter().map(|(kp, _)| kp.public()).collect();
|
||||
for (key, servers) in before {
|
||||
let pk = key.public();
|
||||
let has_acks = self
|
||||
.profile
|
||||
.pending_acks
|
||||
.iter()
|
||||
.any(|a| a.mailbox.as_slice() == pk.as_bytes().as_slice());
|
||||
let known = self.profile.retired_mailboxes.iter().any(|r| r.key.public() == pk);
|
||||
if has_acks && !known && !current.contains(&pk) {
|
||||
self.profile.retired_mailboxes.push(RetiredMailbox { key, servers });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn process(&mut self, stored: Vec<v2::StoredMessage>) -> Result<Vec<Event>> {
|
||||
let mailboxes_before = self.contact_mailboxes();
|
||||
// Handshake messages first (step 2 before step 3), then ratchet messages by arrival.
|
||||
let mut envs: Vec<(u32, u64, Vec<u8>, v2::PeerEnvelope)> = stored
|
||||
.into_iter()
|
||||
@@ -686,6 +818,7 @@ impl MeowClient {
|
||||
_ => tracing::debug!("ignoring unsupported envelope"),
|
||||
}
|
||||
}
|
||||
self.retire_mailboxes(mailboxes_before);
|
||||
// Persist before anything leaves the device.
|
||||
self.save().await?;
|
||||
for (servers, deposit) in outgoing {
|
||||
|
||||
Reference in new issue
Block a user