Created
September 2, 2014 16:17
-
-
Save mhoyer/9010254bf0c31f9eed1b to your computer and use it in GitHub Desktop.
Simple Node.js load balancer
This file contains hidden or 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
var net = require('net'); | |
var globalConnectionCount = 0; | |
module.exports = function(srcPort, dstMaps) { | |
var roundRobinIndex = 0; | |
var balancer = net.createServer(function (inboundSocket) { | |
var cnx = globalConnectionCount++ + ': '; | |
var connected = false; | |
var buffers = new Array(); | |
// new proxy per connection | |
var dstMap = dstMaps[roundRobinIndex++]; | |
var dst = dstMap['host'] + ':' + dstMap['port']; | |
roundRobinIndex = roundRobinIndex % dstMaps.length; | |
console.log(cnx + ' ---> [] ---> ' + dst + ' new'); | |
// create and configure new outbound socket to connect to chosen destination | |
var outboundSocket = new net.Socket(); | |
outboundSocket.connect(parseInt(dstMap['port']), dstMap['host'], function () { | |
connected = true; | |
if (buffers.length > 0) { | |
for (i = 0; i < buffers.length; i++) { | |
outboundSocket.write(buffers[i]); | |
} | |
} | |
}).on("error", function (e) { | |
console.log(cnx + ' ---> [] -X-> ' + dst + ' error: ' + e); | |
inboundSocket.end(); | |
}).on('data', function (data) { | |
inboundSocket.write(data); | |
}).on("close", function(had_error) { | |
console.log(cnx + ' ---> [] |-> ' + dst + ' close (had_error:' + had_error + ')'); | |
inboundSocket.end(); | |
}); | |
// configure inbound socket to connect to chosen destination | |
inboundSocket.on("error", function (e) { | |
console.log(cnx + ' -X-> [] ---> ' + dst + ' error: ' + e); | |
outboundSocket.end(); | |
}).on("data", function (data) { | |
if (connected) { | |
outboundSocket.write(data); | |
} else { | |
buffers[buffers.length] = data; | |
} | |
}).on("close", function(had_error) { | |
console.log(cnx + ' -| [] ---> ' + dst + ' close (had_error:' + had_error + ')'); | |
outboundSocket.end(); | |
outboundSocket.destroy(); | |
}); | |
}); | |
balancer.listen(srcPort); | |
console.log("Listening for TCP connections on:", srcPort); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage
lb.js
:Should be launched with: