new Pool(options, min, max, max_queue, timeout, idle)
- Description:
Create a resource pool,
createandclosecallbacks must be given which perform allocation and deallocation of the resources like db connections.
Parameters:
| Name | Type | Description | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object | defines the following properties Properties
|
||||||||||||||||||||||||||||
min |
int | min number of active resource items |
||||||||||||||||||||||||||||
max |
int | max number of active resource items |
||||||||||||||||||||||||||||
max_queue |
int | how big the waiting queue can be, above this all requests will be rejected immediately |
||||||||||||||||||||||||||||
timeout |
int | number of milliseconds to wait for the next available resource item, cannot be 0 |
||||||||||||||||||||||||||||
idle |
int | number of milliseconds before starting to destroy all active resources above the minimum, 0 to disable. If no create implementation callback is given then all operations are basically noop still calling the callbacks. |
Example
var pool = new Pool({
min: 1,
max: 5,
create: function(cb) {
someDb.connect((err) => { cb(err, this) }
},
destroy: function(client) {
client.close() }
})
});
pool.use((err, client) => {
...
pool.release(client);
});
const { err, client } = await pool.ause();
if (!err) {
...
pool.release(client);
}