Skip to content

Instantly share code, notes, and snippets.

@JosephMoniz
Created September 13, 2012 19:55
Show Gist options
  • Save JosephMoniz/3717140 to your computer and use it in GitHub Desktop.
Save JosephMoniz/3717140 to your computer and use it in GitHub Desktop.
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