npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@appolo/rate-limiter

v8.0.2

Published

appolo rate-limiter module

Downloads

488

Readme

Appolo RateLimit Module

RateLimit module for appolo built with Redis

Installation

npm i @appolo/rate-limiter

Options

| key | Description | Type | Default | --- | --- | --- | --- | | id | RateLimiter injection id | string| rateLimiter| | connection| redis connection string| string| | keyPrefix| redis key prefix| string| rl | maxBuckets| max number of buckets| number| 600 | minBucketInterval| min bucket interval in milisec| number| 5000

in config/modules/all.ts

import {RateLimiterModule} from '@appolo/rate-limiter';

export = async function (app: App) {
    await app.module(new RateLimiterModule({connection:process.env.REDIS}));
    
}

Usage

import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";

@define()
@singleton()
export class SomeManager {

    @inject() rateLimiter: RateLimiter;

    async checkLimits(): Promise<boolean> {
        let result = await this.rateLimiter.reserve({
            key:"someKey",
            roles:[{
                interval:10 * 60 * 1000, //10 min
                limit:100,
            }]
        })

        return result.isValid;
    }
}

RateLimiter

Reserve

reserve key and return results object of the key rate limit usage

Options

  • key - key string
  • roles - array of roles
    • interval - number of miliseconds in a sliding window
    • limit - max number of items in a sliding window
    • spread - true to spread the limit evenly across interval buckets - default false
    • reserve - the amount items to reserve default 1
    • bucket - bucket interval in milisec - if not defined will be set automatically
    • start - if we in fixed windowx - start point of the interval default Date.now()
  • type - RateLimitType - sliding window or fixed window - default sliding window

Results

  • isValid - true if all the limits are valid
  • results - array of limit results
    • count - current limit count
    • bucket - bucket interval in milisec
    • remaining - remaining limit of the key
    • rateLimit - if spread defined the rate limit per bucket
    • rate - if spread defined - current rate
    • reset - the number of milisec until the limit will reset to its maximum capacity
    • retry - the number of milisec until bucket reset
import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";

@define()
@singleton()
export class SomeManager {

    @inject() rateLimiter: RateLimiter;

    async checkLimits(): Promise<boolean> {
        let result = await this.rateLimiter.reserve({
            key:"someKey",
            limits:[{
                interval:10 * 60 * 1000, //10 min
                limit:100,
                spread:true,
                reserve:100
            }]
        })

        return result.isValid;
    }
}

Check

Checks current usage of the key the counters won't be updated

Options

same as Reserve options

Results

same as Reserve results

import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";

@define()
@singleton()
export class SomeManager {

    @inject() rateLimiter: RateLimiter;

    async checkLimits(): Promise<boolean> {
        let result = await this.rateLimiter.check({
            key:"someKey",
            limits:[{
                interval:10 * 60 * 1000, //10 min
                limit:100,
                spread:true,
                reserve:100
            }]
        })

        return result.isValid;
    }
}

Cancel

cancel rate limit and clean all for given key

await this.rateLimiter.clean("someKey")