From 94330d252c57c16a60799c827214e486bb2c3878 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 4 Jan 2024 06:53:46 +0100 Subject: [PATCH] add rate limit for quota check in background fetch (12h for now) --- src/constants.rs | 3 +++ src/context.rs | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 165de5391e..b94c76d86b 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -209,6 +209,9 @@ pub const WORSE_IMAGE_SIZE: u32 = 640; // this value can be increased if the folder configuration is changed and must be redone on next program start pub(crate) const DC_FOLDERS_CONFIGURED_VERSION: i32 = 4; +/// How far the last quota check needs to be in the past to be checked by the background function (in seconds). +pub(crate) const DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT: i64 = 12 * 60 * 60; // 12 hours + #[cfg(test)] mod tests { use num_traits::FromPrimitive; diff --git a/src/context.rs b/src/context.rs index e5dbec5d70..a01da019ef 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,7 +15,7 @@ use tokio::sync::{Mutex, Notify, RwLock}; use crate::chat::{get_chat_cnt, ChatId}; use crate::config::Config; -use crate::constants::DC_VERSION_STR; +use crate::constants::{DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT, DC_VERSION_STR}; use crate::contact::Contact; use crate::debug_logging::DebugLogging; use crate::events::{Event, EventEmitter, EventType, Events}; @@ -462,9 +462,19 @@ impl Context { .await?; } - // update quota (to send warning if full) - if let Err(err) = self.update_recent_quota(&mut connection).await { - warn!(self, "Failed to update quota: {err:#}."); + // update quota (to send warning if full) - but only check it once in a while + let quota_needs_update = { + let quota = self.quota.read().await; + quota + .as_ref() + .filter(|quota| quota.modified + DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT > time()) + .is_none() + }; + + if quota_needs_update { + if let Err(err) = self.update_recent_quota(&mut connection).await { + warn!(self, "Failed to update quota: {err:#}."); + } } info!(