Move rate limiter into its own crate

This commit is contained in:
link2xt
2023-01-23 11:33:24 +00:00
parent 6067622c19
commit 5dfe7bea8e
6 changed files with 21 additions and 7 deletions

5
Cargo.lock generated
View File

@@ -918,6 +918,7 @@ dependencies = [
"r2d2", "r2d2",
"r2d2_sqlite", "r2d2_sqlite",
"rand 0.8.5", "rand 0.8.5",
"ratelimit",
"regex", "regex",
"reqwest", "reqwest",
"rusqlite", "rusqlite",
@@ -2878,6 +2879,10 @@ dependencies = [
"rand_core 0.6.3", "rand_core 0.6.3",
] ]
[[package]]
name = "ratelimit"
version = "1.0.0"
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.5.3" version = "1.5.3"

View File

@@ -20,6 +20,7 @@ panic = 'abort'
[dependencies] [dependencies]
deltachat_derive = { path = "./deltachat_derive" } deltachat_derive = { path = "./deltachat_derive" }
format-flowed = { path = "./format-flowed" } format-flowed = { path = "./format-flowed" }
ratelimit = { path = "./deltachat-ratelimit" }
ansi_term = { version = "0.12.1", optional = true } ansi_term = { version = "0.12.1", optional = true }
anyhow = "1" anyhow = "1"
@@ -101,6 +102,7 @@ members = [
"deltachat_derive", "deltachat_derive",
"deltachat-jsonrpc", "deltachat-jsonrpc",
"deltachat-rpc-server", "deltachat-rpc-server",
"deltachat-ratelimit",
"format-flowed", "format-flowed",
] ]

View File

@@ -0,0 +1,8 @@
[package]
name = "ratelimit"
version = "1.0.0"
description = "Token bucket implementation"
edition = "2021"
license = "MPL-2.0"
[dependencies]

View File

@@ -7,7 +7,7 @@
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Ratelimit { pub struct Ratelimit {
/// Time of the last update. /// Time of the last update.
last_update: SystemTime, last_update: SystemTime,
@@ -25,7 +25,7 @@ impl Ratelimit {
/// Returns a new rate limiter with the given constraints. /// Returns a new rate limiter with the given constraints.
/// ///
/// Rate limiter will allow to send no more than `quota` messages within duration `window`. /// 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()) Self::new_at(window, quota, SystemTime::now())
} }
@@ -57,7 +57,7 @@ impl Ratelimit {
/// Returns true if can send another message now. /// Returns true if can send another message now.
/// ///
/// This method takes mutable reference /// This method takes mutable reference
pub(crate) fn can_send(&self) -> bool { pub fn can_send(&self) -> bool {
self.can_send_at(SystemTime::now()) 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 /// 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 /// 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. /// 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()) self.send_at(SystemTime::now())
} }
@@ -87,7 +87,7 @@ impl Ratelimit {
} }
/// Calculates the time until `can_send` will return `true`. /// 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()) self.until_can_send_at(SystemTime::now())
} }
} }

View File

@@ -11,6 +11,7 @@ use std::time::{Duration, Instant, SystemTime};
use anyhow::{ensure, Result}; use anyhow::{ensure, Result};
use async_channel::{self as channel, Receiver, Sender}; use async_channel::{self as channel, Receiver, Sender};
use ratelimit::Ratelimit;
use tokio::sync::{Mutex, RwLock}; use tokio::sync::{Mutex, RwLock};
use crate::chat::{get_chat_cnt, ChatId}; use crate::chat::{get_chat_cnt, ChatId};
@@ -22,7 +23,6 @@ use crate::key::{DcKey, SignedPublicKey};
use crate::login_param::LoginParam; use crate::login_param::LoginParam;
use crate::message::{self, MessageState, MsgId}; use crate::message::{self, MessageState, MsgId};
use crate::quota::QuotaInfo; use crate::quota::QuotaInfo;
use crate::ratelimit::Ratelimit;
use crate::scheduler::Scheduler; use crate::scheduler::Scheduler;
use crate::sql::Sql; use crate::sql::Sql;
use crate::stock_str::StockStrings; use crate::stock_str::StockStrings;

View File

@@ -103,7 +103,6 @@ mod color;
pub mod html; pub mod html;
mod net; mod net;
pub mod plaintext; pub mod plaintext;
mod ratelimit;
pub mod summary; pub mod summary;
pub mod receive_imf; pub mod receive_imf;