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

Support Keep-Alive Header #58

Closed
fengmk2 opened this issue Feb 26, 2018 · 0 comments · Fixed by #59
Closed

Support Keep-Alive Header #58

fengmk2 opened this issue Feb 26, 2018 · 0 comments · Fixed by #59
Assignees

Comments

@fengmk2
Copy link
Member

fengmk2 commented Feb 26, 2018

Since http server has a default KeepAliveTimeout = 5s, we should try to support Keep-Alive Header to avoid the timeout condition confuse.

Repeat the root case code

const http = require('http');
const urllib = require('urllib');

const keepaliveAgent = new http.Agent({
  keepAlive: true,
});
const server = http.createServer((req, res) => {
  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.listen(3000);

function request() {
  urllib.request('http://127.0.0.1:3000', {
    agent: keepaliveAgent,
    dataType: 'text',
  }, (err, data, res) => {
    if (err) {
      console.error('error: %s, status: %s', err, res.status, res);
      throw err;
    }
    console.log('status: %s, data: %s', res.status, data, res);
  });
}

setInterval(request, 5000);
request();

It will throw the ECONNRESET error after 2~4 requests.

status: 200, data: Hello World, 53299 { status: 200,
  statusCode: 200,
  headers: 
   { date: 'Mon, 26 Feb 2018 09:32:43 GMT',
     connection: 'keep-alive',
     'content-length': '18' },
  size: 18,
  aborted: false,
  rt: 2,
  keepAliveSocket: true,
  data: 'Hello World, 53299',
  requestUrls: [ 'http://127.0.0.1:3000/' ],
  timing: null,
  remoteAddress: '127.0.0.1',
  remotePort: 3000 }
error: ResponseError: read ECONNRESET (req "error"), GET http://127.0.0.1:3000 -1 (connected: true, keepalive socket: true)
headers: {}, status: -1 { status: -1,
  statusCode: -1,
  headers: {},
  size: 0,
  aborted: false,
  rt: 2,
  keepAliveSocket: true,
  data: undefined,
  requestUrls: [ 'http://127.0.0.1:3000/' ],
  timing: null,
  remoteAddress: '127.0.0.1',
  remotePort: 3000 }
xxx/server_timeout.js:25
      throw err;
      ^

Error: read ECONNRESET
    at _errnoException (util.js:1022:11)
    at TCP.onread (net.js:615:25)
@fengmk2 fengmk2 self-assigned this Feb 26, 2018
fengmk2 added a commit that referenced this issue Feb 27, 2018
to support Keep-Alive header on the user land

closes #58
fengmk2 added a commit that referenced this issue Feb 27, 2018
to support Keep-Alive header on the user land

closes #58
fengmk2 added a commit to node-modules/urllib that referenced this issue Feb 27, 2018
fengmk2 added a commit to node-modules/urllib that referenced this issue Feb 28, 2018
fengmk2 added a commit to eggjs/egg that referenced this issue Feb 28, 2018
fengmk2 added a commit to eggjs/egg that referenced this issue Feb 28, 2018
popomore pushed a commit to eggjs/egg that referenced this issue Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant