bg bugfix
This commit is contained in:
1 parent
7eace57528
commit
c30fddb1ef
14 files changed
+3809
-12
No files matched your search
@@ -93,10 +93,12 @@ impl ClientHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for `d`, or until asked to stop. Returns `true` when stopping.
|
||||
async fn pause(d: Duration, stop: &mut watch::Receiver<bool>) -> bool {
|
||||
/// Wait for `d`, until the client state changes, or until asked to stop. Returns
|
||||
/// `true` when stopping.
|
||||
async fn pause(d: Duration, stop: &mut watch::Receiver<bool>, changes: &mut watch::Receiver<u64>) -> bool {
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(d) => *stop.borrow(),
|
||||
_ = changes.changed() => *stop.borrow(),
|
||||
_ = stop.changed() => true,
|
||||
}
|
||||
}
|
||||
@@ -104,6 +106,7 @@ async fn pause(d: Duration, stop: &mut watch::Receiver<bool>) -> bool {
|
||||
async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop: watch::Receiver<bool>) {
|
||||
let mut wait = opts.long_poll;
|
||||
let mut backoff = opts.backoff;
|
||||
let mut changes = client.lock().await.changes.subscribe();
|
||||
while !*stop.borrow() {
|
||||
// 1. Due deposits (and expiries).
|
||||
let (plan, transport) = {
|
||||
@@ -130,6 +133,8 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
|
||||
// for the next queued deposit to be retried.
|
||||
let plan = {
|
||||
let mut c = client.lock().await;
|
||||
// Everything up to here (including our own saves) is reflected in the plan.
|
||||
changes.borrow_and_update();
|
||||
let w = match c.next_retry_in() {
|
||||
Some(d) => wait.min(d.max(Duration::from_secs(1))),
|
||||
None => wait,
|
||||
@@ -139,13 +144,19 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
|
||||
if plan.is_empty() {
|
||||
// No contact yet: just keep the queue moving.
|
||||
let d = client.lock().await.next_retry_in().unwrap_or(opts.long_poll);
|
||||
if pause(d.clamp(Duration::from_secs(1), opts.long_poll), &mut stop).await {
|
||||
if pause(d.clamp(Duration::from_secs(1), opts.long_poll), &mut stop, &mut changes).await {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let results = tokio::select! {
|
||||
r = execute_pull(&transport, &plan) => r,
|
||||
// The state changed under us (new contact, unlocked space, message to send):
|
||||
// restart with an up-to-date request. Abandoned acks are simply resent.
|
||||
_ = changes.changed() => {
|
||||
wait = Duration::ZERO;
|
||||
continue;
|
||||
}
|
||||
_ = stop.changed() => break,
|
||||
};
|
||||
|
||||
@@ -172,7 +183,7 @@ async fn run(client: Arc<Mutex<MeowClient>>, opts: BackgroundOptions, mut stop:
|
||||
Err(e) => {
|
||||
drop(c);
|
||||
tracing::warn!("background pull failed: {e}; retrying in {backoff:?}");
|
||||
if pause(backoff, &mut stop).await {
|
||||
if pause(backoff, &mut stop, &mut changes).await {
|
||||
break;
|
||||
}
|
||||
backoff = (backoff * 2).min(opts.max_backoff);
|
||||
|
||||
@@ -28,7 +28,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use futures_util::future::join_all;
|
||||
use meow_core::contact::{Contact, ContactState};
|
||||
use meow_core::invitation::{self, OfferInfo};
|
||||
use meow_core::server::{canonical_url, ServerInfo};
|
||||
use meow_core::server::canonical_url;
|
||||
use meow_core::{messaging, rpc};
|
||||
use meow_crypto::password::KdfParams;
|
||||
use meow_crypto::{SignPublic, SigningKeyPair};
|
||||
@@ -38,6 +38,7 @@ use meow_store::{Batch, RetiredMailbox, SpaceId, State as SpaceState, Store};
|
||||
|
||||
pub use background::{BackgroundOptions, ClientHandle};
|
||||
pub use meow_core::contact::ContactState as State;
|
||||
pub use meow_core::server::ServerInfo;
|
||||
pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck, Settings, Status};
|
||||
pub use outbox::{PendingSend, RetryPolicy};
|
||||
|
||||
@@ -216,6 +217,9 @@ pub struct MeowClient {
|
||||
pending_events: Vec<Event>,
|
||||
/// Every event, for subscribers ([`MeowClient::subscribe`]).
|
||||
events: tokio::sync::broadcast::Sender<Event>,
|
||||
/// Bumped at every save: the background task restarts its wait when the state
|
||||
/// changed under it (new contact to poll, unlocked space, queued message).
|
||||
changes: tokio::sync::watch::Sender<u64>,
|
||||
}
|
||||
|
||||
type Mailboxes = Vec<(SigningKeyPair, Vec<ServerInfo>)>;
|
||||
@@ -252,6 +256,7 @@ impl MeowClient {
|
||||
retry: RetryPolicy::default(),
|
||||
pending_events: Vec::new(),
|
||||
events: tokio::sync::broadcast::channel(EVENT_BUFFER).0,
|
||||
changes: tokio::sync::watch::channel(0).0,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -270,6 +275,7 @@ impl MeowClient {
|
||||
let batch = std::mem::take(&mut sp.batch);
|
||||
lock(&self.store)?.save(sp.id, &sp.state, batch)?;
|
||||
sp.dirty = false;
|
||||
self.changes.send_modify(|v| *v = v.wrapping_add(1));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -337,6 +343,7 @@ impl MeowClient {
|
||||
batch: Batch::default(),
|
||||
dirty: false,
|
||||
});
|
||||
self.changes.send_modify(|v| *v = v.wrapping_add(1));
|
||||
Ok(self.spaces.len() - 1)
|
||||
}
|
||||
|
||||
@@ -752,13 +759,23 @@ impl MeowClient {
|
||||
|
||||
pub async fn send_file(&mut self, contact_name: &str, path: &Path) -> Result<SendReport> {
|
||||
let data = tokio::fs::read(path).await?;
|
||||
if data.len() > MAX_FILE_BYTES {
|
||||
return Err(Error::Invalid(format!("file larger than {MAX_FILE_BYTES} bytes")));
|
||||
}
|
||||
let name = path
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
.ok_or_else(|| Error::Invalid("bad file name".into()))?;
|
||||
self.send_file_data(contact_name, &name, data).await
|
||||
}
|
||||
|
||||
/// Send a file given by its content (mobile platforms often have no path).
|
||||
pub async fn send_file_data(&mut self, contact_name: &str, name: &str, data: Vec<u8>) -> Result<SendReport> {
|
||||
if data.len() > MAX_FILE_BYTES {
|
||||
return Err(Error::Invalid(format!("file larger than {MAX_FILE_BYTES} bytes")));
|
||||
}
|
||||
let name = Path::new(name)
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
.filter(|n| !n.is_empty())
|
||||
.ok_or_else(|| Error::Invalid("bad file name".into()))?;
|
||||
let size = data.len() as u64;
|
||||
let body = v2::user_message::Body::File(v2::FileBody {
|
||||
filename: name.clone(),
|
||||
|
||||
@@ -771,3 +771,48 @@ async fn invitations_straight_into_hidden_spaces() {
|
||||
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["still secret"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The background task must notice state changes made through the handle (here, a
|
||||
/// first contact while it was idling), not only after its 30 s wait.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn background_task_wakes_up_on_new_contacts() {
|
||||
let servers = vec![spawn_server().await];
|
||||
let dir = tmpdir();
|
||||
let alice = client(&dir, "alice", &servers)
|
||||
.await
|
||||
.start(meow_client::BackgroundOptions::default());
|
||||
let bob = client(&dir, "bob", &servers)
|
||||
.await
|
||||
.start(meow_client::BackgroundOptions::default());
|
||||
let alice_rx = alice.subscribe();
|
||||
let bob_rx = bob.subscribe();
|
||||
tokio::time::sleep(Duration::from_millis(300)).await; // both idle, nothing to poll
|
||||
|
||||
let url = alice
|
||||
.lock()
|
||||
.await
|
||||
.publish_invitation("bob", &[], "", "", 3600, None)
|
||||
.await
|
||||
.unwrap();
|
||||
bob.lock()
|
||||
.await
|
||||
.accept_published_invitation(&url, "", None, &[], None)
|
||||
.await
|
||||
.unwrap();
|
||||
async fn activated(mut rx: tokio::sync::broadcast::Receiver<Event>, who: &str) {
|
||||
tokio::time::timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
if let Event::ContactActivated { .. } = rx.recv().await.unwrap() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|_| panic!("{who} not activated in time"))
|
||||
}
|
||||
let (a, b) = (activated(alice_rx, "alice"), activated(bob_rx, "bob"));
|
||||
tokio::join!(a, b);
|
||||
alice.stop().await;
|
||||
bob.stop().await;
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user