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

Commit

Permalink
Call retain_recent periodically on rate limiters
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Jul 26, 2024
1 parent b12bec2 commit 110c695
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/cli/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ impl Options {

let limiter = Limiter::default();

limiter.start();

let graphql_schema = mas_handlers::graphql_schema(
&pool,
&policy_factory,
Expand Down
24 changes: 23 additions & 1 deletion crates/handlers/src/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{net::IpAddr, sync::Arc};
use std::{net::IpAddr, sync::Arc, time::Duration};

use governor::{clock::QuantaClock, state::keyed::DashMapStateStore, Quota, RateLimiter};
use mas_data_model::User;
Expand Down Expand Up @@ -83,6 +83,28 @@ impl Default for LimiterInner {
}

impl Limiter {
/// Start the rate limiter housekeeping task
///
/// This task will periodically remove old entries from the rate limiters,
/// to make sure we don't build up a huge number of entries in memory.
pub fn start(&self) {
// Spawn a task that will periodically clean the rate limiters
let this = self.clone();
tokio::spawn(async move {
// Run the task every minute
let mut interval = tokio::time::interval(Duration::from_secs(60));
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

loop {
// Call the retain_recent method on each rate limiter
this.inner.password_check_for_requester.retain_recent();
this.inner.password_check_for_user.retain_recent();

interval.tick().await;
}
});
}

/// Check if a password check can be performed
///
/// # Errors
Expand Down

0 comments on commit 110c695

Please sign in to comment.