diff options
| author | Luca Casonato <hello@lcas.dev> | 2022-10-26 21:04:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-26 19:04:27 +0000 |
| commit | f4f1f4f0b64030b744cfb43693af321ea8332bf4 (patch) | |
| tree | 9d4e1fcf9ea9b92ef5a863fcd449edf8c0c9a834 /ext/net | |
| parent | de580cedd24be22dc267d5b92538364ed9998a46 (diff) | |
feat(ext/net): reusePort for TCP on Linux (#16398)
Diffstat (limited to 'ext/net')
| -rw-r--r-- | ext/net/01_net.js | 2 | ||||
| -rw-r--r-- | ext/net/02_tls.js | 3 | ||||
| -rw-r--r-- | ext/net/lib.deno_net.d.ts | 9 | ||||
| -rw-r--r-- | ext/net/ops.rs | 8 | ||||
| -rw-r--r-- | ext/net/ops_tls.rs | 9 |
5 files changed, 27 insertions, 4 deletions
diff --git a/ext/net/01_net.js b/ext/net/01_net.js index acd8ee179..765b94035 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -302,7 +302,7 @@ const [rid, addr] = ops.op_net_listen_tcp({ hostname: args.hostname ?? "0.0.0.0", port: args.port, - }); + }, args.reusePort); addr.transport = "tcp"; return new Listener(rid, addr); } diff --git a/ext/net/02_tls.js b/ext/net/02_tls.js index d3f906dbd..89913d1af 100644 --- a/ext/net/02_tls.js +++ b/ext/net/02_tls.js @@ -65,13 +65,14 @@ hostname = "0.0.0.0", transport = "tcp", alpnProtocols = undefined, + reusePort = false, }) { if (transport !== "tcp") { throw new TypeError(`Unsupported transport: '${transport}'`); } const [rid, localAddr] = ops.op_net_listen_tls( { hostname, port }, - { cert, certFile, key, keyFile, alpnProtocols }, + { cert, certFile, key, keyFile, alpnProtocols, reusePort }, ); return new TlsListener(rid, localAddr); } diff --git a/ext/net/lib.deno_net.d.ts b/ext/net/lib.deno_net.d.ts index df569f93a..1941c0426 100644 --- a/ext/net/lib.deno_net.d.ts +++ b/ext/net/lib.deno_net.d.ts @@ -91,6 +91,11 @@ declare namespace Deno { hostname?: string; } + /** @category Network */ + // deno-lint-ignore no-empty-interface + export interface TcpListenOptions extends ListenOptions { + } + /** Listen announces on the local transport address. * * ```ts @@ -106,11 +111,11 @@ declare namespace Deno { * @category Network */ export function listen( - options: ListenOptions & { transport?: "tcp" }, + options: TcpListenOptions & { transport?: "tcp" }, ): Listener; /** @category Network */ - export interface ListenTlsOptions extends ListenOptions { + export interface ListenTlsOptions extends TcpListenOptions { /** Server private key in PEM format */ key?: string; /** Cert chain in PEM format */ diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 9a6d95586..e6420bf9e 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -246,10 +246,14 @@ impl Resource for UdpSocketResource { fn op_net_listen_tcp<NP>( state: &mut OpState, addr: IpAddr, + reuse_port: bool, ) -> Result<(ResourceId, IpAddr), AnyError> where NP: NetPermissions + 'static, { + if reuse_port { + super::check_unstable(state, "Deno.listen({ reusePort: true })"); + } state .borrow_mut::<NP>() .check_net(&(&addr.hostname, Some(addr.port)), "Deno.listen()")?; @@ -264,6 +268,10 @@ where let socket = Socket::new(domain, Type::STREAM, None)?; #[cfg(not(windows))] socket.set_reuse_address(true)?; + if reuse_port { + #[cfg(target_os = "linux")] + socket.set_reuse_port(true)?; + } let socket_addr = socket2::SockAddr::from(addr); socket.bind(&socket_addr)?; socket.listen(128)?; diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs index b27426894..1e2b54533 100644 --- a/ext/net/ops_tls.rs +++ b/ext/net/ops_tls.rs @@ -990,6 +990,7 @@ pub struct ListenTlsArgs { // TODO(kt3k): Remove this option at v2.0. key_file: Option<String>, alpn_protocols: Option<Vec<String>>, + reuse_port: bool, } #[op] @@ -1001,6 +1002,10 @@ pub fn op_net_listen_tls<NP>( where NP: NetPermissions + 'static, { + if args.reuse_port { + super::check_unstable(state, "Deno.listenTls({ reusePort: true })"); + } + let cert_file = args.cert_file.as_deref(); let key_file = args.key_file.as_deref(); let cert = args.cert.as_deref(); @@ -1061,6 +1066,10 @@ where let socket = Socket::new(domain, Type::STREAM, None)?; #[cfg(not(windows))] socket.set_reuse_address(true)?; + if args.reuse_port { + #[cfg(target_os = "linux")] + socket.set_reuse_port(true)?; + } let socket_addr = socket2::SockAddr::from(bind_addr); socket.bind(&socket_addr)?; socket.listen(128)?; |
