fib-pool 1.5.4 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +21 -0
- package/appveyor.yml +3 -0
- package/lib/index.js +38 -45
- package/package.json +8 -5
- package/tsconfig.json +6 -1
- package/typings/index.d.ts +41 -0
- package/@types/index.d.ts +0 -75
- package/test/fixtures/block-avoid.js +0 -16
- package/test/fixtures/block.js +0 -23
- package/test/index.js +0 -436
- /package/{README.md → Readme.md} +0 -0
package/.travis.yml
CHANGED
|
@@ -26,6 +26,15 @@ matrix:
|
|
|
26
26
|
- os: linux
|
|
27
27
|
env:
|
|
28
28
|
- VERSION=0.27.0
|
|
29
|
+
- os: linux
|
|
30
|
+
env:
|
|
31
|
+
- VERSION=0.28.0
|
|
32
|
+
- os: linux
|
|
33
|
+
env:
|
|
34
|
+
- VERSION=0.29.0
|
|
35
|
+
- os: linux
|
|
36
|
+
env:
|
|
37
|
+
- VERSION=0.30.0
|
|
29
38
|
- os: osx
|
|
30
39
|
osx_image: xcode9
|
|
31
40
|
env:
|
|
@@ -58,6 +67,18 @@ matrix:
|
|
|
58
67
|
osx_image: xcode9
|
|
59
68
|
env:
|
|
60
69
|
- VERSION=0.27.0
|
|
70
|
+
- os: osx
|
|
71
|
+
osx_image: xcode9
|
|
72
|
+
env:
|
|
73
|
+
- VERSION=0.28.0
|
|
74
|
+
- os: osx
|
|
75
|
+
osx_image: xcode9
|
|
76
|
+
env:
|
|
77
|
+
- VERSION=0.29.0
|
|
78
|
+
- os: osx
|
|
79
|
+
osx_image: xcode9
|
|
80
|
+
env:
|
|
81
|
+
- VERSION=0.30.0
|
|
61
82
|
before_install:
|
|
62
83
|
- OS=linux
|
|
63
84
|
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
|
package/appveyor.yml
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
const coroutine = require("coroutine");
|
|
2
3
|
const util = require("util");
|
|
3
|
-
const timers_1 = require("timers");
|
|
4
4
|
function ensureError(e) {
|
|
5
5
|
if (typeof e === 'string')
|
|
6
6
|
e = new Error(e);
|
|
7
7
|
return e;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
module.exports = function GetPool(_opt, maxsize, timeout) {
|
|
10
|
+
let opt = _opt;
|
|
11
11
|
if (util.isFunction(_opt)) {
|
|
12
12
|
opt = {
|
|
13
13
|
create: _opt,
|
|
@@ -15,8 +15,8 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
15
15
|
timeout: timeout
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const create = opt.create;
|
|
19
|
+
const destroy = opt.destroy || ((o) => {
|
|
20
20
|
if (util.isFunction(o.close))
|
|
21
21
|
o.close();
|
|
22
22
|
if (util.isFunction(o.destroy))
|
|
@@ -26,19 +26,19 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
26
26
|
});
|
|
27
27
|
maxsize = opt.maxsize || 10;
|
|
28
28
|
timeout = opt.timeout || 60000;
|
|
29
|
-
|
|
29
|
+
let tm = timeout / 10;
|
|
30
30
|
if (tm < 10)
|
|
31
31
|
tm = 10;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
const retry = opt.retry || 1;
|
|
33
|
+
let pools = [];
|
|
34
|
+
const jobs = [];
|
|
35
|
+
let count = 0;
|
|
36
|
+
let running = 0;
|
|
37
|
+
const sem = new coroutine.Semaphore(maxsize);
|
|
38
|
+
let clearTimer;
|
|
39
39
|
function clearPool() {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
let c;
|
|
41
|
+
const d = new Date().getTime();
|
|
42
42
|
while (count) {
|
|
43
43
|
c = pools[0];
|
|
44
44
|
if (d - c.time.getTime() > timeout) {
|
|
@@ -57,12 +57,12 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
else if (!clearTimer)
|
|
60
|
-
clearTimer =
|
|
60
|
+
clearTimer = setInterval(clearPool, tm);
|
|
61
61
|
}
|
|
62
62
|
function putback(name, o, e) {
|
|
63
63
|
e = ensureError(e);
|
|
64
|
-
for (
|
|
65
|
-
|
|
64
|
+
for (let i = 0; i < jobs.length; i++) {
|
|
65
|
+
const job = jobs[i];
|
|
66
66
|
if (job.name === name) {
|
|
67
67
|
jobs.splice(i, 1);
|
|
68
68
|
job.o = o;
|
|
@@ -79,15 +79,16 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
function connect(name) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
let o;
|
|
83
|
+
let cn = 0;
|
|
84
|
+
let err;
|
|
85
85
|
while (true) {
|
|
86
86
|
try {
|
|
87
87
|
o = create(name);
|
|
88
88
|
break;
|
|
89
89
|
}
|
|
90
90
|
catch (e) {
|
|
91
|
+
console.log(e);
|
|
91
92
|
if (++cn >= retry) {
|
|
92
93
|
err = e;
|
|
93
94
|
break;
|
|
@@ -96,21 +97,18 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
96
97
|
}
|
|
97
98
|
putback(name, o, err);
|
|
98
99
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
var name = _name;
|
|
103
|
-
if (util.isFunction(_name)) {
|
|
104
|
-
func = _name;
|
|
100
|
+
const pool = (name, func) => {
|
|
101
|
+
if (typeof name === 'function') {
|
|
102
|
+
func = name;
|
|
105
103
|
name = "";
|
|
106
104
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
let r;
|
|
106
|
+
let o;
|
|
107
|
+
let p = false;
|
|
110
108
|
clearPool();
|
|
111
109
|
sem.acquire();
|
|
112
110
|
if (count) {
|
|
113
|
-
for (
|
|
111
|
+
for (let i = count - 1; i >= 0; i--)
|
|
114
112
|
if (pools[i].name === name) {
|
|
115
113
|
p = true;
|
|
116
114
|
o = pools[i].o;
|
|
@@ -121,7 +119,7 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
121
119
|
}
|
|
122
120
|
if (!p) {
|
|
123
121
|
coroutine.start(connect, name);
|
|
124
|
-
|
|
122
|
+
const job = {
|
|
125
123
|
name: name,
|
|
126
124
|
ev: new coroutine.Event()
|
|
127
125
|
};
|
|
@@ -150,22 +148,17 @@ const Pool = function (_opt, maxsize, timeout) {
|
|
|
150
148
|
}
|
|
151
149
|
return r;
|
|
152
150
|
};
|
|
153
|
-
pool.connections = () =>
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
wait: sem.count(),
|
|
162
|
-
timeout: timeout
|
|
163
|
-
};
|
|
164
|
-
};
|
|
151
|
+
pool.connections = () => count;
|
|
152
|
+
pool.info = () => ({
|
|
153
|
+
maxsize: maxsize,
|
|
154
|
+
count: count,
|
|
155
|
+
running: running,
|
|
156
|
+
wait: sem.count(),
|
|
157
|
+
timeout: timeout
|
|
158
|
+
});
|
|
165
159
|
pool.clear = () => {
|
|
166
160
|
timeout = -1;
|
|
167
161
|
clearPool();
|
|
168
162
|
};
|
|
169
163
|
return pool;
|
|
170
164
|
};
|
|
171
|
-
module.exports = Pool;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fib-pool",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Generic resource pooling for fibjs",
|
|
5
5
|
"main": "./lib",
|
|
6
|
-
"types": "
|
|
6
|
+
"types": "typings/index.d.ts",
|
|
7
7
|
"repository": "git://github.com/fibjs/fib-pool.git",
|
|
8
8
|
"author": "Liu Hu <lion@9465.net>",
|
|
9
9
|
"contributors": [
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"fibjs"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "
|
|
23
|
+
"build": "ftsc ./src/* --outDir lib",
|
|
24
24
|
"test": "fibjs test",
|
|
25
25
|
"ci": "npm run build && npm run test",
|
|
26
26
|
"prepublishOnly": "npm run build"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@fibjs/ci": "^2.3.0",
|
|
30
30
|
"@fibjs/types": "^0.27.1",
|
|
31
|
-
"fib-typify": "^0.
|
|
31
|
+
"fib-typify": "^0.8.4"
|
|
32
32
|
},
|
|
33
33
|
"ci": {
|
|
34
34
|
"type": "travis, appveyor",
|
|
@@ -40,7 +40,10 @@
|
|
|
40
40
|
"0.25.0",
|
|
41
41
|
"0.26.0",
|
|
42
42
|
"0.26.1",
|
|
43
|
-
"0.27.0"
|
|
43
|
+
"0.27.0",
|
|
44
|
+
"0.28.0",
|
|
45
|
+
"0.29.0",
|
|
46
|
+
"0.30.0"
|
|
44
47
|
]
|
|
45
48
|
}
|
|
46
49
|
}
|
package/tsconfig.json
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
declare namespace FibPoolNS {
|
|
2
|
+
type FirstParameter<T> = T extends (arg: infer T, ...args: any[]) => any ? T : never;
|
|
3
|
+
interface DippedItemStruct {
|
|
4
|
+
close?: FibPoolOptionResult['destroy'];
|
|
5
|
+
destroy?: FibPoolOptionResult['destroy'];
|
|
6
|
+
dispose?: FibPoolOptionResult['destroy'];
|
|
7
|
+
}
|
|
8
|
+
interface FibPoolOptionArgs<T extends DippedItemStruct = {}> {
|
|
9
|
+
create: (name: string) => T;
|
|
10
|
+
destroy?: (item?: T) => any;
|
|
11
|
+
maxsize?: number;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
retry?: number | boolean;
|
|
14
|
+
}
|
|
15
|
+
interface FibPoolOptionResult<T extends DippedItemStruct = {}> {
|
|
16
|
+
create: FibPoolOptionArgs<T>['create'];
|
|
17
|
+
destroy: FibPoolOptionArgs<T>['destroy'];
|
|
18
|
+
maxsize: FibPoolOptionArgs<T>['maxsize'];
|
|
19
|
+
timeout: FibPoolOptionArgs<T>['timeout'];
|
|
20
|
+
retry: FibPoolOptionArgs<T>['retry'];
|
|
21
|
+
}
|
|
22
|
+
interface FibPoolDipperFn<T_CREATED_ITEM, T_POOL_FUNC_RETURN = any> {
|
|
23
|
+
(o: T_CREATED_ITEM): T_POOL_FUNC_RETURN;
|
|
24
|
+
}
|
|
25
|
+
type FibPool<T_CREATED_ITEM, T_POOL_FUNC_RETURN = any> = {
|
|
26
|
+
<T_RETURN = T_POOL_FUNC_RETURN>(name: string, func: FibPoolDipperFn<T_CREATED_ITEM, T_RETURN>): T_RETURN;
|
|
27
|
+
<T_RETURN = T_POOL_FUNC_RETURN>(func: FibPoolDipperFn<T_CREATED_ITEM, T_RETURN>): T_RETURN;
|
|
28
|
+
} & {
|
|
29
|
+
connections?(): number;
|
|
30
|
+
info?(): {
|
|
31
|
+
maxsize: number;
|
|
32
|
+
count: number;
|
|
33
|
+
running: number;
|
|
34
|
+
wait: number;
|
|
35
|
+
timeout: number;
|
|
36
|
+
};
|
|
37
|
+
clear?(): void;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
declare const _default: <T_CREATED_ITEM extends FibPoolNS.DippedItemStruct, T_POOL_FUNC_RETURN>(_opt: FibPoolNS.FibPoolOptionArgs<T_CREATED_ITEM> | ((name: string) => T_CREATED_ITEM), maxsize?: number, timeout?: number) => FibPoolNS.FibPool<T_CREATED_ITEM, T_POOL_FUNC_RETURN>;
|
|
41
|
+
export = _default;
|
package/@types/index.d.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/// <reference types="@fibjs/types" />
|
|
2
|
-
|
|
3
|
-
declare namespace FibPoolNS {
|
|
4
|
-
interface FibPoolInfo {
|
|
5
|
-
maxsize: number;
|
|
6
|
-
count: number;
|
|
7
|
-
// boolean-type number
|
|
8
|
-
running: number;
|
|
9
|
-
wait: number;
|
|
10
|
-
timeout: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface FibPoolOptionCreator<T = FibPoolPayloadObject> {
|
|
14
|
-
(name: string): T;
|
|
15
|
-
}
|
|
16
|
-
interface FibPoolOptionDestoryor<T = any> {
|
|
17
|
-
(item?: T): void;
|
|
18
|
-
}
|
|
19
|
-
type FibPoolRetryCountType = number | boolean
|
|
20
|
-
interface FibPoolOptionArgs<T = any> {
|
|
21
|
-
create?: FibPoolOptionCreator
|
|
22
|
-
destroy?: FibPoolOptionDestoryor<T>
|
|
23
|
-
maxsize?: number
|
|
24
|
-
timeout?: number
|
|
25
|
-
retry?: FibPoolRetryCountType
|
|
26
|
-
}
|
|
27
|
-
interface FibPoolOptionResult {
|
|
28
|
-
create: FibPoolOptionCreator
|
|
29
|
-
destroy: FibPoolOptionDestoryor
|
|
30
|
-
maxsize: number
|
|
31
|
-
timeout: number
|
|
32
|
-
retry: FibPoolRetryCountType
|
|
33
|
-
}
|
|
34
|
-
type FibPoolOptsArg = FibPoolOptionArgs | FibPoolOptionCreator
|
|
35
|
-
|
|
36
|
-
type FibPoolPayloadObject = any
|
|
37
|
-
interface FibPoolUnit<T = FibPoolPayloadObject> {
|
|
38
|
-
o: T;
|
|
39
|
-
name: string;
|
|
40
|
-
time: Date;
|
|
41
|
-
}
|
|
42
|
-
type FibPoolInnerJobName = string;
|
|
43
|
-
interface FibPoolInnerJob<T = FibPoolPayloadObject> {
|
|
44
|
-
name: FibPoolInnerJobName;
|
|
45
|
-
ev: Class_Event;
|
|
46
|
-
o?: T;
|
|
47
|
-
// Error
|
|
48
|
-
e?: FibPoolInnerErr;
|
|
49
|
-
}
|
|
50
|
-
type FibPoolInnerErr = Error;
|
|
51
|
-
|
|
52
|
-
interface FibPoolDipperFn<DippedItem = FibPoolPayloadObject, RETURN_TYPE = any> {
|
|
53
|
-
(o: DippedItem): RETURN_TYPE
|
|
54
|
-
}
|
|
55
|
-
type FibPoolCallback<T = any, T2 = any> = FibPoolDipperFn<T, T2>
|
|
56
|
-
|
|
57
|
-
interface FibPoolFunction<DippedItem = FibPoolPayloadObject, RETURN_TYPE = any> {
|
|
58
|
-
(name: string, o: FibPoolCallback<DippedItem, RETURN_TYPE>): RETURN_TYPE
|
|
59
|
-
(o: FibPoolCallback<DippedItem, RETURN_TYPE>): RETURN_TYPE
|
|
60
|
-
connections?(): number;
|
|
61
|
-
info?(): FibPoolInfo;
|
|
62
|
-
clear?(): void;
|
|
63
|
-
}
|
|
64
|
-
interface FibPoolObjectToExtract {
|
|
65
|
-
close?: FibPoolOptionResult['destroy'] | Function;
|
|
66
|
-
destroy?: FibPoolOptionResult['destroy'] | Function;
|
|
67
|
-
dispose?: FibPoolOptionResult['destroy'] | Function;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function FibPoolGenerator<DippedItem = FibPoolPayloadObject> (opt: FibPoolOptsArg, maxsize?: number, timeout?: number): FibPoolNS.FibPoolFunction<DippedItem>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
declare module "fib-pool" {
|
|
74
|
-
export = FibPoolNS.FibPoolGenerator
|
|
75
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
var Pool = require('../../')
|
|
2
|
-
var co = require('coroutine')
|
|
3
|
-
|
|
4
|
-
var p = Pool(() => {
|
|
5
|
-
throw new Error('Test create error')
|
|
6
|
-
}, 11);
|
|
7
|
-
|
|
8
|
-
co.parallel(() => {
|
|
9
|
-
try {
|
|
10
|
-
p((pooled) => {
|
|
11
|
-
console.log('tx operation: pooled value = %s', pooled)
|
|
12
|
-
})
|
|
13
|
-
} catch(e) {
|
|
14
|
-
console.log("Can't get pooled object - %s", e)
|
|
15
|
-
}
|
|
16
|
-
}, p.info().maxsize /* 11 */ );
|
package/test/fixtures/block.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
var Pool = require('../../')
|
|
2
|
-
var co = require('coroutine')
|
|
3
|
-
|
|
4
|
-
var p = Pool(() => {
|
|
5
|
-
throw new Error('Test create error')
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
// uncomment below to see fibers before process exit.
|
|
9
|
-
// var timer;
|
|
10
|
-
// timer = setInterval(() => {
|
|
11
|
-
// console.log('current fibers', co.fibers)
|
|
12
|
-
// clearInterval(timer);
|
|
13
|
-
// }, 1000)
|
|
14
|
-
|
|
15
|
-
co.parallel(() => {
|
|
16
|
-
try {
|
|
17
|
-
p((pooled) => {
|
|
18
|
-
console.log('tx operation: pooled value = %s', pooled)
|
|
19
|
-
})
|
|
20
|
-
} catch(e) {
|
|
21
|
-
console.log("Can't get pooled object - %s", e)
|
|
22
|
-
}
|
|
23
|
-
}, 10);
|
package/test/index.js
DELETED
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
var test = require("test");
|
|
2
|
-
test.setup();
|
|
3
|
-
|
|
4
|
-
var coroutine = require("coroutine");
|
|
5
|
-
|
|
6
|
-
var Pool = require("../lib");
|
|
7
|
-
|
|
8
|
-
describe("pool", () => {
|
|
9
|
-
it("run", () => {
|
|
10
|
-
var p = Pool(() => {
|
|
11
|
-
return 10;
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
assert.equal(p((v) => {
|
|
15
|
-
return v + 1;
|
|
16
|
-
}), 11);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("pool", () => {
|
|
20
|
-
var n = 0;
|
|
21
|
-
var running = -1;
|
|
22
|
-
|
|
23
|
-
var p = Pool(() => {
|
|
24
|
-
n++;
|
|
25
|
-
return n;
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
assert.equal(p((v) => {
|
|
29
|
-
running = p.info().running;
|
|
30
|
-
return v + 1;
|
|
31
|
-
}), 2);
|
|
32
|
-
|
|
33
|
-
assert.equal(running, 1);
|
|
34
|
-
assert.equal(p.info().running, 0);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("maxsize", () => {
|
|
38
|
-
var n = 0;
|
|
39
|
-
var m = 0;
|
|
40
|
-
|
|
41
|
-
var p = Pool(() => {
|
|
42
|
-
// n++;
|
|
43
|
-
return n;
|
|
44
|
-
}, 10);
|
|
45
|
-
|
|
46
|
-
coroutine.parallel(() => {
|
|
47
|
-
p((c) => {
|
|
48
|
-
n++;
|
|
49
|
-
if (n > m)
|
|
50
|
-
m = n;
|
|
51
|
-
coroutine.sleep(50);
|
|
52
|
-
n--;
|
|
53
|
-
});
|
|
54
|
-
}, 20);
|
|
55
|
-
|
|
56
|
-
assert.equal(m, 10);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("name", () => {
|
|
60
|
-
var n = 0;
|
|
61
|
-
|
|
62
|
-
var p = Pool((name) => {
|
|
63
|
-
n++;
|
|
64
|
-
return name;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
assert.equal(n, 0);
|
|
68
|
-
|
|
69
|
-
assert.equal(p('a', (v) => {
|
|
70
|
-
return v;
|
|
71
|
-
}), 'a');
|
|
72
|
-
|
|
73
|
-
assert.equal(n, 1);
|
|
74
|
-
|
|
75
|
-
assert.equal(p('b', (v) => {
|
|
76
|
-
return v;
|
|
77
|
-
}), 'b');
|
|
78
|
-
|
|
79
|
-
assert.equal(n, 2);
|
|
80
|
-
|
|
81
|
-
assert.equal(p('a', (v) => {
|
|
82
|
-
return v;
|
|
83
|
-
}), 'a');
|
|
84
|
-
|
|
85
|
-
assert.equal(n, 2);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("throw", () => {
|
|
89
|
-
var n = 0;
|
|
90
|
-
var running = -1;
|
|
91
|
-
|
|
92
|
-
var p = Pool(() => {
|
|
93
|
-
n++;
|
|
94
|
-
return n;
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
assert.equal(p((v) => {
|
|
98
|
-
return v + 1;
|
|
99
|
-
}), 2);
|
|
100
|
-
|
|
101
|
-
assert.throws(() => {
|
|
102
|
-
p((v) => {
|
|
103
|
-
running = p.info().running;
|
|
104
|
-
throw "error";
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
assert.equal(running, 1);
|
|
108
|
-
assert.equal(p.info().running, 0);
|
|
109
|
-
|
|
110
|
-
assert.equal(p((v) => {
|
|
111
|
-
return v + 1;
|
|
112
|
-
}), 3);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("async close when throw", () => {
|
|
116
|
-
var called = false;
|
|
117
|
-
|
|
118
|
-
var p = Pool({
|
|
119
|
-
create: () => {
|
|
120
|
-
return 100;
|
|
121
|
-
},
|
|
122
|
-
destroy: (o) => {
|
|
123
|
-
called = true;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
assert.throws(() => {
|
|
128
|
-
p((v) => {
|
|
129
|
-
throw "error";
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
assert.isFalse(called);
|
|
134
|
-
coroutine.sleep(10);
|
|
135
|
-
assert.isTrue(called);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("default close function", () => {
|
|
139
|
-
var called = false;
|
|
140
|
-
|
|
141
|
-
var p = Pool({
|
|
142
|
-
create: () => {
|
|
143
|
-
return {
|
|
144
|
-
close: () => {
|
|
145
|
-
called = true;
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
assert.throws(() => {
|
|
152
|
-
p((v) => {
|
|
153
|
-
throw "error";
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
assert.isFalse(called);
|
|
158
|
-
coroutine.sleep(10);
|
|
159
|
-
assert.isTrue(called);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("default destroy function", () => {
|
|
163
|
-
var called = false;
|
|
164
|
-
|
|
165
|
-
var p = Pool({
|
|
166
|
-
create: () => {
|
|
167
|
-
return {
|
|
168
|
-
destroy: () => {
|
|
169
|
-
called = true;
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
assert.throws(() => {
|
|
176
|
-
p((v) => {
|
|
177
|
-
throw "error";
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
assert.isFalse(called);
|
|
182
|
-
coroutine.sleep(10);
|
|
183
|
-
assert.isTrue(called);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
it("default destroy function, but destroy is not a function, mongodb3.0 case", () => {
|
|
188
|
-
var cnt = 0;
|
|
189
|
-
var p = Pool({
|
|
190
|
-
create: () => {
|
|
191
|
-
return {
|
|
192
|
-
destroy: ++cnt
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
assert.throws(() => {
|
|
198
|
-
p((v) => {
|
|
199
|
-
throw "error";
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
assert.equal(cnt, 1);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it("default dispose function", () => {
|
|
207
|
-
var called = false;
|
|
208
|
-
|
|
209
|
-
var p = Pool({
|
|
210
|
-
create: () => {
|
|
211
|
-
return {
|
|
212
|
-
dispose: () => {
|
|
213
|
-
called = true;
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
assert.throws(() => {
|
|
220
|
-
p((v) => {
|
|
221
|
-
throw "error";
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
assert.isFalse(called);
|
|
226
|
-
coroutine.sleep(10);
|
|
227
|
-
assert.isTrue(called);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it("clean timeout", () => {
|
|
231
|
-
var called = false;
|
|
232
|
-
|
|
233
|
-
var p = Pool({
|
|
234
|
-
create: () => {
|
|
235
|
-
return 100;
|
|
236
|
-
},
|
|
237
|
-
destroy: (o) => {
|
|
238
|
-
called = true;
|
|
239
|
-
},
|
|
240
|
-
timeout: 10
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
p((v) => {});
|
|
244
|
-
|
|
245
|
-
assert.isFalse(called);
|
|
246
|
-
assert.equal(p.connections(), 1);
|
|
247
|
-
coroutine.sleep(100);
|
|
248
|
-
assert.isTrue(called);
|
|
249
|
-
assert.equal(p.connections(), 0);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it("retry", () => {
|
|
253
|
-
var n = 0;
|
|
254
|
-
|
|
255
|
-
var p = Pool(() => {
|
|
256
|
-
n++;
|
|
257
|
-
throw "open error";
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
assert.throws(() => {
|
|
261
|
-
p((v) => {});
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
assert.equal(n, 1);
|
|
265
|
-
|
|
266
|
-
var n1 = 0;
|
|
267
|
-
var p1 = Pool({
|
|
268
|
-
create: () => {
|
|
269
|
-
n1++;
|
|
270
|
-
throw "open error";
|
|
271
|
-
},
|
|
272
|
-
retry: 10
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
assert.throws(() => {
|
|
276
|
-
p1((v) => {});
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
assert.equal(n1, 10);
|
|
280
|
-
|
|
281
|
-
var n2 = 0;
|
|
282
|
-
var p2 = Pool({
|
|
283
|
-
create: () => {
|
|
284
|
-
n2++;
|
|
285
|
-
if (n2 == 3)
|
|
286
|
-
return;
|
|
287
|
-
throw "open error";
|
|
288
|
-
},
|
|
289
|
-
retry: 10
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
p2((v) => {});
|
|
293
|
-
|
|
294
|
-
assert.equal(n2, 3);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
it("long time create", () => {
|
|
298
|
-
var called = 0;
|
|
299
|
-
|
|
300
|
-
var p = Pool({
|
|
301
|
-
create: () => {
|
|
302
|
-
called++;
|
|
303
|
-
if (called == 1)
|
|
304
|
-
coroutine.sleep(20);
|
|
305
|
-
return called;
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
var cs = [];
|
|
310
|
-
coroutine.parallel([0, 1, 2, 3, 4], n => {
|
|
311
|
-
p((c) => {
|
|
312
|
-
coroutine.sleep(1);
|
|
313
|
-
cs[n] = c;
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
assert.deepEqual(cs, [2, 3, 4, 5, 2]);
|
|
318
|
-
assert.equal(p.info().count, 4);
|
|
319
|
-
coroutine.sleep(100);
|
|
320
|
-
assert.equal(p.info().count, 5);
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it("long time fault create", () => {
|
|
324
|
-
var called = 0;
|
|
325
|
-
|
|
326
|
-
var p = Pool({
|
|
327
|
-
create: () => {
|
|
328
|
-
called++;
|
|
329
|
-
if (called == 1) {
|
|
330
|
-
coroutine.sleep(20);
|
|
331
|
-
throw 100;
|
|
332
|
-
}
|
|
333
|
-
return called;
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
var cs = [];
|
|
338
|
-
coroutine.parallel([0, 1, 2, 3, 4], n => {
|
|
339
|
-
p((c) => {
|
|
340
|
-
coroutine.sleep(1);
|
|
341
|
-
cs[n] = c;
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
assert.deepEqual(cs, [2, 3, 4, 5, 2]);
|
|
346
|
-
assert.equal(p.info().count, 4);
|
|
347
|
-
coroutine.sleep(100);
|
|
348
|
-
assert.equal(p.info().count, 4);
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
it("maxsize and create", () => {
|
|
352
|
-
var createCount = 0,
|
|
353
|
-
maxsize = 2;
|
|
354
|
-
|
|
355
|
-
var p = Pool({
|
|
356
|
-
create: () => {
|
|
357
|
-
createCount++;
|
|
358
|
-
return {};
|
|
359
|
-
},
|
|
360
|
-
maxsize: maxsize
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
coroutine.parallel(["a", "b", "c", "d"], n => {
|
|
364
|
-
p((n) => {
|
|
365
|
-
coroutine.sleep(50);
|
|
366
|
-
})
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
assert.equal(createCount, maxsize);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
it("clear", () => {
|
|
373
|
-
function parallel(data) {
|
|
374
|
-
coroutine.parallel(data, function (n) {
|
|
375
|
-
p((n) => {
|
|
376
|
-
coroutine.sleep(50);
|
|
377
|
-
})
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
var maxsize = 3;
|
|
382
|
-
|
|
383
|
-
var p = Pool({
|
|
384
|
-
create: () => {
|
|
385
|
-
return {};
|
|
386
|
-
},
|
|
387
|
-
maxsize: maxsize,
|
|
388
|
-
timeout: 60 * 1000
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
parallel(["a", "b", "c", "d"]);
|
|
392
|
-
|
|
393
|
-
assert.equal(p.info().count, maxsize);
|
|
394
|
-
|
|
395
|
-
p.clear();
|
|
396
|
-
coroutine.sleep(100);
|
|
397
|
-
|
|
398
|
-
assert.equal(p.info().count, 0);
|
|
399
|
-
|
|
400
|
-
parallel(["a"]);
|
|
401
|
-
|
|
402
|
-
assert.equal(p.info().count, 0);
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
it("throw real error", () => {
|
|
406
|
-
var called = false;
|
|
407
|
-
var destroyed = false;
|
|
408
|
-
|
|
409
|
-
var p = Pool({
|
|
410
|
-
create: () => {
|
|
411
|
-
return 100;
|
|
412
|
-
},
|
|
413
|
-
destroy: (o) => {
|
|
414
|
-
destroyed = true;
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
try {
|
|
419
|
-
p((v) => {
|
|
420
|
-
throw "error";
|
|
421
|
-
});
|
|
422
|
-
} catch (error) {
|
|
423
|
-
called = true
|
|
424
|
-
assert.isObject(error)
|
|
425
|
-
assert.equal(error.message, 'error')
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
assert.isTrue(called);
|
|
429
|
-
|
|
430
|
-
assert.isFalse(destroyed);
|
|
431
|
-
coroutine.sleep(10);
|
|
432
|
-
assert.isTrue(destroyed);
|
|
433
|
-
});
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
process.exit(test.run(console.DEBUG));
|
/package/{README.md → Readme.md}
RENAMED
|
File without changes
|