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

olpo

v0.5.2

Published

Olpo: A type-safe TypeScript resource pool

Downloads

16

Readme

OLPO - A TypeScript Resource Pool

CircleCI codecov

OLPO is yet another TypeScript resource pool. It's written to be small (~5.5k minified), fast, Promise-native, and written in TypeScript. It also has no external dependencies.

100% test coverage is a goal of the project.

Requires ES6 support.

Documentation

Documentation is available here

Documentation is updated every version bump. A changelog is available here.

Motivation

There's a lot of pools out there. A lot of them have a lot of features, but don't have the particular intersection of features I wanted.

  • Asynchronous support everywhere
  • Verification of pool items
  • Idle timeouts
  • TypeScript ready, and checked

Usage

Create a pool:

import { Pool } from 'olpo'
import { SomeClass } from 'somewhere'

const pool = new Pool({
  // Required parameters:

  // Synchronous or asynchronous function that creates pool
  // items. Item creation can be synchronous or asynchronous.
  create: async () => {
    const cls = new SomeClass()
    await cls.connect()
    return cls
  }
  // Maximum pool size, must be > `min`
  max: 10,

  // Optional parameters:

  // Minimum pool size
  min: 2,
  // Which promise library to use (uses builtin ES6 Promise by default)
  promise: Bluebird,
  // Default timeout in ms if none is specified during `acquire`
  acquireTimeout: 5000,
  // Timeout in ms past which idle pool items will be `dispose`d of, so long as
  // that disposal doesn't reduce past the minimum pool size
  idleTimeout: 300000,

  // Called to verify a pool item to determine if it's still valid. Can be
  // be synchronous or asynchronous. This function should return `true` or
  // `false`.
  verify(acq) {
    const now = new Date();
    if (acq.uses > 30) {
      return false;
    }
    if (now - acq.createTime > 60000) {
      return false;
    }
    if (now - acq.lastReleaseTime > 20000) {
      return false;
    }
    if (now - acq.lastAcquireTime > 10000) {
      return false;
    }
    return Promise.resolve(asyncCheckIsOk(acq.item))
  },

  // Called when disposing of a pool item (due to pool shutdown or
  // if verification fails, or if an item is released to the pool with
  // the `dispose` flag set to true).
  // This method can be synchronous or asynchronous. If asynchronous, the
  // disposal promise will be awaited before space in the pool is released.
  dispose(acq) { console.log('Disposed: ', acq.item) },


  // Callbacks. These are mainly event callbacks useful for logging -- be sure
  // not to throw errors from here, as they are not handled within the pooling
  // code:

  // Called immediately before an item is acquired
  onAcquire(acq) { console.log('Acquired: ', acq.item) },

  // Called immediately after an item has been released, potentially
  // immediately before it's disposed, if necessary
  onRelease(acq) { console.log('Released: ', acq.item) },

  // Called when an acquire fails (mainly for logging)
  onTimeout({ timeout }) { console.log('Timeout hit: ', timeout) },

  // Called when an asynchronous error occurs -- `type` can be `'create'`,
  // `'verify'`, or `'dispose'`, indicating errors that occur during those
  // operations
  onError(type, err) {
    console.log('Uncaught error when performing ' + type + ': ' + err)
  }
})

And then acquire resources:

pool.acquire({ timeout: 1000 }).then(poolItem => {
  const someClassInstance = poolItem.item
  someClassInstance.doStuff()
  // Be sure to release
  poolItem.release()
})

Or acquire them this way (the acquired item is automatically released when the callback has completed):

pool.acquire(someClassInstance => {
  someClassInstance.doStuff()
}, { timeout: 1000 })

Maybe you want to release a resource but not add it back to the pool. Easy:

pool.acquire({ timeout: 1000 }).then(poolItem => {
  const someClassInstance = poolItem.item
  const wasSuccessful = someClassInstance.doStuff()
  // If `true` is passed to release, it disposes of the returned
  // client
  poolItem.release(!wasSuccessful)
})

License

MIT, available here.