node master&
node test&
# wait ~7 seconds
pkill -SIGINT -x test-server
Last active
March 3, 2016 21:48
-
-
Save elliotttf/beec8a0351b784f52d4a to your computer and use it in GitHub Desktop.
socket stuff
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
'use strict'; | |
const adios = require('adios'); | |
const cluster = require('cluster'); | |
if (cluster.isMaster) { | |
process.title = 'test-server'; | |
adios.master.init('/tmp/test.sock') | |
.then(() => { | |
cluster.fork(); | |
}); | |
} | |
else { | |
const server = require('./test-server-express'); | |
adios.child.init(() => new Promise(resolve => { | |
server.close(() => { | |
console.log('closed'); | |
resolve(); | |
}); | |
}), '/tmp/test.sock'); | |
} |
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
{ | |
"name": "socket-stuff", | |
"version": "1.0.0", | |
"description": "", | |
"main": "master.js", | |
"dependencies": { | |
"adios": "^1.1.2", | |
"express": "^4.13.4" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Elliott Foster <[email protected]> (http://codebrews.com/)", | |
"license": "MIT" | |
} |
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
'use strict'; | |
const express = require('express'); | |
const app = express(); | |
app.use((req, res, next) => { | |
req.socket.ref(); | |
res.on('finish', () => req.socket.unref()); | |
next(); | |
}); | |
app.get('/', (req, res) => { | |
res.status(204).end(); | |
}); | |
module.exports = app.listen(3000, () => { | |
console.log('listening'); | |
}); |
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
'use strict'; | |
const http = require('http'); | |
const agent = new http.Agent({keepAlive: true}); | |
new Promise(resolve => { | |
http.get({ | |
hostname: '127.0.0.1', | |
port: 3000, | |
path: '/v3.4/videos', | |
agent: agent | |
}, res => { | |
res.on('end', () => { | |
console.log('req done'); | |
resolve(); | |
}); | |
res.resume(); | |
}).on('error', err => console.log(err)); | |
}).then(() => { | |
console.log('time keeps on ticking...'); | |
agent.freeSockets['127.0.0.1:3000:'][0].on('timeout', () => { | |
console.log('socket timed out!'); | |
process.exit(0); | |
}); | |
setInterval(() => console.log(`${Object.keys(agent.freeSockets).length} sockets`), 1000); | |
}); | |
setTimeout(() => { | |
http.get({ | |
hostname: '127.0.0.1', | |
port: 3000, | |
path: '/v3.4/videos', | |
agent: agent | |
}, res => { | |
console.log(res.statusCode); | |
res.on('end', () => { | |
console.log('req2 done'); | |
}); | |
res.resume(); | |
}).on('error', err => console.log(err)); | |
}, 5000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment