/
index.html
116 lines (86 loc) · 4.58 KB
/
index.html
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<h1 id="poolrjs">poolr.js</h1>
<p>Small but usefull resource pooling/limiting/throttling library for node.js.</p>
<p>Poolr runs asyncronous tasks in parallel until a configured limit is
reached and then switches to serial execution.
This helps maximizing the use of limited resources like filedescriptors
or remote (database-) connections.</p>
<p>poolr tries to be small and intuitive to use. For a much more advanced
implementation of resource pools take a look at
<a href="https://github.com/coopernurse/node-pool">node-pool</a></p>
<p>You can add any function you like to a pool:</p>
<p><code>staticFunc(foo, bar, callback)</code> becomes:
<code>thisPool.addTask(staticFunc, foo, bar, callback);</code></p>
<p>For object methods you can either pass the object as a default context as a
second argument to the constructor of poolr, or use function binding:</p>
<pre><code>var someObj = new someClass();
var thisPool = new poolr(10, someObj);
thisPool.addTask(someObj.someMethod, arg1, argN, callback);
</code></pre>
<p>or:</p>
<pre><code>var someObj = new someClass();
var thisPool = new poolr(10);
thisPool.addTask(someObj.someMethod.bind(someObj), arg1, argN, callback);
</code></pre>
<h2 id="synopsis">Synopsis</h2>
<p>A basic example limiting cradle connections to two at a time:</p>
<pre><code>var cradle = require('cradle'),
poolr = require('poolr');
cradle.setup({host: 'localhost', port: 5984});
var conn = new (cradle.Connection)(),
db = conn.database('test'),
couchPool = new poolr(2, db);
// all cradle functions are called via the pool:
couchPool.addTask(db.exists, function(err, result) {
if (err) throw err;
console.log(result + ' exists');
}
});
couchPool.addTask(db.save, {foo:'bar'}, function(err, res) {
if (err) throw err;
console.log(res);
});
// ...and so on.
// Add more as fast as you like. Only two functions will be run at a time
</code></pre>
<h2 id="poolr">poolr</h2>
<p><code>new poolr()</code> ceates a new reource pool. The constructor takes one integer
argument representing the maximum number of tasks allowed for using this
resource in parallel.
If you want to support a default context for binding functions to, you can
supply it as a second argument.</p>
<p><code>new poolr(5);</code> creates a resource pool for 5 parallel exections
<code>new poolr(5, someObj);</code> creates a resource pool for 5 parallel exections
with <code>someObj</code> as <code>this</code> context for tasks.</p>
<h2 id="addtask">addTask()</h2>
<p>add a task to the resource pool. Takes the function to be called as the
first argument. Later arguments are passed to the function.
The last argument is expected to be a callback to pass back the results.
If the function is an object method and needs its object context, you
should either supply the object as a second argument to the poolr constructor
or bind the method to its object:</p>
<pre><code>var obj = new someClass();
myPool = new poolr(10, obj);
addTask(staticFunction, arg1, argn, callback);
addTask(someObj.someFunction.bind(someObj), arg1, argn, callback);
myPool.addTask(obj.someFunction, arg1, argn, callback);
</code></pre>
<p>The last two lines are equivalent.</p>
<h2 id="license">License</h2>
<p>(The MIT License)</p>
<p>Copyright (c) 2011 Bernhard Weisshuhn <a href="mailto:bkw@codingforce.com">bkw@codingforce.com</a></p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>