-
Notifications
You must be signed in to change notification settings - Fork 0
/
lanes.js
63 lines (57 loc) · 1.57 KB
/
lanes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const net = require('net')
const cluster = require('cluster')
const debug = require('util').debuglog('lanes')
module.exports = (laneID = 'default') => {
const joinMsg = `lanes-${laneID}:join`
const connectionMsg = `lanes-${laneID}:connection`
const server = net.createServer({ pauseOnConnect: true }, route)
let workers = []
return { listen, join }
function route(connection) {
const worker = workerForIP(connection.remoteAddress)
if (worker) {
worker.send(connectionMsg, connection)
}
else {
debug('No worker available for connection')
}
}
function listen(...args) {
cluster
.on('message', (worker, msg, handle) => {
if (msg === joinMsg) {
debug(`Worker ${workers.length} joined`)
workers.push(worker)
}
})
.on('disconnect', (worker) => {
const index = workers.indexOf(worker)
if (index != -1) {
debug(`Worker ${index} disconnected`)
workers.splice(index, 1)
}
})
return server.listen(...args)
}
function join(server, done) {
server.listen(0, 'localhost', done)
process.send(joinMsg)
process.on('message', (msg, conn) => {
if (msg !== connectionMsg) return;
server.emit('connection', conn)
conn.resume()
})
}
function workerForIP(ip) {
const ipLen = ip.length
let s = ''
for (var i = 0; i < ipLen; i++) {
if (!isNaN(ip[i])) {
s += ip[i]
}
}
const index = Number(s) % workers.length
debug(`Routing ${ ip } to ${ index }`)
return workers[index]
}
}