mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
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.
This commit is contained in:
@@ -66,7 +66,7 @@ impl WebxdcMessageInfo {
|
|||||||
self_addr,
|
self_addr,
|
||||||
is_app_sender,
|
is_app_sender,
|
||||||
is_broadcast,
|
is_broadcast,
|
||||||
send_update_interval,
|
send_update_interval_ms,
|
||||||
send_update_max_size,
|
send_update_max_size,
|
||||||
} = message.get_webxdc_info(context).await?;
|
} = message.get_webxdc_info(context).await?;
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ impl WebxdcMessageInfo {
|
|||||||
self_addr,
|
self_addr,
|
||||||
is_app_sender,
|
is_app_sender,
|
||||||
is_broadcast,
|
is_broadcast,
|
||||||
send_update_interval,
|
send_update_interval: send_update_interval_ms,
|
||||||
send_update_max_size,
|
send_update_max_size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,9 +91,9 @@ impl Ratelimit {
|
|||||||
self.until_can_send_at(SystemTime::now())
|
self.until_can_send_at(SystemTime::now())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns minimum possible update interval in milliseconds.
|
/// Returns the minimum possible sending interval.
|
||||||
pub fn update_interval(&self) -> usize {
|
pub fn min_send_interval(&self) -> Duration {
|
||||||
(self.window.as_millis() as f64 / self.quota) as usize
|
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);
|
let mut ratelimit = Ratelimit::new_at(Duration::new(60, 0), 3.0, now);
|
||||||
assert!(ratelimit.can_send_at(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.
|
// Send burst of 3 messages.
|
||||||
ratelimit.send_at(now);
|
ratelimit.send_at(now);
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ mod maps_integration;
|
|||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as _, Result, anyhow, bail, ensure, format_err};
|
use anyhow::{Context as _, Result, anyhow, bail, ensure, format_err};
|
||||||
|
|
||||||
@@ -119,7 +120,7 @@ pub struct WebxdcInfo {
|
|||||||
|
|
||||||
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
|
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
|
||||||
/// Should be exposed to `window.sendUpdateInterval` in JS land.
|
/// 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.
|
/// Maximum number of bytes accepted for a serialized update object.
|
||||||
/// Should be exposed to `window.sendUpdateMaxSize` in JS land.
|
/// Should be exposed to `window.sendUpdateMaxSize` in JS land.
|
||||||
@@ -974,7 +975,16 @@ impl Message {
|
|||||||
self_addr,
|
self_addr,
|
||||||
is_app_sender,
|
is_app_sender,
|
||||||
is_broadcast,
|
is_broadcast,
|
||||||
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_nanos(999_999))
|
||||||
|
.context("Overflow occurred")?
|
||||||
|
.as_millis()
|
||||||
|
.try_into()?,
|
||||||
send_update_max_size: RECOMMENDED_FILE_SIZE as usize,
|
send_update_max_size: RECOMMENDED_FILE_SIZE as usize,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1226,7 +1226,7 @@ async fn test_get_webxdc_info() -> Result<()> {
|
|||||||
let info = instance.get_webxdc_info(&t).await?;
|
let info = instance.get_webxdc_info(&t).await?;
|
||||||
assert_eq!(info.name, "minimal.xdc");
|
assert_eq!(info.name, "minimal.xdc");
|
||||||
assert_eq!(info.icon, WEBXDC_DEFAULT_ICON.to_string());
|
assert_eq!(info.icon, WEBXDC_DEFAULT_ICON.to_string());
|
||||||
assert_eq!(info.send_update_interval, 1000);
|
assert_eq!(info.send_update_interval_ms, 1000);
|
||||||
assert_eq!(info.send_update_max_size, RECOMMENDED_FILE_SIZE as usize);
|
assert_eq!(info.send_update_max_size, RECOMMENDED_FILE_SIZE as usize);
|
||||||
|
|
||||||
let mut instance = create_webxdc_instance(
|
let mut instance = create_webxdc_instance(
|
||||||
|
|||||||
Reference in New Issue
Block a user