Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f7a49d7
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,19 @@ | |||
Copyright (C) 2011 by Diogo Resende | |||
|
|||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
## NodeJS Cluster Abstraction Layer | |||
|
|||
This is an abstraction layer based on EventEmitter to provide a | |||
simple way of dealing with cluster support added on v0.6 of NodeJS. | |||
|
|||
## Install | |||
|
|||
npm install rack | |||
|
|||
## Usage | |||
|
|||
var rack = require("rack").create(); | |||
|
|||
rack.on("master-start", function () { | |||
console.log("master starting.."); | |||
}); | |||
rack.on("worker-start", function () { | |||
console.log("worker starting.."); | |||
}); | |||
rack.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
var rack = require("../lib/rack").create(); | |||
|
|||
rack.on("master-start", startMaster); | |||
rack.on("worker-start", startWorker); | |||
rack.start(); | |||
|
|||
function startMaster(rack) { | |||
console.log("[%d] master started", rack.pid); | |||
|
|||
rack.on("worker-message", function (msg, worker) { | |||
console.log("[%d (worker)] -> [%d (master)] %j", worker.pid, rack.pid, msg); | |||
}); | |||
} | |||
|
|||
function startWorker(worker) { | |||
console.log("[%d] worker started", worker.pid); | |||
|
|||
require("http").createServer(function (req, res) { | |||
worker.send({ url: req.url }); | |||
|
|||
console.log("[%d] %s", worker.pid, req.url); | |||
|
|||
res.writeHead(200, { "Content-Type": "text/plain" }); | |||
res.end("Your requested " + req.url); | |||
}).listen(1337); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
var rack = require("../lib/rack").create(); | |||
|
|||
rack.on("master-start", startMaster); | |||
rack.on("worker-start", startWorker); | |||
rack.start(); | |||
|
|||
function startMaster(rack) { | |||
console.log("[%d] master started", rack.pid); | |||
} | |||
|
|||
function startWorker(worker) { | |||
console.log("[%d] worker started", worker.pid); | |||
|
|||
require("http").createServer(function (req, res) { | |||
console.log("[%d] %s", worker.pid, req.url); | |||
|
|||
res.writeHead(200, { "Content-Type": "text/plain" }); | |||
res.end("Your requested " + req.url); | |||
}).listen(1337); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
var util = require("util"), | |||
events = require("events"), | |||
cluster = require("cluster"), | |||
rack = null; | |||
|
|||
module.exports = rack = { | |||
cores: function () { | |||
return require("os").cpus().length; | |||
}, | |||
create: function () { | |||
return new Rack(); | |||
} | |||
} | |||
|
|||
function Rack(cores) { | |||
events.EventEmitter.call(this); | |||
} | |||
util.inherits(Rack, events.EventEmitter); | |||
|
|||
Rack.prototype.start = function (workers) { | |||
workers || (workers = rack.cores()); | |||
|
|||
if (cluster.isMaster) { | |||
Object.defineProperty(this, "pid", { | |||
value: process.pid, | |||
writable: false | |||
}); | |||
this.emit("master-start", this); | |||
|
|||
cluster.on("death", (function (rack) { | |||
return function (worker) { | |||
rack.emit("worker-end", worker, rack); | |||
} | |||
})(this)); | |||
|
|||
for (var i = 0; i < workers; i++) { | |||
this.worker(); | |||
} | |||
} else { | |||
this.emit("worker-start", process); | |||
} | |||
}; | |||
Rack.prototype.worker = function () { | |||
var worker = cluster.fork(); | |||
|
|||
worker.on("message", (function (rack) { | |||
return function (msg) { | |||
if (msg.hasOwnProperty("_queryId")) { | |||
// ignore core messages | |||
return; | |||
} | |||
rack.emit("worker-message", msg, worker, rack); | |||
} | |||
})(this)); | |||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
{ | |||
"name": "rack", | |||
"version": "0.1.0", | |||
"description": "NodeJS Cluster Abstraction Layer", | |||
"keywords": [ "cluster" ], | |||
"author": "Diogo Resende <dresende@thinkdigital.pt>", | |||
"engines": { "node": ">= 0.6.0" }, | |||
"repository": { | |||
"type": "git", | |||
"url": "http://github.com/dresende/node-rack.git" | |||
}, | |||
"directories": { | |||
"lib": "./lib" | |||
}, | |||
"main": "./lib/index", | |||
"licenses": [{ | |||
"type": "MIT", | |||
"url": "http://github.com/dresende/node-rack/raw/master/LICENSE" | |||
}] | |||
} |