From 621eabfce3ad1d6f059515602bbd435673207f57 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Fri, 28 Mar 2025 22:55:36 -0300 Subject: [PATCH] refactor(ratelimit): Return Duration from Ratelimit functions - `Ratelimit` is rather a low-level Rust tool, so it should only use `Duration` and `SystemTime`. - Rename `Ratelimit::update_interval()` to `min_send_interval()`, the word "update" only occurs in `last_update`, but it's a private field. --- deltachat-jsonrpc/src/api/types/webxdc.rs | 4 ++-- deltachat-ratelimit/src/lib.rs | 8 ++++---- src/webxdc.rs | 14 ++++++++++++-- src/webxdc/webxdc_tests.rs | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/deltachat-jsonrpc/src/api/types/webxdc.rs b/deltachat-jsonrpc/src/api/types/webxdc.rs index 28d27a47a..2e0e9eb1e 100644 --- a/deltachat-jsonrpc/src/api/types/webxdc.rs +++ b/deltachat-jsonrpc/src/api/types/webxdc.rs @@ -60,7 +60,7 @@ impl WebxdcMessageInfo { request_integration: _, internet_access, self_addr, - send_update_interval, + send_update_interval_ms, send_update_max_size, } = message.get_webxdc_info(context).await?; @@ -72,7 +72,7 @@ impl WebxdcMessageInfo { source_code_url: maybe_empty_string_to_option(source_code_url), internet_access, self_addr, - send_update_interval, + send_update_interval: send_update_interval_ms, send_update_max_size, }) } diff --git a/deltachat-ratelimit/src/lib.rs b/deltachat-ratelimit/src/lib.rs index 497cc83bb..c98306867 100644 --- a/deltachat-ratelimit/src/lib.rs +++ b/deltachat-ratelimit/src/lib.rs @@ -91,9 +91,9 @@ impl Ratelimit { self.until_can_send_at(SystemTime::now()) } - /// Returns minimum possible update interval in milliseconds. - pub fn update_interval(&self) -> usize { - (self.window.as_millis() as f64 / self.quota) as usize + /// Returns the minimum possible sending interval. + pub fn min_send_interval(&self) -> Duration { + self.window.div_f64(self.quota) } } @@ -107,7 +107,7 @@ mod tests { let mut ratelimit = Ratelimit::new_at(Duration::new(60, 0), 3.0, now); assert!(ratelimit.can_send_at(now)); - assert_eq!(ratelimit.update_interval(), 20_000); + assert_eq!(ratelimit.min_send_interval(), Duration::new(20, 0)); // Send burst of 3 messages. ratelimit.send_at(now); diff --git a/src/webxdc.rs b/src/webxdc.rs index a4ea27116..9159714ca 100644 --- a/src/webxdc.rs +++ b/src/webxdc.rs @@ -21,6 +21,7 @@ mod maps_integration; use std::cmp::max; use std::collections::HashMap; use std::path::Path; +use std::time::Duration; use anyhow::{anyhow, bail, ensure, format_err, Context as _, Result}; @@ -113,7 +114,7 @@ pub struct WebxdcInfo { /// Milliseconds to wait before calling `sendUpdate()` again since the last call. /// Should be exposed to `window.sendUpdateInterval` in JS land. - pub send_update_interval: usize, + pub send_update_interval_ms: usize, /// Maximum number of bytes accepted for a serialized update object. /// Should be exposed to `window.sendUpdateMaxSize` in JS land. @@ -955,7 +956,16 @@ impl Message { request_integration, internet_access, self_addr, - send_update_interval: context.ratelimit.read().await.update_interval(), + send_update_interval_ms: context + .ratelimit + .read() + .await + .min_send_interval() + // Round the value up so that it's not 0 at least. + .checked_add(Duration::from_micros(999)) + .context("Overflow occurred")? + .as_millis() + .try_into()?, send_update_max_size: RECOMMENDED_FILE_SIZE as usize, }) } diff --git a/src/webxdc/webxdc_tests.rs b/src/webxdc/webxdc_tests.rs index 1d0e92492..a0a4d2fc1 100644 --- a/src/webxdc/webxdc_tests.rs +++ b/src/webxdc/webxdc_tests.rs @@ -1262,7 +1262,7 @@ async fn test_get_webxdc_info() -> Result<()> { let info = instance.get_webxdc_info(&t).await?; assert_eq!(info.name, "minimal.xdc"); assert_eq!(info.icon, WEBXDC_DEFAULT_ICON.to_string()); - assert_eq!(info.send_update_interval, 10000); + assert_eq!(info.send_update_interval_ms, 10000); assert_eq!(info.send_update_max_size, RECOMMENDED_FILE_SIZE as usize); let mut instance = create_webxdc_instance(