-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http: provide keep-alive timeout response header #34561
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ const { | |
ObjectKeys, | ||
ObjectPrototypeHasOwnProperty, | ||
ObjectSetPrototypeOf, | ||
MathFloor, | ||
Symbol, | ||
} = primordials; | ||
|
||
|
@@ -123,6 +124,8 @@ function OutgoingMessage() { | |
this._header = null; | ||
this[kOutHeaders] = null; | ||
|
||
this._keepAliveTimeout = 0; | ||
|
||
this._onPendingData = noopPendingOutput; | ||
} | ||
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype); | ||
|
@@ -424,6 +427,10 @@ function _storeHeader(firstLine, headers) { | |
(state.contLen || this.useChunkedEncodingByDefault || this.agent); | ||
if (shouldSendKeepAlive) { | ||
header += 'Connection: keep-alive\r\n'; | ||
if (this._keepAliveTimeout) { | ||
const timeoutSeconds = MathFloor(this._keepAliveTimeout) / 1000; | ||
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not add the header if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR welcome There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} else { | ||
this._last = true; | ||
header += 'Connection: close\r\n'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
const body = 'hello world\n'; | ||
|
||
res.writeHead(200, { 'Content-Length': body.length }); | ||
res.write(body); | ||
res.end(); | ||
})); | ||
server.keepAliveTimeout = 12000; | ||
|
||
const agent = new http.Agent({ maxSockets: 1, keepAlive: true }); | ||
|
||
server.listen(0, common.mustCall(function() { | ||
http.get({ | ||
path: '/', port: this.address().port, agent: agent | ||
}, common.mustCall((response) => { | ||
response.resume(); | ||
assert.strictEqual( | ||
response.headers['keep-alive'], 'timeout=12'); | ||
server.close(); | ||
agent.destroy(); | ||
})); | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we always set this to less than the actual timeout? And if so what is a good algorithm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based nodejs/undici#279 I feel that any client should always assume a value less than the hint. So further reducing it here can be counter productive.