Generic resource pooling for fibjs
Install
1npm install fib-pool [--save]
Test
1npm run ci
Creating a pool
Simple example.
1
2
3
4
5
6var 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
6var 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
13var 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
17var 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
16var 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
8var db = require("db");
var Pool = require("fib-pool");
var p = Pool(() => {
return db.open("sqlite:test.db");
});
p.clear();