diff options
author | Luca Casonato <hello@lcas.dev> | 2023-11-01 20:26:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-01 20:26:12 +0100 |
commit | d42f1543121e7245789a96a485d1ef7645cb5fba (patch) | |
tree | d57a10ac527fe5b6796a3a8866af95f0f1a5d7bd /ext/http/01_http.js | |
parent | 1d19b1011bd7df50598f5981408c2d78c35b76d2 (diff) |
feat: disposable Deno resources (#20845)
This commit implements Symbol.dispose and Symbol.asyncDispose for
the relevant resources.
Closes #20839
---------
Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/http/01_http.js')
-rw-r--r-- | ext/http/01_http.js | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/ext/http/01_http.js b/ext/http/01_http.js index 705e616e4..cfe0f710e 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -44,6 +44,7 @@ import { ReadableStreamPrototype, } from "ext:deno_web/06_streams.js"; import { serve } from "ext:deno_http/00_serve.js"; +import { SymbolDispose } from "ext:deno_web/00_infra.js"; const { ArrayPrototypeIncludes, ArrayPrototypeMap, @@ -69,6 +70,9 @@ const { const connErrorSymbol = Symbol("connError"); const _deferred = Symbol("upgradeHttpDeferred"); +/** @type {(self: HttpConn, rid: number) => boolean} */ +let deleteManagedResource; + class HttpConn { #rid = 0; #closed = false; @@ -79,7 +83,12 @@ class HttpConn { // that were created during lifecycle of this request. // When the connection is closed these resources should be closed // as well. - managedResources = new SafeSet(); + #managedResources = new SafeSet(); + + static { + deleteManagedResource = (self, rid) => + SetPrototypeDelete(self.#managedResources, rid); + } constructor(rid, remoteAddr, localAddr) { this.#rid = rid; @@ -123,7 +132,7 @@ class HttpConn { } const { 0: streamRid, 1: method, 2: url } = nextRequest; - SetPrototypeAdd(this.managedResources, streamRid); + SetPrototypeAdd(this.#managedResources, streamRid); /** @type {ReadableStream<Uint8Array> | undefined} */ let body = null; @@ -167,13 +176,21 @@ class HttpConn { if (!this.#closed) { this.#closed = true; core.close(this.#rid); - for (const rid of new SafeSetIterator(this.managedResources)) { - SetPrototypeDelete(this.managedResources, rid); + for (const rid of new SafeSetIterator(this.#managedResources)) { + SetPrototypeDelete(this.#managedResources, rid); core.close(rid); } } } + [SymbolDispose]() { + core.tryClose(this.#rid); + for (const rid of new SafeSetIterator(this.#managedResources)) { + SetPrototypeDelete(this.#managedResources, rid); + core.tryClose(rid); + } + } + [SymbolAsyncIterator]() { // deno-lint-ignore no-this-alias const httpConn = this; @@ -395,7 +412,7 @@ function createRespondWith( abortController.abort(error); throw error; } finally { - if (SetPrototypeDelete(httpConn.managedResources, streamRid)) { + if (deleteManagedResource(httpConn, streamRid)) { core.close(streamRid); } } |