Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use socket custom freeSocketKeepAliveTimeout first
to support Keep-Alive header on the user land closes #58
- Loading branch information
Showing
5 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ node_js: | |
- '4.3.2' | ||
- '4' | ||
- '6' | ||
- '7' | ||
- '8' | ||
- '9' | ||
install: | ||
- npm i npminstall && npminstall | ||
script: | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
const Agent = require('..'); | ||
|
||
const keepaliveAgent = new Agent({ | ||
keepAlive: true, | ||
}); | ||
const server = http.createServer((req, res) => { | ||
if (server.keepAliveTimeout) { | ||
res.setHeader('Keep-Alive', `time=${parseInt(server.keepAliveTimeout / 1000)}`); | ||
} | ||
res.end('Hello World, ' + req.connection.remotePort); | ||
}); | ||
server.on('clientError', (err, socket) => { | ||
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); | ||
}); | ||
|
||
const port = 3000; | ||
server.listen(port); | ||
server.keepAliveTimeout = 1000; | ||
|
||
let count = 0; | ||
function request() { | ||
count++; | ||
const req = http.request({ | ||
method: 'GET', | ||
port, | ||
path: '/', | ||
agent: keepaliveAgent, | ||
}, res => { | ||
assert(res.statusCode === 200); | ||
const chunks = []; | ||
res.on('data', data => { | ||
chunks.push(data); | ||
}); | ||
res.on('end', () => { | ||
const text = Buffer.concat(chunks).toString(); | ||
console.log('[%s] status: %s, text: %s, headers: %j', count, text, res.statusCode, res.headers); | ||
assert(res.headers.connection === 'keep-alive'); | ||
assert(res.headers['keep-alive'] === 'time=1'); | ||
const m = /^time=(\d+?)/.exec(res.headers['keep-alive']); | ||
if (m) { | ||
const keepAliveTimeout = parseInt(m[1]) * 1000 - 500; | ||
if (keepAliveTimeout > 0) { | ||
res.socket.freeSocketKeepAliveTimeout = keepAliveTimeout; | ||
} | ||
} | ||
if (count > 5) { | ||
process.exit(0); | ||
} | ||
}); | ||
}); | ||
req.on('error', err => { | ||
console.error('[%s] error: %s', count, err); | ||
throw err; | ||
}); | ||
req.end(); | ||
} | ||
|
||
setInterval(request, server.keepAliveTimeout); | ||
request(); |