pub fn reduce(&mut self, mut tokens: u64) -> bool {
        // First things first: consume the one-time-burst budget.
        if let Some(otb) = self.one_time_burst.as_mut() {
            if *otb > 0 {
                // We still have burst budget for *all* tokens requests.
                if *otb >= tokens {
                    *otb -= tokens;
                    self.last_update = Instant::now();
                    // No need to continue to the refill process, we still have burst budget to consume from.
                    return true;
                } else {
                    // We still have burst budget for *some* of the tokens requests.
                    // The tokens left unfulfilled will be consumed from current `self.budget`.
                    tokens -= *otb;
                    *otb = 0;
                }
            }
        }
        // Compute time passed since last refill/update.
        let time_delta = self.last_update.elapsed().as_nanos() as u64;
        self.last_update = Instant::now();

        // At each 'time_delta' nanoseconds the bucket should refill with:
        // refill_amount = (time_delta * size) / (complete_refill_time_ms * 1_000_000)
        // `processed_capacity` and `processed_refill_time` are the result of simplifying above
        // fraction formula with their greatest-common-factor.
        self.budget += (time_delta * self.processed_capacity) / self.processed_refill_time;

        if self.budget >= self.size {
            self.budget = self.size;
        }

        if tokens > self.budget {
            // TODO (Issue #259) remove this block when issue is resolved
            if tokens > self.size {
                error!(
                    "Trying to consume more tokens {} than the total capacity {}",
                    tokens, self.size
                );
                // best effort rate-limiting, this is a dirty workaround for Issue #259
                if self.budget == self.size {
                    self.budget = 0;
                    return true;
                }
            }
            // If not enough tokens consume() fails, return false.
            return false;
        }

        self.budget -= tokens;
        true
    }
/// Attempts to consume `tokens` from the bucket and returns whether the action succeeded.
// TODO (Issue #259): handle cases where a single request is larger than the full capacity
// for such cases we need to support partial fulfilment of requests
Attempts to consume `tokens` from the bucket and returns whether the action succeeded.
TODO (Issue #259): handle cases where a single request is larger than the full capacity
for such cases we need to support partial fulfilment of requests
Attempts to consume ` tokens ` from the bucket and returns whether the action succeeded . TODO ( Issue #259 ) : handle cases where a single request is larger than the full capacity for such cases we need to support partial fulfilment of requests
pub fn reduce(&mut self, mut tokens: u64) -> bool {
        // First things first: consume the one-time-burst budget.
        if let Some(otb) = self.one_time_burst.as_mut() {
            if *otb > 0 {
                // We still have burst budget for *all* tokens requests.
                if *otb >= tokens {
                    *otb -= tokens;
                    self.last_update = Instant::now();
                    // No need to continue to the refill process, we still have burst budget to consume from.
                    return true;
                } else {
                    // We still have burst budget for *some* of the tokens requests.
                    // The tokens left unfulfilled will be consumed from current `self.budget`.
                    tokens -= *otb;
                    *otb = 0;
                }
            }
        }
        // Compute time passed since last refill/update.
        let time_delta = self.last_update.elapsed().as_nanos() as u64;
        self.last_update = Instant::now();
        // At each 'time_delta' nanoseconds the bucket should refill with:
        // refill_amount = (time_delta * size) / (complete_refill_time_ms * 1_000_000)
        // `processed_capacity` and `processed_refill_time` are the result of simplifying above
        // fraction formula with their greatest-common-factor.
        self.budget += (time_delta * self.processed_capacity) / self.processed_refill_time;
        if self.budget >= self.size {
            self.budget = self.size;
        }
        if tokens > self.budget {
            // TODO (Issue #259) remove this block when issue is resolved
            if tokens > self.size {
                error!(
                    "Trying to consume more tokens {} than the total capacity {}",
                    tokens, self.size
                );
                // best effort rate-limiting, this is a dirty workaround for Issue #259
                if self.budget == self.size {
                    self.budget = 0;
                    return true;
                }
            }
            // If not enough tokens consume() fails, return false.
            return false;
        }
        self.budget -= tokens;
        true
    }
pub fn reduce ( & mut self , mut tokens : u64 ) -> bool { // First things first: consume the one-time-burst budget. if let Some ( otb ) = self . one_time_burst . as_mut ( ) { if * otb > 0 { // We still have burst budget for *all* tokens requests. if * otb >= tokens { * otb -= tokens ; self . last_update = Instant :: now ( ) ; // No need to continue to the refill process, we still have burst budget to consume from. return true ; } else { // We still have burst budget for *some* of the tokens requests. // The tokens left unfulfilled will be consumed from current `self.budget`. tokens -= * otb ; * otb = 0 ; } } } // Compute time passed since last refill/update. let time_delta = self . last_update . elapsed ( ) . as_nanos ( ) as u64 ; self . last_update = Instant :: now ( ) ; // At each 'time_delta' nanoseconds the bucket should refill with: // refill_amount = (time_delta * size) / (complete_refill_time_ms * 1_000_000) // `processed_capacity` and `processed_refill_time` are the result of simplifying above // fraction formula with their greatest-common-factor. self . budget += ( time_delta * self . processed_capacity ) / self . processed_refill_time ; if self . budget >= self . size { self . budget = self . size ; } if tokens > self . budget { // TODO (Issue #259) remove this block when issue is resolved if tokens > self . size { error ! ( " " tokens self size ) ; // best effort rate-limiting, this is a dirty workaround for Issue #259 if self . budget == self . size { self . budget = 0 ; return true ; } } // If not enough tokens consume() fails, return false. return false ; } self . budget -= tokens ; true }
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