A simple bit of code to manage the concurrency of a function. Very useful in limiting the number of asynch calls in a loop.
Guvna allows for easily managing the number of times a function is run concurrently.
I ran into a problem where I neede to execute a resource intensive function iteratively and quickly killed the box.
To address the problem, I wrote the Guvna to govern the volume and execution of this function.
this.percDone metric to make it easy to show the percentage done in the callback.
Since Guvna now uses integer variables to signal when your callback has completed a run, you MUST include
this.next(); in your function, though if you're running an asynch function, you'll likely have to create a reference to
this as seen in the example below that gets tucked into the asynch function. In the code below, I call create
self so that I can get to the
In this example, I use
setTimeout to simulate an asynch call.
var response =Guvna = require'guvna';var guv =max: 2list: name: 'blargh' name: 'honk' name: 'toot'var rand = MathfloorMathrandom*10+1 * 1000self = this;setTimeoutresponsepushlname+'-'+rand;console.logparseIntselfpercDone*100+'% complete';selfnext; // CHANGE FROM v0.0.1rand;console.logresponse;;guvstart;
When declaring your
guv variable, you need to pass it several options:
list: This is a simple array. Each item in the array is passed to the
callbackfunction one at a time.
callback: This is the function that you want to govern. It is passed each item from the
done: This is the function to run when all your
callbacks are complete.
max: (optional) This is an integer for how many concurrent
callbacks you want to have running. Default =
- 0.0.5 - Added
- 0.0.4 - Added error handling.
- 0.0.3 - Tweaked calculation of
maxif it's omitted.
- 0.0.2 - Refactored to use tracking variables as using events created unnecessary comnplexity especially when nesting guvnas.
- 0.0.1 - Initial drop, used events to manage concurrency.