diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2022-03-23 12:04:20 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-23 12:04:20 +0900 |
commit | 7feb25d448b356ac869ef919c57ef314382a8eb7 (patch) | |
tree | 35b5137590366e915750ab19d6c3b7dbd3dfe32d /ext/net/01_net.js | |
parent | 5c9844e5f7074b2623ff0ddd69f5adcd1356ae38 (diff) |
feat(unstable): add ref/unref to Listener (#13961)
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/net/01_net.js')
-rw-r--r-- | ext/net/01_net.js | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 11d6fbfac..a17c66724 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -6,14 +6,18 @@ const { BadResourcePrototype, InterruptedPrototype } = core; const { ReadableStream, WritableStream } = window.__bootstrap.streams; const { + Error, ObjectPrototypeIsPrototypeOf, PromiseResolve, + Symbol, SymbolAsyncIterator, - Error, - Uint8Array, + SymbolFor, TypedArrayPrototypeSubarray, + Uint8Array, } = window.__bootstrap.primordials; + const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId"); + async function read( rid, buffer, @@ -191,9 +195,16 @@ class UnixConn extends Conn {} + // Use symbols for method names to hide these in stable API. + // TODO(kt3k): Remove these symbols when ref/unref become stable. + const listenerRef = Symbol("listenerRef"); + const listenerUnref = Symbol("listenerUnref"); + class Listener { #rid = 0; #addr = null; + #unref = false; + #promiseId = null; constructor(rid, addr) { this.#rid = rid; @@ -208,15 +219,21 @@ return this.#addr; } - async accept() { - const res = await opAccept(this.rid, this.addr.transport); - if (this.addr.transport == "tcp") { - return new TcpConn(res.rid, res.remoteAddr, res.localAddr); - } else if (this.addr.transport == "unix") { - return new UnixConn(res.rid, res.remoteAddr, res.localAddr); - } else { - throw new Error("unreachable"); + accept() { + const promise = opAccept(this.rid, this.addr.transport); + this.#promiseId = promise[promiseIdSymbol]; + if (this.#unref) { + this.#unrefOpAccept(); } + return promise.then((res) => { + if (this.addr.transport == "tcp") { + return new TcpConn(res.rid, res.remoteAddr, res.localAddr); + } else if (this.addr.transport == "unix") { + return new UnixConn(res.rid, res.remoteAddr, res.localAddr); + } else { + throw new Error("unreachable"); + } + }); } async next() { @@ -247,6 +264,27 @@ [SymbolAsyncIterator]() { return this; } + + [listenerRef]() { + this.#unref = false; + this.#refOpAccept(); + } + + [listenerUnref]() { + this.#unref = true; + this.#unrefOpAccept(); + } + + #refOpAccept() { + if (typeof this.#promiseId === "number") { + core.refOp(this.#promiseId); + } + } + #unrefOpAccept() { + if (typeof this.#promiseId === "number") { + core.unrefOp(this.#promiseId); + } + } } class Datagram { @@ -304,14 +342,14 @@ } } - function listen({ hostname, ...options }) { + function listen({ hostname, ...options }, constructor = Listener) { const res = opListen({ transport: "tcp", hostname: typeof hostname === "undefined" ? "0.0.0.0" : hostname, ...options, }); - return new Listener(res.rid, res.localAddr); + return new constructor(res.rid, res.localAddr); } async function connect(options) { @@ -335,6 +373,8 @@ UnixConn, opConnect, listen, + listenerRef, + listenerUnref, opListen, Listener, shutdown, |