LogoAntiRaid

@antiraid-ext/gcra

API reference for @antiraid-ext/gcra

@antiraid-ext/gcra

Types

RateLimiter

Provides a simple ratelimiter using the GCRA algorithm

Raw Type
--- Provides a simple ratelimiter using the GCRA algorithm
type RateLimiter<BucketKey> = {
	--- Maximum number of actions allowed
	getCount: () -> number,

	--- Time period for the rate limit in seconds
	getPeriod: () -> number,

	--- Time interval between allowed actions in seconds
	--- This is calculated as period / count
	getEmissionInterval: () -> number,

	--- Buckets for different keys, where each key has a time after which the next action is allowed
	getBuckets: () -> {
		[BucketKey]: number
	},

	--- Attempt to perform an action
	--- @param key The key for which the action is being attempted
	--- @return boolean \`\`true\`\` if the action is allowed, \`\`false\`\` if it is denied (rate limited)
	attemptAction: (key: BucketKey) -> boolean
}
PropTypeDescription
getCount
() -> number
Maximum number of actions allowed
getPeriod
() -> number
Time period for the rate limit in seconds
getEmissionInterval
() -> number
Time interval between allowed actions in secondsThis is calculated as period / count
getBuckets
() -> {[BucketKey]: number}
Buckets for different keys, where each key has a time after which the next action is allowed
attemptAction
(key: BucketKey) -> boolean
Attempt to perform an action

Functions

RateLimiter

Return a new rate limiter instance

Function Signature
--- Return a new rate limiter instance
--- @param count Maximum number of actions allowed in the period
--- @param period Time period for the rate limit in seconds
--- @return RateLimiter<BucketKey> A new rate limiter instance
function RateLimiter<BucketKey>(count: number, period: number) -> RateLimiter<BucketKey> end

Generics

BucketKey

This generic is unconstrained and can be any type

Arguments

count

Maximum number of actions allowed in the period

number

period

Time period for the rate limit in seconds

number

Returns

RateLimiter<BucketKey>

A new rate limiter instance

RateLimiter<BucketKey>

Last updated on