Socket
Socket
Sign inDemoInstall

tarn

Package Overview
Dependencies
1
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

tarn

Simple and robust resource pool for node.js


Version published
Maintainers
1
Install size
631 kB
Created

Package description

What is tarn?

The tarn.js package is a small, robust resource pool for JavaScript. It is primarily used for managing a pool of resources such as database connections, ensuring efficient resource utilization and scalability in applications. It provides features like acquiring and releasing resources, timeout handling, and error management.

What are tarn's main functionalities?

Resource Pool Management

This code demonstrates how to create a resource pool with minimum and maximum limits, and manage resource acquisition and release. It handles timeouts and resource reaping intervals.

const { Pool } = require('tarn');
const pool = new Pool({
  create: () => Promise.resolve(new Resource()),
  destroy: resource => Promise.resolve(resource.dispose()),
  min: 2,
  max: 10,
  acquireTimeoutMillis: 30000,
  idleTimeoutMillis: 30000,
  reapIntervalMillis: 1000
});

pool.acquire().promise.then(resource => {
  console.log('Resource acquired');
  pool.release(resource);
});

Other packages similar to tarn

Readme

Source

Build Status

Why yet another resource pool?

Tarn is focused on robustness and ability to recover from errors. Tarn has timeouts for all operations that can fail or timeout so that you should never end up with pool full of crap. Tarn has a comprehensive test suite and we are committed to adding tests and fixing all bugs that are found.

Tarn will always remain simple.

Install

npm install tarn

Usage:

const Tarn = require('tarn').Tarn;

const pool = new Tarn({

  // function that creates a resource. You can either pass the resource
  // to the callback or return a promise that resolves the resource
  // (but not both).
  create: (cb) => {
    cb(null, new SomeResource());
  },
  
  // validates a connection before it is used. Return true or false
  // from it. If false is returned, the resource is destroyed and a
  // another one is acquired.
  validate: (resource) {
    return true;
  },
  
  // function that destroys a resource. This is always synchronous
  // as nothing waits for the return value.
  destroy: (someResource) => {
    someResource.cleanup();
  },
  
  // minimum size
  min: 2,
  
  // maximum size
  max: 10,
  
  // acquire promises are rejected after this many milliseconds
  // if a resource cannot be acquired
  acquireTimeoutMillis: 30000,
  
  // create operations are cancelled after this many milliseconds
  // if a resource cannot be acquired
  createTimeoutMillis: 30000,
  
  // free resouces are destroyed after this many milliseconds
  idleTimeoutMillis: 30000,
  
  // how often to check for idle resources to destroy
  reapIntervalMillis: 1000
});

pool.acquire().promise.then(someResource => {
  return useResource(someResource);
}).then(someResource => {
  pool.release(someResource);
});

// returns the number of non-free resources
pool.numUsed()

// returns the number of free resources
pool.numFree()

// how many acquires are waiting for a resource to be released
pool.numPendingAcquires()

// how many asynchronous create calls are running
pool.numPendingCreates()

Keywords

FAQs

Last updated on 27 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc