diff --git a/Cargo.lock b/Cargo.lock index 489a1a754..2cdd08bdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -918,6 +918,7 @@ dependencies = [ "r2d2", "r2d2_sqlite", "rand 0.8.5", + "ratelimit", "regex", "reqwest", "rusqlite", @@ -2878,6 +2879,10 @@ dependencies = [ "rand_core 0.6.3", ] +[[package]] +name = "ratelimit" +version = "1.0.0" + [[package]] name = "rayon" version = "1.5.3" diff --git a/Cargo.toml b/Cargo.toml index 31e01a91e..c4f61ab30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ panic = 'abort' [dependencies] deltachat_derive = { path = "./deltachat_derive" } format-flowed = { path = "./format-flowed" } +ratelimit = { path = "./deltachat-ratelimit" } ansi_term = { version = "0.12.1", optional = true } anyhow = "1" @@ -101,6 +102,7 @@ members = [ "deltachat_derive", "deltachat-jsonrpc", "deltachat-rpc-server", + "deltachat-ratelimit", "format-flowed", ] diff --git a/deltachat-ratelimit/Cargo.toml b/deltachat-ratelimit/Cargo.toml new file mode 100644 index 000000000..04f2a9107 --- /dev/null +++ b/deltachat-ratelimit/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ratelimit" +version = "1.0.0" +description = "Token bucket implementation" +edition = "2021" +license = "MPL-2.0" + +[dependencies] diff --git a/src/ratelimit.rs b/deltachat-ratelimit/src/lib.rs similarity index 94% rename from src/ratelimit.rs rename to deltachat-ratelimit/src/lib.rs index 64edc1338..351746adc 100644 --- a/src/ratelimit.rs +++ b/deltachat-ratelimit/src/lib.rs @@ -7,7 +7,7 @@ use std::time::{Duration, SystemTime}; #[derive(Debug)] -pub(crate) struct Ratelimit { +pub struct Ratelimit { /// Time of the last update. last_update: SystemTime, @@ -25,7 +25,7 @@ impl Ratelimit { /// Returns a new rate limiter with the given constraints. /// /// Rate limiter will allow to send no more than `quota` messages within duration `window`. - pub(crate) fn new(window: Duration, quota: f64) -> Self { + pub fn new(window: Duration, quota: f64) -> Self { Self::new_at(window, quota, SystemTime::now()) } @@ -57,7 +57,7 @@ impl Ratelimit { /// Returns true if can send another message now. /// /// This method takes mutable reference - pub(crate) fn can_send(&self) -> bool { + pub fn can_send(&self) -> bool { self.can_send_at(SystemTime::now()) } @@ -71,7 +71,7 @@ impl Ratelimit { /// It is possible to send message even if over quota, e.g. if the message sending is initiated /// by the user and should not be rate limited. However, sending messages when over quota /// further postpones the time when it will be allowed to send low priority messages. - pub(crate) fn send(&mut self) { + pub fn send(&mut self) { self.send_at(SystemTime::now()) } @@ -87,7 +87,7 @@ impl Ratelimit { } /// Calculates the time until `can_send` will return `true`. - pub(crate) fn until_can_send(&self) -> Duration { + pub fn until_can_send(&self) -> Duration { self.until_can_send_at(SystemTime::now()) } } diff --git a/src/context.rs b/src/context.rs index 329667ddf..9e0db4fc5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -11,6 +11,7 @@ use std::time::{Duration, Instant, SystemTime}; use anyhow::{ensure, Result}; use async_channel::{self as channel, Receiver, Sender}; +use ratelimit::Ratelimit; use tokio::sync::{Mutex, RwLock}; use crate::chat::{get_chat_cnt, ChatId}; @@ -22,7 +23,6 @@ use crate::key::{DcKey, SignedPublicKey}; use crate::login_param::LoginParam; use crate::message::{self, MessageState, MsgId}; use crate::quota::QuotaInfo; -use crate::ratelimit::Ratelimit; use crate::scheduler::Scheduler; use crate::sql::Sql; use crate::stock_str::StockStrings; diff --git a/src/lib.rs b/src/lib.rs index 831f6609e..c941db562 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,7 +103,6 @@ mod color; pub mod html; mod net; pub mod plaintext; -mod ratelimit; pub mod summary; pub mod receive_imf;