New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

resource-pooler

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resource-pooler

Minimal lib for managing resource access concurrent access over resources

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

Resource-Pooler

The minimal, lock-free library for managing shared access to resources.

Use cases:

  • Worker Pools
  • Request Throttling
  • Task Queues

Distribution Strategy

Resources are assigned on a FIFO (first in, first out) basis:

  1. The caller that requested a resource first will be assigned the first available resource
  2. The resource that first becomes available will be the first to be assigned

Guarantees

  1. No two callers will be able to access the same resource concurrently (as long as the API is respected)
  2. Each caller will eventually be assigned a resource (as long as one eventually becomes available)

API

ResourcePooler

Constructor accepts a factory to manage the creation, disposal and access to resources.

See below for examples.

createPool()

Helper function to create a pre-sized pool.

async function createPool(factory, size = 8) {
    const pooler = new ResourcePooler({ factory });
    await pooler.resize(size);
    return pooler;
}

Examples

Worker Pooling

Sample usage

const workerPool = await createPool({
    create() {
        return new Worker(require.resolve('./Worker.js'))
    },
    async dispose(worker) {
        await worker.terminate()
    },
})

workerPool.use((worker) => {
    worker.postMessage({
        message: "Hello World!"
    })
    return new Promise((resolve, reject) => {
        worker
          .once('message', (content) => {
            resolve(content)
          })
          .once('error', (error) => reject(error))
    })
})

FAQs

Package last updated on 16 May 2023

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc