Compare commits

...

1 Commits

Author SHA1 Message Date
iequidoo
621eabfce3 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.
2025-03-29 00:32:32 -03:00
4 changed files with 19 additions and 9 deletions

View File

@@ -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,
})
}

View File

@@ -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);

View File

@@ -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,
})
}

View File

@@ -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(