pub fn reduce(&mut self, mut tokens: u64) -> BucketReduction {
        // First things first: consume the one-time-burst budget.
        if self.one_time_burst > 0 {
            // We still have burst budget for *all* tokens requests.
            if self.one_time_burst >= tokens {
                self.one_time_burst -= tokens;
                self.last_update = Instant::now();
                // No need to continue to the refill process, we still have burst budget to consume from.
                return BucketReduction::Success;
            } else {
                // We still have burst budget for *some* of the tokens requests.
                // The tokens left unfulfilled will be consumed from current `self.budget`.
                tokens -= self.one_time_burst;
                self.one_time_burst = 0;
            }
        }

        if tokens > self.budget {
            // Hit the bucket bottom, let's auto-replenish and try again.
            self.auto_replenish();

            // This operation requests a bandwidth higher than the bucket size
            if tokens > self.size {
                error!(
                    "Consumed {} tokens from bucket of size {}",
                    tokens, self.size
                );
                // Empty the bucket and report an overconsumption of
                // (remaining tokens / size) times larger than the bucket size
                tokens -= self.budget;
                self.budget = 0;
                return BucketReduction::OverConsumption(tokens as f64 / self.size as f64);
            }

            if tokens > self.budget {
                // Still not enough tokens, consume() fails, return false.
                return BucketReduction::Failure;
            }
        }

        self.budget -= tokens;
        BucketReduction::Success
    }
/// Attempts to consume `tokens` from the bucket and returns whether the action succeeded.
Attempts to consume `tokens` from the bucket and returns whether the action succeeded.
Attempts to consume ` tokens ` from the bucket and returns whether the action succeeded .
pub fn reduce(&mut self, mut tokens: u64) -> BucketReduction {
        // First things first: consume the one-time-burst budget.
        if self.one_time_burst > 0 {
            // We still have burst budget for *all* tokens requests.
            if self.one_time_burst >= tokens {
                self.one_time_burst -= tokens;
                self.last_update = Instant::now();
                // No need to continue to the refill process, we still have burst budget to consume from.
                return BucketReduction::Success;
            } else {
                // We still have burst budget for *some* of the tokens requests.
                // The tokens left unfulfilled will be consumed from current `self.budget`.
                tokens -= self.one_time_burst;
                self.one_time_burst = 0;
            }
        }
        if tokens > self.budget {
            // Hit the bucket bottom, let's auto-replenish and try again.
            self.auto_replenish();
            // This operation requests a bandwidth higher than the bucket size
            if tokens > self.size {
                error!(
                    "Consumed {} tokens from bucket of size {}",
                    tokens, self.size
                );
                // Empty the bucket and report an overconsumption of
                // (remaining tokens / size) times larger than the bucket size
                tokens -= self.budget;
                self.budget = 0;
                return BucketReduction::OverConsumption(tokens as f64 / self.size as f64);
            }
            if tokens > self.budget {
                // Still not enough tokens, consume() fails, return false.
                return BucketReduction::Failure;
            }
        }
        self.budget -= tokens;
        BucketReduction::Success
    }
pub fn reduce ( & mut self , mut tokens : u64 ) -> BucketReduction { // First things first: consume the one-time-burst budget. if self . one_time_burst > 0 { // We still have burst budget for *all* tokens requests. if self . one_time_burst >= tokens { self . one_time_burst -= tokens ; self . last_update = Instant :: now ( ) ; // No need to continue to the refill process, we still have burst budget to consume from. return BucketReduction :: Success ; } else { // We still have burst budget for *some* of the tokens requests. // The tokens left unfulfilled will be consumed from current `self.budget`. tokens -= self . one_time_burst ; self . one_time_burst = 0 ; } } if tokens > self . budget { // Hit the bucket bottom, let's auto-replenish and try again. self . auto_replenish ( ) ; // This operation requests a bandwidth higher than the bucket size if tokens > self . size { error ! ( " " tokens self size ) ; // Empty the bucket and report an overconsumption of // (remaining tokens / size) times larger than the bucket size tokens -= self . budget ; self . budget = 0 ; return BucketReduction :: OverConsumption ( tokens as f64 / self . size as f64 ) ; } if tokens > self . budget { // Still not enough tokens, consume() fails, return false. return BucketReduction :: Failure ; } } self . budget -= tokens ; BucketReduction :: Success }
Attempts to consume `tokens` from the bucket and returns whether the action succeeded.
Attempts to consume ` tokens ` from the bucket and returns whether the action succeeded .
&mut self
tokens
u64
&mut self
tokens
u64