respool

respool

Usage no npm install needed!

<script type="module">
  import respool from 'https://cdn.skypack.dev/respool';
</script>

README

ResPool

Instance pooling and load-balancing.

var _ 			= require('underscore');
var respool 	= require('./respool');

var childClass	= function(callback) {
    this.count	= 0;
    setTimeout(function() {
        callback();
    }, _.random(200,3000));
};
childClass.prototype.hit = function () {
    this.count++;
    return this.count;
};



var demo	= function(n) {
    var scope	= this;
    this.n		= n;
    this.spool	= new respool({
        async:		false,
        progress:	'Creating...',
        count:		10,
        min:		2,
        create:		function(done) {
            var instance;
            instance = new childClass(function() {
                console.log("> created");
                done(instance);
            });
        }
    });
    this.spool.init(function() {
        console.log("Spool.init()");
        scope.start();
        scope.output();
    });
    
}
demo.prototype.start = function() {
    var i;
    console.log("Start", this.n);
    for (i=0;i<this.n;i++) {
        this.spool.instance().hit();
    }
}
demo.prototype.output = function() {
    _.each(this.spool.counts, function(count, n) {
        console.log("Instance #"+n, count);
    });
}


var test	= new demo(100);