From aa5999cc2579753f427dbd7691f7a4b698993fda Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 26 Jan 2026 14:27:29 +0000 Subject: [PATCH] refactor: remove Mutex around rate limiter Rate limiter handles all the necessary locking internally. --- filtermail/src/outbound.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/filtermail/src/outbound.rs b/filtermail/src/outbound.rs index 9535f379..907e9caf 100644 --- a/filtermail/src/outbound.rs +++ b/filtermail/src/outbound.rs @@ -10,12 +10,12 @@ use async_trait::async_trait; use governor::{DefaultKeyedRateLimiter, Quota, RateLimiter}; use lettre::{AsyncSmtpTransport, AsyncTransport, Tokio1Executor}; use mailparse::{MailHeaderMap, parse_mail}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; /// Handler for outgoing SMTP messages. pub struct OutgoingBeforeQueueHandler { config: Arc, - send_rate_limiter: Arc>>, + send_rate_limiter: DefaultKeyedRateLimiter, } impl OutgoingBeforeQueueHandler { @@ -23,7 +23,7 @@ impl OutgoingBeforeQueueHandler { let quota = Quota::per_minute(config.max_user_send_per_minute); Self { config: Arc::new(config), - send_rate_limiter: Arc::new(Mutex::new(RateLimiter::keyed(quota))), + send_rate_limiter: RateLimiter::keyed(quota), } } } @@ -38,11 +38,7 @@ impl SmtpHandler for OutgoingBeforeQueueHandler { return Err(format!("500 Invalid from address <{}>", address)); } - let Ok(limiter) = self.send_rate_limiter.lock() else { - log::error!("send_rate_limiter lock panicked!"); - return Err("451 Temporary server error".to_string()); - }; - if let Err(e) = limiter.check_key(&address.to_string()) { + if let Err(e) = self.send_rate_limiter.check_key(&address.to_string()) { // " rate limited until: ..." log::debug!("<{address}> {e}"); return Err(format!("450 4.7.1: Too much mail from <{address}>, {e}")); @@ -55,7 +51,7 @@ impl SmtpHandler for OutgoingBeforeQueueHandler { // In the future, in case of higher traffic this can be further optimized by e.g. calling it // every N messages or in a separate task every N minutes. // Time complexity is O(n) where n is the number of unique senders in the last minute. - limiter.retain_recent(); + self.send_rate_limiter.retain_recent(); Ok(()) }