Sign in

    gaato/discord/cooldown does not have a README file

    CooldownStore

    pub(open) trait CooldownStore {
    async fn try_acquire(Self, key : String, window_ms~ : Int64) -> CooldownDecision
    }

    Storage for command cooldown windows. Implementations own their clock.

    CooldownDecision

    pub(all) enum CooldownDecision {
    Acquired
    Active(retry_after_ms~ : Int64)
    Unavailable(reason~ : String)
    } derive(Eq,
    Debug
    )

    The result of attempting to start a cooldown window.

    InMemoryCooldownStore

    pub struct InMemoryCooldownStore {
    // private fields
    }

    In-process fixed windows. Expired keys are swept each time the map has doubled since the last sweep, so memory follows the live windows however long or short they are, and at least once a minute, so a burst's expired keys do not stay once traffic returns to a few keys.

    InMemoryCooldownStore::InMemoryCooldownStore

    fn InMemoryCooldownStore::InMemoryCooldownStore(now? : () -> Int64) -> InMemoryCooldownStore

    Create an empty store, optionally supplying a clock for deterministic tests.

    async test {
    let store = @cooldown.InMemoryCooldownStore(now=() => 1_000L)
    assert_eq(store.try_acquire("1:ping|global", window_ms=10_000L), Acquired)
    assert_eq(
    store.try_acquire("1:ping|global", window_ms=10_000L),
    Active(retry_after_ms=10_000L),
    )
    }

    InMemoryCooldownStore::try_acquire

    async fn InMemoryCooldownStore::try_acquire(self : InMemoryCooldownStore, key : String, window_ms~ : Int64) -> CooldownDecision

    Source Files