Function calculate_token_refill

Source
pub fn calculate_token_refill(
    elapsed: Duration,
    window_duration: Duration,
    max_tokens: usize,
    current_tokens: usize,
) -> usize
Expand description

Calculate the number of tokens to refill based on elapsed time

This function implements the standard proportional token refill algorithm used across all rate limiter implementations. It calculates how many tokens should be available based on the time elapsed since the last refresh.

§Arguments

  • elapsed - Time elapsed since last refresh
  • window_duration - Duration of the rate limiting window
  • max_tokens - Maximum number of tokens in the bucket
  • current_tokens - Current number of available tokens

§Returns

The new number of available tokens (capped at max_tokens)