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

schedule-limiter

v0.1.2

Published

A generic Schedule rate limiter for NodeJS. Useful for API clients and sms, email, notification daemons which tasks that need to be throttled.

Downloads

6

Readme

node-schedule-limiter

A generic Schedule rate limiter for NodeJS. Useful for API clients and sms, email, notification daemons which tasks that need to be throttled.

Installation

Use NPM to install using;

npm install schedule-limiter --save

Getting Started

let ScheduleLimiter = require('schedule-limiter');
let limiter = new ScheduleLimiter({
    database: {
        type: 'Redis',
        options: {
            host: 'localhost',
            port: 6379
        }
    }
});
let id = 1; // e.g. userId
limiter.setLimit(id,100); // Set Limit as 100
limiter.createSchedule(id, {'2015': {'Dec': 50}, '2016': {'Jan': 60}})
  .then(function(usage) {
      callMyRequestSendingFunction(...);
  }).catch(function(error) {
      console.log(error.message);
      // Handle error and notify user
  });
limiter.getUsage(id, {'2015': ['Dec'], '2016': ['Jan', 'Feb'] })
  .then(function(usage) {
      console.log(usage);
      // {'2015': {'12': 50}, '2016': {'1': 60, '2': 0}}
  });
limiter.cancelSchedule(id, {'2015': {'Dec': 50}, '2016': {'Jan': 60}})
  .then(function(usage) {
      callMyFunction(...);
  }).catch(function(error) {
      console.log(error.message);
      // Handle error and notify user
  });

API

Following methods are available in Schedule Limiter;

  1. constructor
  2. setLimit(id, limit)
  3. getLimit(id)
  4. setLimits(limits)
  5. createSchedule(id, tokens [,force])
  6. cancelSchedule(id, tokens [,force])
  7. getUsage(id, months))

constructor

Create new instance of Schedule Limiter

let ScheduleLimiter = require('schedule-limiter');
let limiter = new ScheduleLimiter({
   database: {
       type: 'Redis',
       options: {
           host: 'localhost',
           port: 6379
       }
   }
});

setLimit(id, limit)

Set the limit against particular id. Then this limit will consider as limit for every month

Parameters
  • id : {Integer/String} ID (appId or userId) which want to limit
  • limit : {Integer} Limit value

getLimit(id)

Get the stored limit against particular ID

Parameters
  • id : {Integer/String} ID (appId or userId) which want to limit
Return

{Integer} Limit value

setLimits(limits)

Set the limit against particular id. Then this limit will consider as limit for every month

Parameters
  • limit : {Array} Array of limit Objects s.t. [{'userId': 1000}, {'userId2: 500}, ...]

createSchedule(id, tokens [,force])

Increase the usage value of given set of months against particular ID. If successfully increase the values, return values after increment. Otherwise rollback to previous state and return an error.

Parameters
  • id : {Integer/String} ID (appId or userId) which want to limit
  • tokens : {Object} Number of tokens which want to use s.t. {'2015': {'Jan': 10, 'feb': 15, 3: 20, '4': 30}, '2016': {'1': 40}}
Return

{Object} Remaining limits for each month s.t. {'2015': {'1': 10, '2': 15, '3': 20, '4': 30}, '2016': {'1': 40}}

cancelSchedule(id, tokens [,force])

Increase the usage value of given set of months against particular ID. If successfully increase the values, return values after increment. Otherwise rollback to previous state and return an error.

Parameters
  • id : {Integer/String} ID (appId or userId) which want to limit
  • tokens : {Object} Number of tokens which want to use s.t. {'2015': {'Jan': 10, 'feb': 15, 3: 20, '4': 30}, '2016': {'1': 40}}
Return

{Object} Remaining limits for each month s.t. {'2015': {'1': 10, '2': 15, '3': 20, '4': 30}, '2016': {'1': 40}}

getUsage(id, months)

Get the current usage (used tokens) against a particular id.

Parameters
  • id : {Integer/String} ID (appId or userId) which want to limit
  • months : {Object} Object which contains Months Array s.t. {'2015': ['Jan', 'feb', 3, '4']}
Return

{Object} Remaining limits for each month s.t. {'2015': {'1': 10, '2': 15, '3': 20, '4': 30}}

License

This Software is licensed under MIT License

Copyright (c) 2015 Gihan Karunarathne [email protected]