How to use the Pool function from generic-pool

Find comprehensive JavaScript generic-pool.Pool code examples handpicked from public code repositorys.

generic-pool.Pool is a Node.js library that provides a generic object pool implementation with configurable object creation, validation, and destruction semantics.

69
70
71
72
73
74
75
76
77
78
        });
    });
}

schema.adapter = new RethinkDB(s, schema);
schema.adapter.pool = gpool.Pool({
    name: "canario-rethink-pool",
    create: connect,
    destroy: function (client) {
        client.close();
fork icon0
star icon0
watch icon1

+ 38 other calls in file

How does generic-pool.Pool work?

generic-pool.Pool is a library for creating and managing object pools in Node.js, allowing for efficient reuse of expensive resources such as database connections or network sockets. It provides a set of configurable options for creating and managing the pool, such as minimum and maximum pool size, resource creation and destruction functions, and timeouts for acquiring and releasing resources. When an object is released, it is returned to the pool and made available for reuse, rather than being destroyed and recreated every time it is needed, which can improve performance and reduce resource usage.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const { createPool } = require("generic-pool");

// A factory function to create objects to be pooled
function createObj() {
  return { prop: "value" };
}

// An object to hold configuration options for the pool
const poolOpts = {
  max: 10, // Maximum number of objects to create
  min: 2, // Minimum number of objects to keep in pool
  idleTimeoutMillis: 30000, // How long an object can sit idle before being removed
  acquireTimeoutMillis: 5000, // How long to wait for an object to become available before timing out
  createTimeoutMillis: 3000, // How long to wait for an object to be created before timing out
  destroy: (obj) => {}, // A function to be called when an object is removed from the pool
  validate: (obj) => {}, // A function to be called to validate an object before it is acquired
};

// Create the pool
const pool = createPool({
  create: createObj, // The factory function to create objects to be pooled...poolOpts,
});

// Acquire an object from the pool
pool.acquire().then((obj) => {
  console.log(obj); // { prop: 'value' }
  pool.release(obj); // Release the object back to the pool
});