async_client: Add a timeout to the sync forever even if the sync was successful.

master
Damir Jelić 2020-02-28 10:33:17 +01:00
parent 8181d96bfb
commit 0288376cfe
1 changed files with 18 additions and 2 deletions

View File

@ -17,7 +17,7 @@ use futures::future::{BoxFuture, Future, FutureExt};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use std::time::Duration; use std::time::{Duration, Instant};
use async_std::task::sleep; use async_std::task::sleep;
@ -44,6 +44,8 @@ use crate::VERSION;
type RoomEventCallback = type RoomEventCallback =
Box<dyn FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> BoxFuture<'static, ()> + Send>; Box<dyn FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> BoxFuture<'static, ()> + Send>;
const DEFAULT_SYNC_TIMEOUT: u64 = 30000;
#[derive(Clone)] #[derive(Clone)]
/// An async/await enabled Matrix client. /// An async/await enabled Matrix client.
pub struct AsyncClient { pub struct AsyncClient {
@ -442,6 +444,7 @@ impl AsyncClient {
C: Future<Output = ()>, C: Future<Output = ()>,
{ {
let mut sync_settings = sync_settings; let mut sync_settings = sync_settings;
let mut last_sync_time: Option<Instant> = None;
loop { loop {
let response = self.sync(sync_settings.clone()).await; let response = self.sync(sync_settings.clone()).await;
@ -459,8 +462,21 @@ impl AsyncClient {
callback(response).await; callback(response).await;
let now = Instant::now();
// If the last sync happened less than a second ago, sleep for a
// while to not hammer out requests if the server doesn't respect
// the sync timeout.
if let Some(t) = last_sync_time {
if now - t <= Duration::from_secs(1) {
sleep(Duration::from_secs(1)).await;
}
}
last_sync_time = Some(now);
sync_settings = SyncSettings::new() sync_settings = SyncSettings::new()
.timeout(30000) .timeout(DEFAULT_SYNC_TIMEOUT)
.unwrap() .unwrap()
.token(self.sync_token().unwrap()); .token(self.sync_token().unwrap());
} }