Modules@robinpath/ratelimit
ratelimit

@robinpath/ratelimit

0.1.2Node.jsPublic

Token bucket and sliding window rate limiting for RobinPath

Ratelimit

Rate limiting with token bucket, sliding window, and fixed window algorithms

Package: @robinpath/ratelimit | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the ratelimit module when you need to:

  • Create a named rate limiter (token-bucket, sliding-window, or fixed-window) -- Use ratelimit.create to perform this operation
  • Try to acquire tokens/slots from a rate limiter -- Use ratelimit.acquire to perform this operation
  • Check if a request would be allowed without consuming a token -- Use ratelimit.check to perform this operation
  • Get the number of remaining tokens/slots -- Use ratelimit.remaining to perform this operation
  • Wait until a token is available, then acquire it -- Use ratelimit.wait to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate a named rate limiter (token-bucket, sliding-window, or fixed-window)The limiter configuration
acquireTry to acquire tokens/slots from a rate limiter{allowed: boolean, remaining: number, retryAfterMs?: number}
checkCheck if a request would be allowed without consuming a token{allowed: boolean, remaining: number}
remainingGet the number of remaining tokens/slotsNumber of remaining tokens or request slots
waitWait until a token is available, then acquire it{allowed: true, remaining: number} when token is acquired
resetReset a rate limiter to its initial stateTrue if reset successfully
statusGet detailed status information for a rate limiterObject with type-specific status details
destroyRemove a rate limiter and free its resourcesTrue if the limiter existed and was destroyed

Functions

create

Create a named rate limiter (token-bucket, sliding-window, or fixed-window)

Module: ratelimit | Returns: object -- The limiter configuration

ratelimit.create "api" "token-bucket" {"maxTokens": 100, "refillRate": 10}
ParameterTypeRequiredDescription
namestringYesLimiter name
typestringYesAlgorithm: token-bucket, sliding-window, or fixed-window
optionsobjectNoConfig: {maxTokens/max, refillRate/rate} or {maxRequests/max, windowMs/window}

acquire

Try to acquire tokens/slots from a rate limiter

Module: ratelimit | Returns: object -- {allowed: boolean, remaining: number, retryAfterMs?: number}

ratelimit.acquire "api"
ParameterTypeRequiredDescription
namestringYesLimiter name
countnumberNoNumber of tokens to acquire (default 1)

check

Check if a request would be allowed without consuming a token

Module: ratelimit | Returns: object -- {allowed: boolean, remaining: number}

ratelimit.check "api"
ParameterTypeRequiredDescription
namestringYesLimiter name

remaining

Get the number of remaining tokens/slots

Module: ratelimit | Returns: number -- Number of remaining tokens or request slots

ratelimit.remaining "api"
ParameterTypeRequiredDescription
namestringYesLimiter name

wait

Wait until a token is available, then acquire it

Module: ratelimit | Returns: object -- {allowed: true, remaining: number} when token is acquired

ratelimit.wait "api" 5000
ParameterTypeRequiredDescription
namestringYesLimiter name
maxWaitnumberNoMax wait time in ms (default 30000)

reset

Reset a rate limiter to its initial state

Module: ratelimit | Returns: boolean -- True if reset successfully

ratelimit.reset "api"
ParameterTypeRequiredDescription
namestringYesLimiter name

status

Get detailed status information for a rate limiter

Module: ratelimit | Returns: object -- Object with type-specific status details

ratelimit.status "api"
ParameterTypeRequiredDescription
namestringYesLimiter name

destroy

Remove a rate limiter and free its resources

Module: ratelimit | Returns: boolean -- True if the limiter existed and was destroyed

ratelimit.destroy "api"
ParameterTypeRequiredDescription
namestringYesLimiter name

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Unknown limiter typeCheck the error message for details
Unknown rate limiter type: .... Use "token-bucket", "sliding-window", or "fixed-window".Check the error message for details
Rate limiter "..." not found. Create it first.Check the error message for details
Rate limiter "..." not found.Check the error message for details
Rate limit wait timeout after ...ms for "..."Check the error message for details
@desc "Create and validate result"
do
  set $result as ratelimit.create "api" "token-bucket" {"maxTokens": 100, "refillRate": 10}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Create a new item with create

Create a new resource and capture the result.

set $result as ratelimit.create "api" "token-bucket" {"maxTokens": 100, "refillRate": 10}
print "Created: " + $result

2. Multi-step Ratelimit workflow

Chain multiple ratelimit operations together.

@desc "Create, acquire, and more"
do
  set $r_create as ratelimit.create "api" "token-bucket" {"maxTokens": 100, "refillRate": 10}
  set $r_acquire as ratelimit.acquire "api"
  set $r_check as ratelimit.check "api"
  print "All operations complete"
enddo

3. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as ratelimit.create "api" "token-bucket" {"maxTokens": 100, "refillRate": 10}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/ratelimit

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size5.2 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops