P2P Distributed Workload for NodeJS
To install just use npm
$ npm install partition.io
The api has 3 main components. The Servent, the Worker, and the Job.
The servent acts as the communication layer for the P2P system, the worker is a template that defines work to be done. The job is a representation of work being done by a worker.
Getting going with a distributed system using Partition.IO is easy.
var Servent = require'partition.io'Serventservent =port = 8888;serventon'listening'/*** If we aren't on our default port then we weren't* the first node, so connect to that one*/if serventaddressport !== portserventconnectport;;/*** First bind to our default port, if we can't then use* an ephemeral port instead*/serventon'error'iferrcode === 'EADDRINUSE'serventlisten0;;serventlistenport;
Creating a worker is also easy, just call
be sure to set the last parameter taken as callback, this is how data gets
back to Partition.IO.
/*** Create our worker* params will be passed in directly from your function definition* callback set as the last param*/var worker = serventcreateWorker'random'callbacknull MathfloorMathrandom*max;;/*** This will run the worker (as a job)*/workerdistributevar numbers = ;/*** If all our node don't respond in 5000ms, just get on with what data we* have (useful if accuracy doesn't need to be 100%)*/jobsetTTL5000;/*** Data is called once for each node that responds*/jobon'data'numberspushdata;;/*** Error is emitted if one of the callbacks calls back with error*/jobon'error' console.error;//push the random number on to the datajobon'end'console.lognumbers;;//returns a list of random numbers between 0 and 1000jobrun1000;;
Before I add more features, first I need to write some tests.
If you would like to contribute, just drop me a line or a pull request.
(The MIT License)
Copyright (c) 2011 Russell Bradberry <firstname.lastname@example.org>
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.