-
Notifications
You must be signed in to change notification settings - Fork 57
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 (#59)
- Loading branch information
1 parent
138eda8
commit bc7cadb
Showing
5 changed files
with
85 additions
and
8 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,73 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
const Agent = require('..'); | ||
|
||
describe('test/server_timeout.test.js', () => { | ||
let port; | ||
let server; | ||
before(done => { | ||
server = http.createServer((req, res) => { | ||
if (server.keepAliveTimeout) { | ||
res.setHeader('Keep-Alive', `timeout=${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'); | ||
}); | ||
server.keepAliveTimeout = 1000; | ||
server.listen(0, err => { | ||
port = server.address().port; | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should handle Keep-Alive header and not throw reset error', done => { | ||
const keepaliveAgent = new Agent({ | ||
keepAlive: true, | ||
}); | ||
|
||
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'] === 'timeout=1'); | ||
const m = /^timeout=(\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) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
req.on('error', err => { | ||
console.error('[%s] error: %s', count, err); | ||
done(err); | ||
}); | ||
req.end(); | ||
} | ||
|
||
setInterval(request, server.keepAliveTimeout); | ||
request(); | ||
}); | ||
}); |