Skip to content

Commit 7690711

Browse files
committed
add rate limit for quota check in background fetch (12h for now)
1 parent e7fefc2 commit 7690711

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ pub(crate) const DC_FOLDERS_CONFIGURED_VERSION: i32 = 4;
214214
// `max_smtp_rcpt_to` in the provider db.
215215
pub(crate) const DEFAULT_MAX_SMTP_RCPT_TO: usize = 50;
216216

217+
/// How far the last quota check needs to be in the past to be checked by the background function (in seconds).
218+
pub(crate) const DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT: i64 = 12 * 60 * 60; // 12 hours
219+
217220
#[cfg(test)]
218221
mod tests {
219222
use num_traits::FromPrimitive;

src/context.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tokio::sync::{Mutex, Notify, RwLock};
1515

1616
use crate::chat::{get_chat_cnt, ChatId};
1717
use crate::config::Config;
18-
use crate::constants::DC_VERSION_STR;
18+
use crate::constants::{DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT, DC_VERSION_STR};
1919
use crate::contact::Contact;
2020
use crate::debug_logging::DebugLogging;
2121
use crate::events::{Event, EventEmitter, EventType, Events};
@@ -462,9 +462,19 @@ impl Context {
462462
.await?;
463463
}
464464

465-
// update quota (to send warning if full)
466-
if let Err(err) = self.update_recent_quota(&mut connection).await {
467-
warn!(self, "Failed to update quota: {err:#}.");
465+
// update quota (to send warning if full) - but only check it once in a while
466+
let quota_needs_update = {
467+
let quota = self.quota.read().await;
468+
quota
469+
.as_ref()
470+
.filter(|quota| quota.modified + DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT > time())
471+
.is_none()
472+
};
473+
474+
if quota_needs_update {
475+
if let Err(err) = self.update_recent_quota(&mut connection).await {
476+
warn!(self, "Failed to update quota: {err:#}.");
477+
}
468478
}
469479

470480
info!(

0 commit comments

Comments
 (0)