Skip to content
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

fix: only set keep-alive header before Node.js 14.8.0 #4457

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/middleware/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

'use strict';

const semver = require('semver');

module.exports = options => {
// Node.js >=14.8.0 will set Keep-Alive Header, see https://github.com/nodejs/node/pull/34561
const shouldPatchKeepAliveHeader = semver.lt(process.version, '14.8.0');

return async function meta(ctx, next) {
if (options.logging) {
ctx.coreLogger.info('[meta] request started, host: %s, user-agent: %s', ctx.host, ctx.header['user-agent']);
Expand All @@ -13,9 +18,9 @@ module.exports = options => {
// total response time header
ctx.set('x-readtime', Date.now() - ctx.starttime);

// try to support Keep-Alive Header
// try to support Keep-Alive Header when < 14.8.0
const server = ctx.app.server;
if (server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
if (shouldPatchKeepAliveHeader && server && server.keepAliveTimeout && server.keepAliveTimeout >= 1000 && ctx.header.connection !== 'close') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单测是否有?加上单测确保只会设置一次 header

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

atian25 marked this conversation as resolved.
Show resolved Hide resolved
/**
* use Math.floor instead of parseInt. More: https://github.com/eggjs/egg/pull/2702
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ms": "^2.1.1",
"mz": "^2.7.0",
"on-finished": "^2.3.0",
"semver": "^7.3.2",
"sendmessage": "^1.1.0",
"urllib": "^2.33.0",
"utility": "^1.15.0",
Expand Down