sea

1.0.2 • Public • Published

Sea

An object pool for node.js designed for ease of use and resiliency. Unlike many object pooling libraries, Sea has a bounded free list size while still keeping track of the count of outstanding objects. So, if you miss a free() in your code, the objects will be garbage collected as normal.

Example

var ObjectPool = require('sea'); // singleton
 
function Widget() {
    this.foo = null;
    this.bar = null;
}
 
// `clear` is required to be implemented on your type
Widget.prototype.clear = function clear() {
    this.foo = null;
    this.bar = null;
}
 
// `reset` is required to be implemented on your type
Widget.prototype.reset = function reset(foo, bar) {
    this.foo = foo;
    this.bar = bar;
}
 
ObjectPool.setup({
    Type: Widget,
    maxSize: 1000
});
 
function main(argv) {
    ObjectPool.bootstrap({
        statReceiver: function statReceiver(type, name, value, tags) {
            // type: 'gauge'
            // name: 'object-pool.free' or 'object-pool.outstanding'
            // value: int
            // tags: {name: 'poolname'}
            // do something with stat, like:
            statsd[type](name + '.' + tags.name, value);
        },
 
        reportInterval: 5000, // stat reporting interval
        timers: require('timers'), // or other timers object
 
        // true will enable the outstanding list to keep track of instances
        // that aren't getting freed
        debug: false
    });
 
    var w = Widget.alloc();
    w.reset('some', 'values');
 
    // do things with w
 
    w.free();
 
    ObjectPool.unref();
}
 
if (require.main === module) {
    main(process.argv);
}
 

License

MIT.

Dependencies (0)

    Dev Dependencies (6)

    Package Sidebar

    Install

    npm i sea

    Weekly Downloads

    4

    Version

    1.0.2

    License

    MIT

    Last publish

    Collaborators

    • russfrank