pub fn consume(&mut self, tokens: u64, token_type: TokenType) -> bool {
        // If the timer is active, we can't consume tokens from any bucket and the function fails.
        if self.timer_active {
            return false;
        }

        // Identify the required token bucket.
        let token_bucket = match token_type {
            TokenType::Bytes => self.bandwidth.as_mut(),
            TokenType::Ops => self.ops.as_mut(),
        };
        // Try to consume from the token bucket.
        if let Some(bucket) = token_bucket {
            let refill_time = bucket.refill_time_ms();
            match bucket.reduce(tokens) {
                // When we report budget is over, there will be no further calls here,
                // register a timer to replenish the bucket and resume processing;
                // make sure there is only one running timer for this limiter.
                BucketReduction::Failure => {
                    if !self.timer_active {
                        self.activate_timer(TIMER_REFILL_STATE);
                    }
                    false
                }
                // The operation succeeded and further calls can be made.
                BucketReduction::Success => true,
                // The operation succeeded as the tokens have been consumed
                // but the timer still needs to be armed.
                BucketReduction::OverConsumption(ratio) => {
                    // The operation "borrowed" a number of tokens `ratio` times
                    // greater than the size of the bucket, and since it takes
                    // `refill_time` milliseconds to fill an empty bucket, in
                    // order to enforce the bandwidth limit we need to prevent
                    // further calls to the rate limiter for
                    // `ratio * refill_time` milliseconds.
                    self.activate_timer(TimerState::Oneshot(Duration::from_millis(
                        (ratio * refill_time as f64) as u64,
                    )));
                    true
                }
            }
        } else {
            // If bucket is not present rate limiting is disabled on token type,
            // consume() will always succeed.
            true
        }
    }
/// Attempts to consume tokens and returns whether that is possible.
///
/// If rate limiting is disabled on provided `token_type`, this function will always succeed.
Attempts to consume tokens and returns whether that is possible.
If rate limiting is disabled on provided `token_type`, this function will always succeed.
Attempts to consume tokens and returns whether that is possible . If rate limiting is disabled on provided ` token_type ` this function will always succeed .
pub fn consume(&mut self, tokens: u64, token_type: TokenType) -> bool {
        // If the timer is active, we can't consume tokens from any bucket and the function fails.
        if self.timer_active {
            return false;
        }
        // Identify the required token bucket.
        let token_bucket = match token_type {
            TokenType::Bytes => self.bandwidth.as_mut(),
            TokenType::Ops => self.ops.as_mut(),
        };
        // Try to consume from the token bucket.
        if let Some(bucket) = token_bucket {
            let refill_time = bucket.refill_time_ms();
            match bucket.reduce(tokens) {
                // When we report budget is over, there will be no further calls here,
                // register a timer to replenish the bucket and resume processing;
                // make sure there is only one running timer for this limiter.
                BucketReduction::Failure => {
                    if !self.timer_active {
                        self.activate_timer(TIMER_REFILL_STATE);
                    }
                    false
                }
                // The operation succeeded and further calls can be made.
                BucketReduction::Success => true,
                // The operation succeeded as the tokens have been consumed
                // but the timer still needs to be armed.
                BucketReduction::OverConsumption(ratio) => {
                    // The operation "borrowed" a number of tokens `ratio` times
                    // greater than the size of the bucket, and since it takes
                    // `refill_time` milliseconds to fill an empty bucket, in
                    // order to enforce the bandwidth limit we need to prevent
                    // further calls to the rate limiter for
                    // `ratio * refill_time` milliseconds.
                    self.activate_timer(TimerState::Oneshot(Duration::from_millis(
                        (ratio * refill_time as f64) as u64,
                    )));
                    true
                }
            }
        } else {
            // If bucket is not present rate limiting is disabled on token type,
            // consume() will always succeed.
            true
        }
    }
pub fn consume ( & mut self , tokens : u64 , token_type : TokenType ) -> bool { // If the timer is active, we can't consume tokens from any bucket and the function fails. if self . timer_active { return false ; } // Identify the required token bucket. let token_bucket = match token_type { TokenType :: Bytes => self . bandwidth . as_mut ( ) , TokenType :: Ops => self . ops . as_mut ( ) , } ; // Try to consume from the token bucket. if let Some ( bucket ) = token_bucket { let refill_time = bucket . refill_time_ms ( ) ; match bucket . reduce ( tokens ) { // When we report budget is over, there will be no further calls here, // register a timer to replenish the bucket and resume processing; // make sure there is only one running timer for this limiter. BucketReduction :: Failure => { if ! self . timer_active { self . activate_timer ( TIMER_REFILL_STATE ) ; } false } // The operation succeeded and further calls can be made. BucketReduction :: Success => true , // The operation succeeded as the tokens have been consumed // but the timer still needs to be armed. BucketReduction :: OverConsumption ( ratio ) => { // The operation "borrowed" a number of tokens `ratio` times // greater than the size of the bucket, and since it takes // `refill_time` milliseconds to fill an empty bucket, in // order to enforce the bandwidth limit we need to prevent // further calls to the rate limiter for // `ratio * refill_time` milliseconds. self . activate_timer ( TimerState :: Oneshot ( Duration :: from_millis ( ( ratio * refill_time as f64 ) as u64 , ) ) ) ; true } } } else { // If bucket is not present rate limiting is disabled on token type, // consume() will always succeed. true } }
Attempts to consume tokens and returns whether that is possible.
Attempts to consume tokens and returns whether that is possible .
&mut self
tokens
u64
token_type
TokenType
&mut self
tokens
u64
token_type
TokenType