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

nodejs-broker

v0.1.5

Published

nodejs broker is an operation broker that narrows down multiple operations of the same type being executed to a single operation, while an operation is under execution every other operation of the same type blocks on that operation pending for its results

Downloads

2

Readme

Nodejs-Broker

nodejs broker is an operation broker that narrows down multiple operations of the same type being executed to a single operation, while an operation is under execution every other operation of the same type blocks on that operation pending for its results

Version: 0.1.4

How it works

Suppose you have a database which contains 10K records and these records are being queried by millions of users. Assume in a given moment 100 requests are being processed to read the same record for 100 users this seems like a huge waste. Or assume that you have a website and an article on your website has gone viral, serving the same article many times at the same moment also seems like a huge waste. Nodejs-Broker allows you to process only one type of the operation reading the same set of data and all other equivalent operations block on that operation. Hence, lower response time and better resource utilization. Some might argue that such functionality makes errors impact higher, instead of failing a single request/operation, hunderds of operations will fail. That I can't argue with but hunderds of operations failing at once better than failing one on its own. To lower the error impact a retrial count is provided with each broker, however this retrial count is a hard limit. In ideal cases, an operation is retried by the number of operations pending for the result. For example, If 3 requests are passed to a broker at the same instance with retrial hard limit 5 an operation can be retried up to 3 times, we call this "operation rank".

Limits

Nodejs-Broker operations for the time being can only be promise (http://promisejs.com)

An Operation

An operation should have (type, key, value[optional]) an operation type can either be (read, create, update, delete, insert). all types of operations must have a key operations of the same key block on each others operations such as (create, update, insert) must have a value.

Example

Coffeescript

NBroker = require("nodejs-broker")
b = new NBroker.Broker("My Custom Broker", 5) // 5 is the retrial hard limit
class ReadOp
  constructor:(@key) ->
    return
  commit:() ->
    self = @
    return new Promise((fulfill, reject) ->
      request.get(self.key, (errs, response, body) ->
        if !errs
          fulfill body
      )
    )

b.execute ReadOp, "read", "http://example.com"

Javascript

const NBroker = require("nodejs-broker")
b = new NBroker.Broker("My Custom Broker", 5)

var ReadOp = function(key){
  this.key = key
}
ReadOp.prototype.commit = function(){
  var self = this
  return new Promise(function(fulfill, reject) {
      request.get(self.key, function(errs, response, body){
          if (!errs)
            fulfill(body)
        }
      )
    }
  )
}

b.execute(ReadOp, "read", "http://example.com")

Broker Operations

You might find defining a new class for each operation is too much of a hassle. However, you shouldn't use a broker if it's not worth it. To lessen the hassle, Nodejs-Broker provides a base class called BrokerOp which you can inherit from. However your promise should always be wrapped in an uninstansiated type to avoid being triggered.

Using BrokerOp

NBroker = require("nodejs-broker")
b = new NBroker.Broker("My Custom Broker", 5) // 5 is the retrial hard limit
class ReadOp extends NBroker.BrokerOp
  commit:() ->
    self = @
    return new Promise((fulfill, reject) ->
      request.get(self.key, (errs, response, body) ->
        if !errs
          fulfill body
      )
    )

b.execute ReadOp, "read", "http://example.com"

Javascript

const NBroker = require("nodejs-broker")
b = new NBroker.Broker("My Custom Broker", 5)

var ReadOp = function(){}
util.inherits(ReadOp, NBroker.BrokerOp)

ReadOp.prototype.commit = function(){
  var self = this
  return new Promise(function(fulfill, reject) {
      request.get(self.key, function(errs, response, body){
          if (!errs)
            fulfill(body)
        }
      )
    }
  )
}

b.execute(ReadOp, "read", "http://example.com")

Testing Nodejs-Broker limits

This script creates a readop which invokes a request for http://example.com 100 times. In standard cases, 100 times will take ages to complete. However, since only one request is invoked all 100 operations are fulfilled after one operation is complete.

request = require("request")
NBroker = require("nodejs-broker")

b = new NBroker.Broker("My Custom Broker", 5) # 5 is the retrial hard limit
passedreq = 0

class ReadOp extends NBroker.BrokerOp
  commit:() ->
    self = @
    return new Promise((fulfill, reject) ->
      request.get(self.key, (errs, response, body) ->
        if !errs
          console.log("Request passed")
          fulfill body
      )
    )
    
for i in [1..100]
    (b.execute ReadOp, "read", "http://example.com")

Try this on your machine.