diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/dts/lib.deno.unstable.d.ts | 16 | ||||
-rw-r--r-- | cli/tests/unit/net_test.ts | 45 | ||||
-rw-r--r-- | cli/tests/unit/tls_test.ts | 66 |
3 files changed, 127 insertions, 0 deletions
diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index cde76d4d8..2bcba88da 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -1158,6 +1158,22 @@ declare namespace Deno { [Symbol.asyncIterator](): AsyncIterableIterator<[Uint8Array, Addr]>; } + /** + * @category Network + */ + export interface TcpListenOptions extends ListenOptions { + /** When `true` the SO_REUSEPORT flag will be set on the listener. This + * allows multiple processes to listen on the same address and port. + * + * On Linux this will cause the kernel to distribute incoming connections + * across the different processes that are listening on the same address and + * port. + * + * This flag is only supported on Linux. It is silently ignored on other + * platforms. Defaults to `false`. */ + reusePort?: boolean; + } + /** **UNSTABLE**: New API, yet to be vetted. * * Unstable options which can be set when opening a Unix listener via diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts index d2beb5566..8d004f424 100644 --- a/cli/tests/unit/net_test.ts +++ b/cli/tests/unit/net_test.ts @@ -1005,3 +1005,48 @@ Deno.test( } }, ); + +Deno.test({ + ignore: Deno.build.os !== "linux", + permissions: { net: true }, +}, async function netTcpListenReusePort() { + const port = 4003; + const listener1 = Deno.listen({ port, reusePort: true }); + const listener2 = Deno.listen({ port, reusePort: true }); + let p1; + let p2; + let listener1Recv = false; + let listener2Recv = false; + while (!listener1Recv || !listener2Recv) { + if (!p1) { + p1 = listener1.accept().then((conn) => { + conn.close(); + listener1Recv = true; + p1 = undefined; + }).catch(() => {}); + } + if (!p2) { + p2 = listener2.accept().then((conn) => { + conn.close(); + listener2Recv = true; + p2 = undefined; + }).catch(() => {}); + } + const conn = await Deno.connect({ port }); + conn.close(); + await Promise.race([p1, p2]); + } + listener1.close(); + listener2.close(); +}); + +Deno.test({ + ignore: Deno.build.os === "linux", + permissions: { net: true }, +}, function netTcpListenReusePortDoesNothing() { + const listener1 = Deno.listen({ port: 4003, reusePort: true }); + assertThrows(() => { + Deno.listen({ port: 4003, reusePort: true }); + }, Deno.errors.AddrInUse); + listener1.close(); +}); diff --git a/cli/tests/unit/tls_test.ts b/cli/tests/unit/tls_test.ts index ee7636954..d3ef4d0bf 100644 --- a/cli/tests/unit/tls_test.ts +++ b/cli/tests/unit/tls_test.ts @@ -1410,3 +1410,69 @@ Deno.test( listener2.close(); }, ); + +Deno.test({ + ignore: Deno.build.os !== "linux", + permissions: { net: true }, +}, async function listenTlsReusePort() { + const hostname = "localhost"; + const port = 4003; + const listener1 = Deno.listenTls({ + hostname, + port, + cert, + key, + reusePort: true, + }); + const listener2 = Deno.listenTls({ + hostname, + port, + cert, + key, + reusePort: true, + }); + let p1; + let p2; + let listener1Recv = false; + let listener2Recv = false; + while (!listener1Recv || !listener2Recv) { + if (!p1) { + p1 = listener1.accept().then((conn) => { + conn.close(); + listener1Recv = true; + p1 = undefined; + }).catch(() => {}); + } + if (!p2) { + p2 = listener2.accept().then((conn) => { + conn.close(); + listener2Recv = true; + p2 = undefined; + }).catch(() => {}); + } + const conn = await Deno.connectTls({ hostname, port, caCerts }); + conn.close(); + await Promise.race([p1, p2]); + } + listener1.close(); + listener2.close(); +}); + +Deno.test({ + ignore: Deno.build.os === "linux", + permissions: { net: true }, +}, function listenTlsReusePortDoesNothing() { + const hostname = "localhost"; + const port = 4003; + const listener1 = Deno.listenTls({ + hostname, + port, + cert, + key, + reusePort: true, + }); + assertThrows(() => { + Deno.listenTls({ hostname, port, cert, key, reusePort: true }); + }, Deno.errors.AddrInUse); + listener1.close(); +}); |