Created
September 13, 2012 19:55
-
-
Save JosephMoniz/3717140 to your computer and use it in GitHub Desktop.
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
net = require "net" | |
hashring = require "hashring" | |
zkplus = require "zkplus" | |
bouncy = require "bouncy" | |
{_} = require "UnderscoreKit" | |
ring = new hashring() | |
client = zkplus.createClient | |
servers: [{ | |
host: "localhost" | |
port: 2181 | |
}] | |
timeout: 500 | |
updateNodes = (nodes) -> | |
ring.addServer node, 2 for node in _.difference nodes, ring.nodes | |
ring.removeServer node for node in _.difference ring.nodes, nodes | |
console.log ring.nodes | |
client.on "connect", () -> | |
client.readdir "/warpcore", (error, nodes) -> | |
if error then process.exit() | |
updateNodes nodes | |
client.watch "/warpcore", method: "list", (error, listener) -> | |
if error then process.exit() | |
listener.on "error", () -> process.exit() | |
listener.on "children", (nodes) -> | |
updateNodes nodes | |
bouncer = bouncy (req, bounce) -> | |
node = ring.getNode req.url | |
[host, port] = node.split ":" | |
stream = net.createConnection { host: host, port: port } | |
bounce stream | |
bouncer.listen 80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment