Home Reference Source Test Repository
public class | source

RateLimiter

RateLimiter Class

Handles creation and destruction of TokenBuckets by key.

Constructor Summary

Public Constructor
public

constructor(config: object)

Creates a RateLimiter object.

Method Summary

Public Methods
public

decrementTokens(key: string, amount: integer): integer

Appropriately decrements the remaining token availability when called and returns the remainder.

public

Cleans up all TokenBuckets and nulls the array.

public

getTokensRemaining(key: string): integer

Returns the remaining request count for the given key.

Public Constructors

public constructor(config: object) source

Creates a RateLimiter object.

Params:

NameTypeAttributeDescription
config object

configuration values

config.limit integer

The max amount of tokens per incrementInterval.

config.incrementInterval integer

The time in ms before tokens will increment.

config.increment integer

The amount of tokens that will increment each incrementInterval.

Public Methods

public decrementTokens(key: string, amount: integer): integer source

Appropriately decrements the remaining token availability when called and returns the remainder.

Params:

NameTypeAttributeDescription
key string

The identifier for the request

amount integer
  • optional

The number to decrement by

Return:

integer

Remaining request count

public destroy() source

Cleans up all TokenBuckets and nulls the array.

public getTokensRemaining(key: string): integer source

Returns the remaining request count for the given key. If the key doesn't already exist, it will create it.

Params:

NameTypeAttributeDescription
key string

The identifier for the request

Return:

integer

Remaining request count