Awesome 社区模块

Generic resource pooling for fibjs

Build Status

Install

1
npm install fib-pool [--save]

Test

1
npm run ci

Creating a pool

Simple example.

1 2 3 4 5 6
var db = require("db"); var Pool = require("fib-pool"); var p = Pool(() => { return db.open("sqlite:test.db"); });

Specify maxsize and timeout.

1 2 3 4 5 6
var db = require("db"); var Pool = require("fib-pool"); var p = Pool(() => { return db.open("sqlite:test.db"); }, 10, 30 * 1000);

Specify custom destroy function.

1 2 3 4 5 6 7 8 9 10 11 12 13
var db = require("db"); var Pool = require("fib-pool"); var p = Pool({ create: () => { return db.open("sqlite:test.db"); }, destroy: (o) => { o.close() }, timeout: 30 * 1000, retry: 3 });

Using the pool

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var db = require("db"); var Pool = require("fib-pool"); var p = Pool({ create: () => { return db.open("sqlite:test.db"); }, destroy: (o) => { o.close() }, timeout: 30 * 1000, retry: 3 }); var res = p((conn) => { conn.execute("select * from test"); });

Using the pool with name

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var db = require("db"); var Pool = require("fib-pool"); var p = Pool({ create: (name) => { return db.open("sqlite:" + name + ".db"); }, destroy: (o) => { o.close() }, timeout: 30 * 1000 }); var res = p("test", (conn) => { conn.execute("select * from test"); });

Clear a pool

Simple example.

1 2 3 4 5 6 7 8
var db = require("db"); var Pool = require("fib-pool"); var p = Pool(() => { return db.open("sqlite:test.db"); }); p.clear();