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

api-concurrency

v1.3.0

Published

Lock API when a request arrives and fail other requests on same API with same payload thereby restricting duplicate requests

Downloads

20

Readme

JavaScript Style Guide

api-concurrency

Function to restrict duplicate HTTP requests.

If an API request arrives for the very first time it will lock it (using redis) and another request for same API with same request body arrive even before the response of previous API is sent, it will return an error in response for that second (duplicate) request.

Tip: To increase the throughput of this function, connect the redis client with redis server using unix sockets

Installation

npm install api-concurrency

Usage

var express = require('express')
var apiLock = require('api-concurrency')
var redisClient = require('redis').createClient()
var app = express()

app.use(function (req, res, next) {
    var options = {
        payload: {
            path: req.path, // path should always be given in payload to determine same endpoint
            body: req.body // make sure your body contains a unique key for a given session
        },
        ttl: 60000,
        silent: false,
        key_prefix: 'apiLock:' // a prefix to redis's key
    }
    
    /*
     * Following function will invoke the next middleware with or without error depending on weather a request is duplicate or not.
     **/
    apiLock(redisClient, options, res, next)
})

app.get('/', function(req, res) {
  // We delay the response in order to mock the runtime of an actual API.
  // Now, you can try and hit the same API before 2 secs and it should throw an error
  setTimeout(() => {
    return res.status(200).json('Hello, World!')
  }, 2000)
})

app.listen(3000, function() {
  console.log('Server listening at port 3000')
})

Options

key | data type | description ------------ | ------------ | ------------- payload | string/number/object/array (mandatory) | this value will be used to determine if a given request if duplicate or not ttl | number (optional, default: 60000) | value (in millisecond) to expire the hashkey regardless of response was sent or not silent | boolean (optional, default: false) | if true, it will block the API execution in case of redis error key_prefix | string (optional, default: 'ApiLock') | this string will be added as prefix to the key name in redis error_message | string (optional, default: 'Resource is busy') | this string will contain the error message which the function will pass to next middleware in case of duplicate request