Skip to content
GitHub

MemoryCooldownStore

class in meocord/common Since 4.1.0

class MemoryCooldownStore extends CooldownStore

Counts cooldown calls in this process's memory: the store @Cooldown uses unless another is bound.

It suits a bot in one process; bind another with @MeoCord({ cooldownStore }). Its counts start again on a restart, and with process sharding each shard counts on its own; for those, use ShardedCooldownStore or RedisCooldownStore.

Examples

TypeScript
const store = new MemoryCooldownStore()
const limit = { uses: 1, windowMs: 3_000 }

await store.consume('daily:42', limit) // { allowed: true, retryAfterMs: 0 }
await store.consume('daily:42', limit) // { allowed: false, retryAfterMs: 3000 }, or just under

Members

constructor

new MemoryCooldownStore()

consume

consume(key: string, limit: CooldownLimit): Promise<CooldownVerdict>

Records a call for key if the limit allows it: at most limit.uses calls within the last limit.windowMs milliseconds.

Parameters

NameTypeDescription
keystring

Identifies the handler, the cooldown and the caller, user or place it counts per.

limitCooldownLimit

The calls allowed, and the window they are counted over.

Returns

Promise<CooldownVerdict>

Whether this call was recorded, and if not, how long until one can be.

consumeMany

consumeMany(entries: readonly CooldownEntry[]): Promise<CooldownBatchVerdict>

Checks every entry, and records the call against all of them only if all allow it.

Parameters

NameTypeDescription
entriesreadonly CooldownEntry[]

The keys and limits the call counts against, in the order the cooldowns are declared.

Returns

Promise<CooldownBatchVerdict>

Whether the call was recorded, and if not, how long until it can be and which entry refused it.

peekMany

peekMany(entries: readonly CooldownEntry[]): Promise<CooldownBatchVerdict>

Checks every entry as consumeMany does, recording nothing, and holding nothing for a key not yet counted.

Parameters

NameTypeDescription
entriesreadonly CooldownEntry[]

The keys and limits the call would count against.

Returns

Promise<CooldownBatchVerdict>

Whether the call would be allowed now, and if not, how long until it would be and which entry refuses it.

See also