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

limitd-client

v2.14.1

Published

limitd client for node.js

Downloads

1,158

Readme

Build Status

limitd is a simple daemon for rate limiting highly available applications.

This repository contains the client library for node.js.

Install

npm install limitd-client --save

Example usage

const LimitdClient = require('limitd-client');
const limitd = new LimitdClient('limitd://localhost:9001');

//express middleware
app.use(function (req, res, next) {
  limitd.take('user', req.username, function (err, resp) {
    if (err) return next(err);

    res.set({
      'X-RateLimit-Limit':     resp.limit,
      'X-RateLimit-Remaining': resp.remaining,
      'X-RateLimit-Reset':     resp.reset
    });

    if (resp.conformant) return next();

    // The 429 status code indicates that the user has sent too many
    // requests in a given amount of time ("rate limiting").
    res.send('429');
  });
})

API

limitd.take(type, key, count?, callback)

Take if available count amount of tokens from the bucket with key key of type type.

count defaults to 1.

The callback will be call either with an Error object or a Response object.

  • response.conformant: boolean indicating if the traffic is conformant or not.
  • response.limit: indicates the size of this bucket.
  • response.remaining: the amount of remaining tokens in the bucket.
  • response.reset: a unix timestamp indicating when the bucket is going to be full again.
  • response.delayed: always false for TAKE requests.

limitd.wait(type, key, count?, callback)

Take or Wait for count amount of tokens from the bucket with key key of type type.

count defaults to 1.

The callback will be call either with an Error object or a Response object.

  • response.delayed: boolean indicating if this request was delayed due to insufficient tokens in the bucket.
  • response.limit: indicates the size of this bucket.
  • response.remaining: the amount of remaining tokens in the bucket.
  • response.reset: a unix timestamp indicating when the bucket is going to be full again.
  • response.conformant: always true for WAIT requests.

limitd.put(type, key, count?, callback) (alias reset)

Force put count amount of tokens in the bucket with key key of type type.

count defaults to the size of the bucket.

The callback will be call either with an Error object or a Response object.

  • response.limit: indicates the size of this bucket.
  • response.remaining: the amount of remaining tokens in the bucket.
  • response.reset: a unix timestamp indicating when the bucket is going to be full again.

This is useful for buckets that are not automatically filled or when the application needs to force a reset.

Events

  • connect: emitted when the client connects to the server.
  • disconnect([err]): emitted when the client disconnects from the server.
  • error: emitted in case of errors
  • reconnect(n, delay): emitted when attempting to restablish the connection. See reconnect-core.
  • trip(err, failures, cooldown): emitted when the circuit-breaker mechanism trips.
  • ready: emitted when the client is ready to send requests.
  • response: emitted when the client receives a response. Useful for metrics and debug.

Note: Events on the shard client have the same signature but the last parameter is the instance of the client.

Sharding

Sharding is implemented in the client-side by providing a list of limitd servers.

Example

const LimitdClient = require('limitd-client');

const limitd = new LimitdClient({
  shard: {
    hosts: [ 'limitd://host-1', 'limitd://host-2' ]
  }
});

limitd.take('ip', 'test', (err, resp) => console.dir(resp));

Alternatively you can have a DNS record and use autodiscovery:

const LimitdClient = require('limitd-client');

const limitd = new LimitdClient({
  shard: {
    autodiscovery: 'limitds.internal.company.com'
  }
});

limitd.take('ip', 'test', (err, resp) => console.dir(resp));

This record will be poll every 5 minutes.

License

MIT 2015 - AUTH0 INC.