How to use the createPool function from generic-pool

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

generic-pool.createPool is a function that creates a pool of reusable resources, such as database connections or network sockets, that can be borrowed and returned by multiple parts of a program, improving performance and efficiency.

324
325
326
327
328
329
330
331
332
333
      setImmediate(next);
    });
}

debug('Creating %s channel pool %o', mode, options.pool);
pool = genericPool.createPool(factory, options.pool);
pool.on('factoryCreateError', (err) => {
  debug('Create error emitted by %s channel pool: %s', mode, err.message);
});
pool.on('factoryDestroyError', (err) => {
fork icon63
star icon398
watch icon18

+ 37 other calls in file

25
26
27
28
29
30
31
32
33
34
35
  const opts = {
    min: maxParallelConnections,
    max: maxParallelConnections,
    acquireTimeoutMillis: acquireTimeoutSec * 1000,
  };
  return genericPool.createPool(factory, opts);
}


module.exports = {
  initHttpPool,
fork icon1
star icon0
watch icon4

+ 35 other calls in file

How does generic-pool.createPool work?

When called, the generic-pool.createPool function initializes a new pool of resources with a specified minimum and maximum number of items. As resource requests are made by different parts of a program, the pool either provides a resource from the pool, if one is available, or creates a new resource if the pool is not yet at its maximum capacity. When a resource is no longer needed, it is returned to the pool so that it can be reused by other parts of the program. The pool also has configurable options to control how resources are created, validated, and destroyed, ensuring that only healthy resources are provided to program components.

339
340
341
342
343
344
345
346
347

function onPoolError (err) {
    logger.logerror(`[smtp_client_pool] ${err.message}`)
}

pool = generic_pool.createPool(factory, config);
pool.on('factoryCreateError', onPoolError)
pool.on('factoryDestroyError', onPoolError)
pool.start()
fork icon1
star icon0
watch icon1

+ 38 other calls in file

15
16
17
18
19
20
21
22
23
24
async function destroy(map) {
  map.release();
  return true;
}

return createPool(
  {
    create,
    destroy,
  },
fork icon0
star icon3
watch icon12

+ 39 other calls in file

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
29
30
31
32
33
34
35
36
37
38
const { createPool } = require("generic-pool");
const mysql = require("mysql");

// Set up a factory function to create new database connections
const factory = {
  create: function () {
    return mysql.createConnection({
      host: "localhost",
      user: "db_user",
      password: "db_password",
      database: "my_database",
    });
  },
  destroy: function (connection) {
    connection.end();
  },
};

// Set up pool options
const options = {
  max: 10,
  min: 2,
  acquireTimeoutMillis: 10000,
  idleTimeoutMillis: 30000,
};

// Create a new pool of database connections
const pool = createPool(factory, options);

// Borrow a connection from the pool and execute a query
pool.acquire().then((connection) => {
  connection.query("SELECT * FROM users", (error, results) => {
    // Handle results or error here
    // ...
    // Release the connection back to the pool when done
    pool.release(connection);
  });
});

In this example, the createPool function is used to create a pool of up to 10 database connections, with a minimum of 2 connections always available. The factory object is used to create and destroy database connections, with the create method creating a new connection to a MySQL database using the mysql package, and the destroy method ending the connection when it is no longer needed. Program components can borrow a connection from the pool by calling the pool.acquire() method, which returns a promise that resolves with a connection object when a connection is available. When the program component is finished using the connection, it releases it back to the pool by calling pool.release(connection). The options object also includes some additional configuration options, such as acquireTimeoutMillis, which specifies how long the program component should wait to acquire a connection from the pool before timing out, and idleTimeoutMillis, which specifies how long a connection can remain idle in the pool before it is destroyed.

40
41
42
43
44
45
46
47
48
49
50
51
                helper.error("close browser fail:" + e.toString())
            }
        }
    };


    return genericPool.createPool(puppeteerFactory, opts);
};


const sleep = async function(timeout){
    return new Promise(function(resolve){
fork icon0
star icon9
watch icon1

10
11
12
13
14
15
16
17
18
19
20
        log: false,
        max: 50,
        min: 0,
        idleTimeoutMillis: 30000
    }
    return genericPool.createPool(factory, { ...opt, ..._.config.dbPool })
}


const createSqlSeries = (pool, query, db) => (callback, done) => (req, res, next) => {
    const _execute = (conn) => {
fork icon0
star icon0
watch icon2

+ 39 other calls in file

26
27
28
29
30
31
32
33
34
35
36
37
const opts = {
    max: 15, // maximum size of the pool
    min: 3 // minimum size of the pool
};


const myPool = genericPool.createPool(factory, opts);


const cubrid = {
    DBConnect: async (callback) => {
        console.log('DBConnect : url = ' + dbConf.host);
fork icon0
star icon0
watch icon1

+ 38 other calls in file

279
280
281
282
283
284
285
286
287
288
	testOnBorrow: true,		// validate before giving connection
	acquireTimeoutMillis: 5000,		// 5 second acquire timeout
	evictionRunIntervalMillis: 60000, // 60 second interval for running the eviction checker
	softIdleTimeoutMillis: 300000	// 5 minute timeout for inactive connections
};
poollist[name] = genericPool.createPool(factory, popts);
poollist[name].nfylen = 0;	// notify packet current length, zero = waiting for new packet.
let nfSocket = net.createConnection(port, host, function(){
		nfSocket.setKeepAlive(true);
		nfSocket.write('notify\n');
fork icon0
star icon0
watch icon2

+ 34 other calls in file

7
8
9
10
11
12
13
14
15
16
17
  min: process.env.RedisPoolMin ? process.env.RedisPoolMin : 4,
  max: process.env.RedisPoolMax ? process.env.RedisPoolMax : 8
}


// 创建 Redis 连接池
const redisPool = createPool(
  {
    create: () =>
      redis.createClient({
        host: process.env.RedisHost ? process.env.RedisHost : undefined,
fork icon0
star icon0
watch icon1

+ 2 other calls in file