bun
bun copied to clipboard
http.request and fetch on keep-alive opens always new socket, even it shouldn't
What version of Bun is running?
1.1.15
What platform is your computer?
Linux 5.15.153.1-microsoft-standard-WSL2 x86_64 x86_64
What steps can reproduce the bug?
import { Agent } from 'http';
import { request } from 'http';
import type { Socket } from "bun";
class test {
agent = new Agent({
keepAlive: true,
maxSockets: 1,
keepAliveMsecs: 3000,
});
sockets: Socket[] = [];
async call(method: string, ...params: any[]) {
const body = "testing";
const outData = body; // await this.compress(body);
let headers: any = {
"Content-Type": "text/xml",
"Content-Encoding": "gzip",
'Connection': 'keep-alive',
'Content-Length': Buffer.byteLength(outData),
};
const res: any = await new Promise((resolve, reject) => {
const req = request({
hostname: 'localhost',
port: 3000,
path: '/',
method: 'POST',
headers: headers,
agent: this.agent,
}, (response) => {
response.setEncoding('utf-8');
let recvData = '';
response.on('data', (chunk) => {
recvData += chunk;
});
response.on('end', () => {
resolve({ data: recvData, headers: response.headers });
});
});
req.on('error', function (e) {
console.log(e);
reject(e);
});
req.on("socket", (socket: any) => {
if (!this.sockets.includes(socket)) {
console.log("new socket created");
this.sockets.push(socket);
socket.on("close", function () {
console.log("socket has been closed");
});
}
});
req.write(outData);
console.log("should keepalive? ", req.shouldKeepAlive);
req.end();
});
return res.data;
}
async test() {
const res = await fetch("http://localhost:3000", {
method: "POST",
body: "test",
headers: {
"Content-Type": "text/xml",
"Content-Encoding": "gzip",
'Connection': "Keep-Alive"
},
keepalive: true,
verbose: true
});
return await res.text();
}
}
Bun.serve({
fetch(req: Request): Response | Promise<Response> {
if (req.method == "POST") {
return new Response("Testing...");
} else {
return new Response("Hello World!");
}
},
port: 3000,
});
const t = new test();
setInterval(async () => {
const data: any = await t.call("test", []);
console.log(data);
// const data2: any = await t.test();
// console.log(data2);
}, 2000);
What is the expected behavior?
should keepalive? true new socket created Testing... should keepalive? true Testing... should keepalive? true Testing...
What do you see instead?
should keepalive? true new socket created Testing... should keepalive? true new socket created Testing... should keepalive? true new socket created Testing...
Additional information
I tried everything, but bun http agent and http stack opens always new socket, and doesn't close the previous... even set to maxsockets = 1 and keepalive....