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

call-quota-enforcer

v1.1.1

Published

enforce quota for calling external services

Downloads

17

Readme

CallQuotaEnforcer

Enforce quota for calling external services

What is the problem, what is supposed to be resolved?

In certain situations, you may find it necessary to have control over the way you call external services. To address this, it becomes important to manage the frequency of your calls to these external servers. This package aims to provide a seamless solution to help you effectively manage and control your calls to external services without any hassle. ;)

Installation

NPM Stats

This is a module for node.js and is installed via npm:

npm install call-quota-enforcer --save

Decorators

To utilize this service, you will need to import two decorators that will allow you to annotate your classes and methods accordingly.

import { ScheduledClass, ScheduledMethod } from 'call-quota-enforcer';

To implement a quota for your class, you will need to annotate your class with the ScheduledClass decorator. Additionally, any methods that utilize the defined quota must be annotated using the ScheduledMethod decorator.

@ScheduledClass({
    interval: {
        duration: 5,
        maxWeight: 300,
        size: 'SECOND',
        cache: 'MEMORY',
    }
})
export class TestService {


@ScheduledMethod({ name: 'f1', weight: 1 })
async function1(input: number) {
    // do somthing
        this.function2()
  }

@ScheduledMethod({ name: 'f2', weight: 250 })
async function2() {
    // do somthing

  }

}

Options

ScheduledClass

{
    // you can adjust maximum usage a weight in your classes that can be consumed

    interval: {
        duration: 5,        // 5 second
        size: 'SECOND',     // default: SECOND
        maxWeight: 300,     // The maximum weight that can be consumed within a 5-second interval will be reset after each 5-second period.
        cache: 'MEMORY',    // cache can be MEMORY or REDIS { host:x.x.x.x, port: 6379 }
    }
}

ScheduledMethod

{
    name: 'f2',      // for logging purpose
    weight: 250      // Whenever this function is invoked, the specified amount will be subtracted from the maximum weight.

}

Benefits

By leveraging the power of two decorators, you can exercise precise control over the manner in which you call your external services. These decorators provide you with the flexibility to customize and fine-tune your approach, ensuring that your interactions with external services align with your specific requirements and objectives.

Use Case:

In certain cases, exceeding a certain threshold when making calls to external services can result in your service being banned. In such situations, it becomes essential to have control over the frequency of your calls to these external services. These decorators offer a solution to effectively manage and regulate the number of times you can call external services, helping you avoid any potential issues or restrictions.

Todo:

  • [x] Adding Redis support for multi-service instances: Enhance the functionality of your multi-service instances by incorporating Redis support. This addition will enable efficient management and communication between different service instances.

  • [ ] Obtaining a sharable name from the class decorator: Utilize a class decorator to obtain a sharable name that can be used across different classes. This approach promotes consistency and ease of use, allowing for seamless integration and collaboration between classes.

  • [ ] Adding a logger for debugging purposes: For effective debugging and troubleshooting, consider adding a logger to your codebase. This logging mechanism will assist in identifying and resolving issues, providing valuable insights for error analysis and optimization.

Known Issues:

Keywords: rate-limter, call governance, quota enforcement