How to use the generic-pool.createPool function in generic-pool

To help you get started, we’ve selected a few generic-pool examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / generic-pool / generic-pool-tests.ts View on Github external
const opts = {
    max: 10,
    min: 2,
    maxWaitingClients: 2,
    testOnBorrow: true,
    acquireTimeoutMillis: 100,
    fifo: true,
    priorityRange: 5,
    autostart: false,
    evictionRunIntervalMillis: 200,
    numTestsPerRun: 3,
    softIdleTimeoutMillis: 100,
    idleTimeoutMillis: 5000
};

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

pool.start();

pool.use((conn: Connection) => 'test')
    .then((result: string) => { });

pool.acquire()
    .then((conn: Connection) => {
        console.log(pool.isBorrowedResource(conn));  // => true
        return pool.release(conn);
    }).then(() => {
        return pool.acquire(5);
    }).then((conn: Connection) => {
        return pool.destroy(conn);
    }).then(() => {
        return pool.clear();
github jbaudanza / rxeventstore / src / database / redis_connections.js View on Github external
//
    // The pool should be used for WATCH/MULTI/EXEC transactions
    //
    const factory = {
      create() {
        return Promise.resolve(createClient());
      },
      destroy(client) {
        client.quit();
        return Promise.resolve();
      }
    };

    const opts = { min: 1, max: 10 };

    this.pool = genericPool.createPool(factory, opts);
  }
}
github valhalla / valhalla / lib / index.js View on Github external
return function Actor(configStr) {
        // if we can't create a new actor, throw early rather than in the actor pool
        new Valhalla(configStr);

        const actorPool = genericPool.createPool(actorFactory, opts);

        function actorMethodFactory(methodName) {
            return function(request, cb) {
                if (!request || !cb) throw new Error('method must be called with string and callback');

                actorPool.acquire().then(function(actor) {
                    actorPool.on('factoryCreateError', function(err) {
                        return cb(err);
                    });

                    actor[methodName](request, function(err, result) {
                        actorPool.release(actor);
                        return cb(err, result);
                    });
                });
            }
github hsuehic / react-wechat-backend / middlewares / koa-mongo.js View on Github external
function mongo(options) {
  options = Object.assign({}, defaultOptions, options);
  let mongoUrl = options.uri || options.url;
  if (!mongoUrl) {
    if (options.user && options.pass) {
      mongoUrl = `mongodb://${options.user}:${options.pass}@${options.host}:${options.port}/${options.db}`;
    } else {
      mongoUrl = `mongodb://${options.host}:${options.port}/${options.db}`;
    }
  }

  const mongoPool = genericPool.createPool({
    create: () => MongoClient.connect(mongoUrl),
    destroy: client => client.close()
  }, options);

  async function release(resource) {
    await mongoPool.release(resource);
    debug('Release one connection (min: %s, max: %s, poolSize: %s)', options.min, options.max, mongoPool.size);
  }

  return async function koaMongo(ctx, next) {
    return mongoPool.acquire()
      .then(async mongo => {
        ctx.mongo = mongo;
        debug('Acquire one connection (min: %s, max: %s, poolSize: %s)', options.min, options.max, mongoPool.size);
        return next();
      })
github superfly / fly / src / isolate.ts View on Github external
makePool() {
		log.debug("make a pool")
		this.pool = genericPool.createPool(isoFactory, this.options)
	}
github cube-js / cube.js / packages / cubejs-jdbc-driver / driver / JDBCDriver.js View on Github external
this.config = {
      dbType: process.env.CUBEJS_DB_TYPE,
      url: process.env.CUBEJS_JDBC_URL || dbTypeDescription && dbTypeDescription.jdbcUrl(),
      drivername: process.env.CUBEJS_JDBC_DRIVER || dbTypeDescription && dbTypeDescription.driverClass,
      properties: dbTypeDescription && dbTypeDescription.properties,
      ...config
    };

    if (!this.config.drivername) {
      throw new Error('drivername is required property');
    }
    if (!this.config.url) {
      throw new Error('url is required property');
    }

    this.pool = genericPool.createPool({
      create: async () => {
        await initMvn(config.customClassPath);
        if (!this.jdbcProps) {
          this.jdbcProps = this.getJdbcProperties();
        }
        const getConnection = promisify(DriverManager.getConnection.bind(DriverManager));
        return new Connection(await getConnection(this.config.url, this.jdbcProps));
      },
      destroy: async (connection) => {
        return promisify(connection.close.bind(connection));
      },
      validate: (connection) => {
        const isValid = promisify(connection.isValid.bind(connection));
        try {
          return isValid(this.testConnectionTimeout() / 1000);
        } catch (e) {
github guidesmiths / rascal / lib / amqp / Vhost.js View on Github external
function drain(next) {
      debug('Draining %s channel pool. %o', mode, stats());
      pool.drain().then(function() {
        return pool.clear().then(function() {
          debug('Drained %s channel pool. %o', mode, stats());
          setImmediate(next);
        });
      }).catch(function(err) {
        debug('Error draining %s channel pool. %s', mode, err.message);
        setImmediate(next);
      });
    }

    debug('Creating %s channel pool %o', mode, options.pool);
    pool = genericPool.createPool(factory, options.pool);

    poolQueue = async.queue(function(__, next) {
      pool.acquire().then(function(channel) {
        setImmediate(function() {
          next(null, channel);
        });
      }).catch(next);
    }, 1);

    return {
      stats: stats,
      borrow: borrow,
      return: release,
      drain: drain,
      pause: poolQueue.pause.bind(poolQueue),
      resume: poolQueue.resume.bind(poolQueue),
github cksource / mrgit / lib / utils / createforkpool.js View on Github external
module.exports = function createForkPool( childPath ) {
	const forkPoolFactory = {
		create() {
			return new Promise( resolve => {
				resolve( childProcess.fork( childPath ) );
			} );
		},

		destroy( child ) {
			child.kill();
		}
	};

	const pool = genericPool.createPool( forkPoolFactory, {
		max: 4,
		min: 2
	} );

	return {
		get isDone() {
			return !pool.pending && !pool.borrowed;
		},

		enqueue( data ) {
			return new Promise( ( resolve, reject ) => {
				pool.acquire()
					.then( child => {
						child.once( 'message', returnedData => {
							pool.release( child );

generic-pool

Generic resource pooling for Node.JS

MIT
Latest version published 2 years ago

Package Health Score

76 / 100
Full package analysis

Popular generic-pool functions