-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(net/tls) fix backpressure pause on socket (#15543)
- Loading branch information
1 parent
da3d64b
commit a2e2d11
Showing
4 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import net from "net"; | ||
import tls from "tls"; | ||
import { once } from "events"; | ||
import { tls as certs } from "harness"; | ||
import { test, expect } from "bun:test"; | ||
|
||
test("should be able to upgrade a paused socket and also have backpressure on it #15438", async () => { | ||
// enought to trigger backpressure | ||
const payload = Buffer.alloc(16 * 1024 * 4, "b").toString("utf8"); | ||
|
||
const server = tls.createServer(certs, socket => { | ||
// echo | ||
socket.on("data", data => { | ||
socket.write(data); | ||
}); | ||
}); | ||
|
||
await once(server.listen(0, "127.0.0.1"), "listening"); | ||
|
||
const socket = net.connect({ | ||
port: (server.address() as net.AddressInfo).port, | ||
host: "127.0.0.1", | ||
}); | ||
await once(socket, "connect"); | ||
|
||
// pause raw socket | ||
socket.pause(); | ||
|
||
const tlsSocket = tls.connect({ | ||
ca: certs.cert, | ||
servername: "localhost", | ||
socket, | ||
}); | ||
await once(tlsSocket, "secureConnect"); | ||
|
||
// do http request using tls socket | ||
async function doWrite(socket: net.Socket) { | ||
let downloadedBody = 0; | ||
const { promise, resolve, reject } = Promise.withResolvers(); | ||
function onData(data: Buffer) { | ||
downloadedBody += data.byteLength; | ||
if (downloadedBody === payload.length * 2) { | ||
resolve(); | ||
} | ||
} | ||
socket.pause(); | ||
socket.write(payload); | ||
socket.write(payload, () => { | ||
socket.on("data", onData); | ||
socket.resume(); | ||
}); | ||
|
||
await promise; | ||
socket.off("data", onData); | ||
} | ||
for (let i = 0; i < 100; i++) { | ||
// upgrade the tlsSocket | ||
await doWrite(tlsSocket); | ||
} | ||
|
||
expect().pass(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters