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

connect-once

v0.1.1

Published

Request connection once and memorize it for later

Downloads

69

Readme

connect-once NPM version Build Status Coverage Status Dependency Status

Memorizing async function result

This module solves problem with connection creating in Node.JS.

Most known example - MongoDB connections. Often you want to get one connection shared with all incoming requests. However there are two edges, that can hurt you, when your application starting under significant load. Connection should be:

  • Created once for all requests, that are coming at the same time - otherwise you can take down your database connection pool.
  • Ready before first request arrives - to reduce page loading time.

Also it is nice to have some retries, when database decides to take a rest. For this (and other) tasks connect-once was written.

Usage

You can see express-mongo-db and express-mongoose-db as real-life examples of connect-once usage.

Install package with npm:

npm i connect-once --save

Then create a connection:

var connectOnce = require('connect-once');

var connection = new connectOnce({ 
    retries: 60, 
    reconnectWait: 1000
}, MongoClient.connect, 'mongodb://localhost/test');

connection.when('available', function (err, db) {
    // Do stuff
});

Heartbeat

Heartbeat is useful, when you want check connection from time to time. For example, if you working with mongodb and cache connection to db - what happens when server, which connection binded to goes to heaven? Connection is lost and programmer should recreate it.

Note: for now there is no way to stop heartbeats.

var connectOnce = require('connect-once');

var connection = new connectOnce({ 
    heartbeat: function (db, beat) {
        db.stats(function (err) {
            if (!err) { beat(); }
        });
    }
}, MongoClient.connect, 'mongodb://localhost/test');

connection.when('available', function (err, db) {
    // Do stuff
});

Force retry (update cached value)

There are interesting use case of connect-once in a wild: it can be used as some sort of in-memory cache. Because it consumes general function with callback - there is no difference, what to memorize - connection or abstract object.

Suppose you have a function getCache(callback), that invokes callback with next arguments: null, new Date(). To update cached value from time-to time you can use next code:

var connectOnce = require('connect-once');
var connection = new connectOnce(getCache);
setInterval(function () {
    connection.retry(new Error('Update cache'));
}, 5000);

Also available event will be fired every 5000 seconds.

API

You can read source code (65 sloc!) or generated documentation of Connection class which is not very nice.

License

(MIT License) (c) 2013 Vsevolod Strukchinsky ([email protected])