{"name":"bagpipe","version":"0.2.0","description":"bagpipe.js","keywords":["limiter","concurrency limit","parallel limit"],"main":"index.js","scripts":{"test":"mocha -R spec"},"devDependencies":{"mocha":"*","should":"*"},"author":{"name":"Jackson Tian"},"license":"MIT","readme":"Bagpipe [中文](https://github.com/JacksonTian/bagpipe/blob/master/README_CN.md)\n=======\nYou are the bagpiper.\n\n## Introduction\nIt is convenient for us to use asynchronism or concurrent to promote our business speed in Node. While, if the amount of concurrent is too large, our server may not support, such that we need to limit the amount of concurrent. Though, the http module contains [http.Agent](http://nodejs.org/docs/latest/api/http.html#http_class_http_agent) to control the amount of sockets, usually, our asynchronous API has packaged in advance. It is not realistic to change the inner API agent, let’s realize it on our own logical layer.\n\n## Installation\n```\nnpm install bagpipe\n```\n\n## API\nThe APIs exposed by Bagpipe only include constructor and instance methods `push`.\n\nUnder original status, we may execute concurrent like this, forming 100 concurrent asynchronous invokes.\n\n```\nfor (var i = 0; i < 100; i++) {\n  async(function () {\n    // Asynchronous call\n  });\n}\n```\nIf need to limit concurrency, what is your solution?\nSolution from Bagpipe as follows:\n\n```\nvar Bagpipe = require('bagpipe');\n// Sets the max concurrency as 100\nvar bagpipe = new Bagpipe(10);\nfor (var i = 0; i < 100; i++) {\n  bagpipe.push(async, function () {\n    // execute asynchronous callback\n  });\n}\n```\n\nYes, invoke method only splits method、parameter and callback, then delivery it to bagpipe through `push`.\n\nHow does it like compared with your anticipated solution?\n\n## Principles\nBagpipe delivers invoke into inner queue through `push`. If active invoke amount is less than max concurrent, it will be popped and executed directly, or it will stay in the queue. When an asynchronous invoke ends, a invoke in the head of the queue will be popped and executed, such that assures active asynchronous invoke amount no larger than restricted value.\n\nWhen the queue length is larger than 100 or twice larger than max concurrent number, Bagpipe object will fire its `full` event, which delivers the queue length value. The value helps to assess business performance. For example:\n\n```\nbagpipe.on('full', function (length) {\n  console.warn('Button system cannot deal on time, queue jam, current queue length is:’+ length);\n});\n```\n\n## Module status\nThe unit testing status: [![Build Status](https://secure.travis-ci.org/JacksonTian/bagpipe.png)](http://travis-ci.org/JacksonTian/bagpipe). Unit test coverage [100%](http://html5ify.com/bagpipe/coverage.html).\n\n## Best Practices\n- Ensure that the last parameter of the asynchronous invoke is callback.\n- Listen to the `full` event, adding your business performance assessment.\n- Current asynchronous method has not supported context yet. Ensure that there is no `this` reference in asynchronous method. If there is `this` reference in asynchronous method, please use `bind` pass into correct context.\n- Asynchronous invoke should process method to deal with timeout, it should ensure the invoke will return in a certain time no matter whether the business has been finished or not.\n\n## Real case\nWhen you want to traverse file directories, asynchrony can ensure `full` use of IO. You can invoke thousands of file reading easily. But, system file descriptions are limited. If disobedient, read this article again when occurring errors as follows.\n\n```\nError: EMFILE, too many open files\n```\n\nSomeone may consider dealing it with synchronous method. But, when synchronous, CPU and IO cannot be used concurrently, performance is an indefeasible index under certain condition. You can enjoy concurrent easily, as well as limit concurrent with Bagpipe.\n\n```\nvar bagpipe = new Bagpipe(10);\n\nvar files = ['Here are many files'];\nfor (var i = 0; i < files.length; i++) {\n  // fs.readFile(files[i], 'utf-8', function (err, data) {\n  bagpipe.push(fs.readFile, files[i], 'utf-8', function (err, data) {\n    // won’t occur error because of too many file descriptions\n    // well done\n  });\n}\n```\n\n## License\nReleased under the license of [MIT](https://github.com/JacksonTian/bagpipe/blob/master/MIT-License), welcome to enjoy open source.\n","readmeFilename":"README.md","_id":"bagpipe@0.2.0","dist":{"shasum":"10a28627ba7a33a213dc242a30fed84696ce1296","tarball":"http://registry-npm.ywwl.com/bagpipe/download/bagpipe-0.2.0.tgz","integrity":"sha512-m8qiI8TIa98n+LsMOEcgYdp47XLnzKcBjCbU8zDKEo0w+3irrNnqw8DI6SMvLMywl1xjxCEM7hkUcSV05Pg6UQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDRemrETR6i086qoMy4sSz3yiCsKd1r3bn7nclJl+XX/wIgHTwsxgRndRGhu75CfKDSzmUVAa+XijdTEpGkMea1S68="}],"size":6515,"noattachment":false,"key":"/bagpipe/-/bagpipe-0.2.0.tgz"},"_from":".","_npmVersion":"1.2.2","_npmUser":{"name":"jacksontian","email":"shyvo1987@gmail.com"},"maintainers":[{"name":"jacksontian","email":"shyvo1987@gmail.com"}],"directories":{},"publish_time":1362772866733,"_cnpm_publish_time":1362772866733,"dist-tags":{"latest":"0.3.5"}}