Skip to content

Commit 946eea4

Browse files
Simon-Lauxr10s
authored andcommitted
add rate limit for quota check in background fetch (12h for now)
1 parent 5cbc873 commit 946eea4

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};
@@ -466,9 +466,19 @@ impl Context {
466466
.await?;
467467
}
468468

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

474484
info!(

0 commit comments

Comments
 (0)