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

resource-pools

v2.0.1

Published

JavaScript module for pooling objects to handle them as resources allocated by demand

Downloads

6

Readme

resource-pools

Purpose

This Javascript module introduces a ResourcePool class as an abstraction to manage objects that can be pooled and allocated on demand.

Usage

Once your resource class is compatible with the pool interface (see the Pooled resources paragraph below) you provide a new pool with some basic settings:

const resources = new ResourcePool(config); // create the pool object

...and simply request the pool to allocate a resource wherever it is needed (you always get a promise in return):

resources.allocate().then( obj => /* call for an obj action here */);
// or
obj = await resources.allocate();

Config parameters

A config object has some required and optional parameters:

const config = {
    // required parameters:
    constructor: /* reference to the constructor of pooled objects */,
    arguments: /* arguments for the pooled objects constructor */,
    maxCount: /* maximum number of objects in the pool */,

    // optional:
    log: /* function to which the resource pool object will pass log messages */,
    requestTimeout: /* the request will be rejected if no resource is available within this period, ms */,
    busyTimeout: /* if the resource is stuck in busy state for longer than this, it is deleted from the pool and its close method is being called, ms */,
    idleTimeout: /* the resource is released from the pool and closed if it is not used for longer than this, ms */
}

Log function

The arguments of log function are logLevel and the message. Logging levels are:

  • 0: errors
  • 1: resource assign / release messages
  • 2: internal pool events

Example log function

log: (logLevel, ...args) => {
    /* errors & logs to console */
    if(logLevel < 1) {
        console.error(...args);
    }
    else{
        console.log(...args);
    }
}

Timeouts

If any of the timeouts is not set explicitly, its internal default value is used:

  • requestTimeout: 1 minute
  • busyTimeout: 1 minute
  • idleTimeout: 24 hours

NB: busyTimeout should be greater than it takes a new resource to get ready for being allocated, otherwise new resources will be continuously created and closed until the allocation request timeout is reached and the request id finally rejected.

Pooled resources

Pooled objects must implement the following interface:

  • emit a specific event when it is ready to be allocated for the next task (referenced by a readyEventSym symbol);
  • emit a specific event on error, when the resource is no longer capable of operating and should be deleted from the pool (referenced by an errorEventSym symbol);
  • have a method to properly be shutdown by the pool object (referenced by a closeMethodSym symbol).

Example 1, declaration of a pooled 'tedious' connection:

This implementation is available as a resource-pools-connection package

const {Connection} = require('tedious');
const {readyEventSym, errorEventSym, closeMethodSym} = require('resource-pools');

class ConnectionResource extends Connection {
    constructor(...args) {
        super(...args);
        this.once('connect', err => this.emit(err ? errorEventSym : readyEventSym, err) );
        this.once('error', err => this.emit(errorEventSym, err) );
        this.once('errorMessage', err => this.emit(errorEventSym, err) );
    }

    execSql(...[request, rest]) {
        super.execSql(...[request, rest]);
        request.once('requestCompleted', () => this.emit(readyEventSym));
    }
}
ConnectionResource.prototype[closeMethodSym] = function(...args) { this.close(...args) };

Example 2, declaration of a pooled worker:

This implementation is available as a resource-pools-worker package

const {Worker} = require('worker_threads');
const {readyEventSym, errorEventSym, closeMethodSym} = require('resource-pools');

class WorkerResource extends Worker {
    constructor(...args) {
        super(...args);
        this.once('online', () => this.emit(readyEventSym) );
        this.once('error', () => this.emit(errorEventSym) );
        this.on('message', () => this.emit(readyEventSym) );
    }
}
WorkerResource.prototype[closeMethodSym] = function(...args) { this.terminate(...args) };