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/00_serve.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/00_serve.js')
-rw-r--r-- | ext/http/00_serve.js | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 17a67814b..b99f28c0f 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -36,6 +36,7 @@ import { } from "ext:deno_web/06_streams.js"; import { listen, listenOptionApiName, TcpConn } from "ext:deno_net/01_net.js"; import { listenTls } from "ext:deno_net/02_tls.js"; +import { SymbolAsyncDispose } from "ext:deno_web/00_infra.js"; const { ArrayPrototypePush, ObjectHasOwn, @@ -343,6 +344,7 @@ class CallbackContext { fallbackHost; serverRid; closed; + /** @type {Promise<void> | undefined} */ closing; listener; @@ -671,22 +673,25 @@ function serveHttpOn(context, callback) { PromisePrototypeCatch(callback(req), promiseErrorHandler); } - if (!context.closed && !context.closing) { - context.closed = true; - await op_http_close(rid, false); + if (!context.closing && !context.closed) { + context.closing = op_http_close(rid, false); context.close(); } + + await context.closing; + context.close(); + context.closed = true; })(); return { finished, async shutdown() { - if (!context.closed && !context.closing) { + if (!context.closing && !context.closed) { // Shut this HTTP server down gracefully - context.closing = true; - await op_http_close(context.serverRid, true); - context.closed = true; + context.closing = op_http_close(context.serverRid, true); } + await context.closing; + context.closed = true; }, ref() { ref = true; @@ -700,6 +705,9 @@ function serveHttpOn(context, callback) { core.unrefOp(currentPromise[promiseIdSymbol]); } }, + [SymbolAsyncDispose]() { + return this.shutdown(); + }, }; } |