Skip to content

Commit

Permalink
http: reuse socket only when it is drained
Browse files Browse the repository at this point in the history
Fix test, make sure old socket is not drained
when we makeing new requests
  • Loading branch information
ywave620 committed Jul 23, 2022
1 parent b3e6ea1 commit d66d758
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions test/parallel/test-http-agent-reuse-drained-socket-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const net = require('net');

const agent = new http.Agent({
keepAlive: true,
maxFreeSockets: 1024
maxFreeSockets: Infinity,
maxSockets: Infinity,
maxTotalSockets: Infinity,
});

const server = net.createServer({
Expand All @@ -30,14 +32,14 @@ function sendFstReq(serverPort) {
res.on('data', noop);
res.on('end', common.mustCall(() => {
// Agent's socket reusing code is registered to process.nextTick(),
// to ensure it take effect, fire in the next event loop
setTimeout(sendSecReq, 10, serverPort, req.socket.localPort);
// and will be run after this function, make sure it take effect.
setImmediate(sendSecReq, serverPort, req.socket.localPort);
}));
});

// Overwhelm the flow control window
// note that tcp over the loopback inteface has a large flow control window
assert.strictEqual(req.write('a'.repeat(6_000_000)), false);
// Overwhelm the flow control window, accroding to TCP standard,
// flow control window is up to 1GB in theory.
assert.strictEqual(req.write(Buffer.alloc(1 + 1024 * 1024 * 1024, 0)), false);

req.end();
}
Expand All @@ -52,7 +54,7 @@ function sendSecReq(serverPort, fstReqCliPort) {
}, (res) => {
res.on('data', noop);
res.on('end', common.mustCall(() => {
setTimeout(sendThrReq, 10, serverPort, req.socket.localPort);
setImmediate(sendThrReq, serverPort, req.socket.localPort);
}));
});

Expand Down

0 comments on commit d66d758

Please sign in to comment.