/// Creates a TokenBucket of `size` total capacity that takes `complete_refill_time_ms`
/// milliseconds to go from zero tokens to total capacity. The `one_time_burst` is initial
/// extra credit on top of total capacity, that does not replenish and which can be used
/// for an initial burst of data.
Creates a TokenBucket of `size` total capacity that takes `complete_refill_time_ms`
milliseconds to go from zero tokens to total capacity. The `one_time_burst` is initial
extra credit on top of total capacity, that does not replenish and which can be used
for an initial burst of data.
Creates a TokenBucket of ` size ` total capacity that takes ` complete_refill_time_ms ` milliseconds to go from zero tokens to total capacity . The ` one_time_burst ` is initial extra credit on top of total capacity that does not replenish and which can be used for an initial burst of data .
pub fn new(size: u64, one_time_burst: Option<u64>, complete_refill_time_ms: u64) -> Self {
        // Formula for computing current refill amount:
        // refill_token_count = (delta_time * size) / (complete_refill_time_ms * 1_000_000)
        // In order to avoid overflows, simplify the fractions by computing greatest common divisor.
        // Get the greatest common factor between `size` and `complete_refill_time_ms`.
        let common_factor = gcd(size, complete_refill_time_ms);
        // The division will be exact since `common_factor` is a factor of `size`.
        let mut processed_capacity: u64 = size / common_factor;
        // The division will be exact since `common_factor` is a factor of `complete_refill_time_ms`.
        let mut processed_refill_time: u64 = complete_refill_time_ms / common_factor;
        // Get the gcd between `processed_capacity` and `NANOSEC_IN_ONE_MILLISEC`
        // which is 1_000_000 (see formula from above).
        let common_factor = gcd(processed_capacity, NANOSEC_IN_ONE_MILLISEC);
        // Reduce the capacity factor even further.
        processed_capacity /= common_factor;
        // `processed_refill_time` was ms; turn to nanoseconds and reduce by `common_factor`.
        processed_refill_time *= NANOSEC_IN_ONE_MILLISEC / common_factor;
        TokenBucket {
            size,
            one_time_burst,
            refill_time: complete_refill_time_ms,
            // Start off full.
            budget: size,
            // Last updated is now.
            last_update: Instant::now(),
            processed_capacity,
            processed_refill_time,
        }
    }
pub fn new ( size : u64 , one_time_burst : Option < u64 > , complete_refill_time_ms : u64 ) -> Self { // Formula for computing current refill amount: // refill_token_count = (delta_time * size) / (complete_refill_time_ms * 1_000_000) // In order to avoid overflows, simplify the fractions by computing greatest common divisor. // Get the greatest common factor between `size` and `complete_refill_time_ms`. let common_factor = gcd ( size , complete_refill_time_ms ) ; // The division will be exact since `common_factor` is a factor of `size`. let mut processed_capacity : u64 = size / common_factor ; // The division will be exact since `common_factor` is a factor of `complete_refill_time_ms`. let mut processed_refill_time : u64 = complete_refill_time_ms / common_factor ; // Get the gcd between `processed_capacity` and `NANOSEC_IN_ONE_MILLISEC` // which is 1_000_000 (see formula from above). let common_factor = gcd ( processed_capacity , NANOSEC_IN_ONE_MILLISEC ) ; // Reduce the capacity factor even further. processed_capacity /= common_factor ; // `processed_refill_time` was ms; turn to nanoseconds and reduce by `common_factor`. processed_refill_time *= NANOSEC_IN_ONE_MILLISEC / common_factor ; TokenBucket { size , one_time_burst , refill_time : complete_refill_time_ms , // Start off full. budget : size , // Last updated is now. last_update : Instant :: now ( ) , processed_capacity , processed_refill_time , } }
Creates a TokenBucket of `size` total capacity that takes `complete_refill_time_ms`
milliseconds to go from zero tokens to total capacity.
Creates a TokenBucket of ` size ` total capacity that takes ` complete_refill_time_ms ` milliseconds to go from zero tokens to total capacity .
size
u64
one_time_burst
Option<u64>
complete_refill_time_ms
u64