brandur/redis-cell

Clarification on count & period

Closed this issue · 2 comments

Is there a difference in behaviour between the following 2 invocations?

CL.THROTTLE user123 15 1 2 1
CL.THROTTLE user123 15 30 60 1

They're effectively identical. If you look at the implementation of Rate, it only stores a single number which is the time it takes to refill a single token:

pub struct Rate {
    pub period: time::Duration,
}

That period is calculated from a rate (x actions / y time) with this formula:

    /// Produces a rate for some number of actions per second. For example, if
    /// we wanted to have 10 actions every 2 seconds, the period produced would
    /// be 200 ms.
    pub fn per_period(n: i64, period: time::Duration) -> Rate {
        let ns: i64 = period.num_nanoseconds().unwrap();
        let period = time::Duration::nanoseconds(((ns as f64) / (n as f64)) as i64);
        Rate { period }
    }

Thanks, @brandur.