An open API service indexing awesome lists of open source software.

https://github.com/blackglory/extra-pool

🌲 A library that helps you create object/thread/connection pools
https://github.com/blackglory/extra-pool

browser library nodejs npm-package typescript

Last synced: 3 months ago
JSON representation

🌲 A library that helps you create object/thread/connection pools

Awesome Lists containing this project

README

          

# extra-pool
A library that helps you create object/thread/connection pools.

## Install
```sh
npm install --save extra-pool
# or
yarn add extra-pool
```

## Usage
```ts
import { Pool } from 'extra-pool'

const dbConnectionPool = new Pool({
create: () => Database.connect(/* ... */)
, destroy: connection => conneciton.close()
, minInstances: 1
, maxInstances: 8
, idleTimeout: 1000 * 60
})

const rows = await dbConnectionPool.use(connection => connection.query(/* ... */))

await dbConnectionPool.destroy()
```

## API
### Pool
```ts
interface IPoolOptions {
create: () => Awaitable
destroy?: (instance: T) => Awaitable
maxInstances?: number
minInstances?: number = 0
idleTimeout?: number = 0
concurrencyPerInstance? = 1
}

class Pool {
readonly capacity: number
readonly size: number

constructor(options: IPoolOptions)

prewarm(targetInstances: number): Promise

use(fn: (instance: T) => Awaitable): Promise

destroy(): Promise
}
```