Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 4a275fa

Browse files
committed
Call retain_recent periodically on rate limiters
1 parent e25c170 commit 4a275fa

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

crates/cli/src/commands/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ impl Options {
202202

203203
let limiter = Limiter::default();
204204

205+
limiter.start();
206+
205207
let graphql_schema = mas_handlers::graphql_schema(
206208
&pool,
207209
&policy_factory,

crates/handlers/src/rate_limit.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{net::IpAddr, sync::Arc};
15+
use std::{net::IpAddr, sync::Arc, time::Duration};
1616

1717
use governor::{clock::QuantaClock, state::keyed::DashMapStateStore, Quota, RateLimiter};
1818
use mas_data_model::User;
@@ -83,6 +83,28 @@ impl Default for LimiterInner {
8383
}
8484

8585
impl Limiter {
86+
/// Start the rate limiter housekeeping task
87+
///
88+
/// This task will periodically remove old entries from the rate limiters,
89+
/// to make sure we don't build up a huge number of entries in memory.
90+
pub fn start(&self) {
91+
// Spawn a task that will periodically clean the rate limiters
92+
let this = self.clone();
93+
tokio::spawn(async move {
94+
// Run the task every minute
95+
let mut interval = tokio::time::interval(Duration::from_secs(60));
96+
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
97+
98+
loop {
99+
// Call the retain_recent method on each rate limiter
100+
this.inner.password_check_for_requester.retain_recent();
101+
this.inner.password_check_for_user.retain_recent();
102+
103+
interval.tick().await;
104+
}
105+
});
106+
}
107+
86108
/// Check if a password check can be performed
87109
///
88110
/// # Errors

0 commit comments

Comments
 (0)