From 946eea4c9eb706fe287e0d7c49302775a68000b0 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 ac3c281ed..61bc4909c 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -214,6 +214,9 @@ pub(crate) const DC_FOLDERS_CONFIGURED_VERSION: i32 = 4; // `max_smtp_rcpt_to` in the provider db. pub(crate) const DEFAULT_MAX_SMTP_RCPT_TO: usize = 50; +/// 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 9ed75e231..38bf11e0d 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}; @@ -466,9 +466,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!(