Home Reference Source Test Repository
public class | source

TokenBucket

TokenBucket

Handles how many tokens are available at any given time based on the configuration set and the token bucket algorithm.

See:

Constructor Summary

Public Constructor
public

constructor(config: object)

Creates a TokenBucket object.

Method Summary

Public Methods
public

decrementTokens(amount: integer): integer

Decrements the amount of tokens available based on amount and returns the value.

public

Cleans up the interval set by the constructor.

public

getTokensRemaining(): integer

Returns the currently available tokens.

Public Constructors

public constructor(config: object) source

Creates a TokenBucket object.

Params:

NameTypeAttributeDescription
config object

configuration values

config.limit integer
  • optional
  • default: 25

The max amount of tokens per incrementInterval.

config.incrementInterval integer
  • optional
  • default: 5000

The time in ms before tokens will increment.

config.increment integer
  • optional
  • default: 1

The amount of tokens that will increment each incrementInterval.

Public Methods

public decrementTokens(amount: integer): integer source

Decrements the amount of tokens available based on amount and returns the value.

Params:

NameTypeAttributeDescription
amount integer
  • optional
  • default: 1

The number to decrement by

Return:

integer

The remaining available requests for the identifier

public destroy() source

Cleans up the interval set by the constructor.

public getTokensRemaining(): integer source

Returns the currently available tokens.

Return:

integer

The remaining available requests for the identifier