0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: use Deno.core.tryClose (#11980)

This commit is contained in:
Bartek Iwańczuk 2021-09-11 02:54:37 +02:00 committed by GitHub
parent bd4ca721eb
commit ba8bbe6f1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 85 deletions

View file

@ -33,9 +33,18 @@ declare namespace Deno {
*/ */
function resources(): Record<string, string>; function resources(): Record<string, string>;
/** Close the resource with the specified op id. */ /**
* Close the resource with the specified op id. Throws `BadResource` error
* if resource doesn't exist in resource table.
*/
function close(rid: number): void; function close(rid: number): void;
/**
* Try close the resource with the specified op id; if resource with given
* id doesn't exist do nothing.
*/
function tryClose(rid: number): void;
/** Get heap stats for current isolate/worker */ /** Get heap stats for current isolate/worker */
function heapStats(): Record<string, number>; function heapStats(): Record<string, number>;

View file

@ -86,11 +86,7 @@
// A finalization registry to clean up underlying fetch resources that are GC'ed. // A finalization registry to clean up underlying fetch resources that are GC'ed.
const RESOURCE_REGISTRY = new FinalizationRegistry((rid) => { const RESOURCE_REGISTRY = new FinalizationRegistry((rid) => {
try { core.tryClose(rid);
core.close(rid);
} catch {
// might have already been closed
}
}); });
/** /**
@ -106,11 +102,7 @@
new DOMException("Ongoing fetch was aborted.", "AbortError"), new DOMException("Ongoing fetch was aborted.", "AbortError"),
); );
} }
try { core.tryClose(responseBodyRid);
core.close(responseBodyRid);
} catch (_) {
// might have already been closed
}
} }
// TODO(lucacasonato): clean up registration // TODO(lucacasonato): clean up registration
terminator[abortSignal.add](onAbort); terminator[abortSignal.add](onAbort);
@ -132,11 +124,7 @@
RESOURCE_REGISTRY.unregister(readable); RESOURCE_REGISTRY.unregister(readable);
// We have reached the end of the body, so we close the stream. // We have reached the end of the body, so we close the stream.
controller.close(); controller.close();
try { core.tryClose(responseBodyRid);
core.close(responseBodyRid);
} catch (_) {
// might have already been closed
}
} }
} catch (err) { } catch (err) {
RESOURCE_REGISTRY.unregister(readable); RESOURCE_REGISTRY.unregister(readable);
@ -149,11 +137,7 @@
// error. // error.
controller.error(err); controller.error(err);
} }
try { core.tryClose(responseBodyRid);
core.close(responseBodyRid);
} catch (_) {
// might have already been closed
}
} }
}, },
cancel() { cancel() {
@ -234,15 +218,11 @@
}, reqBody instanceof Uint8Array ? reqBody : null); }, reqBody instanceof Uint8Array ? reqBody : null);
function onAbort() { function onAbort() {
try { if (cancelHandleRid !== null) {
core.close(cancelHandleRid); core.tryClose(cancelHandleRid);
} catch (_) {
// might have already been closed
} }
try { if (requestBodyRid !== null) {
core.close(requestBodyRid); core.tryClose(requestBodyRid);
} catch (_) {
// might have already been closed
} }
} }
terminator[abortSignal.add](onAbort); terminator[abortSignal.add](onAbort);
@ -280,11 +260,7 @@
break; break;
} }
} }
try { core.tryClose(requestBodyRid);
core.close(requestBodyRid);
} catch (_) {
// might have already been closed
}
})(); })();
} }
@ -295,10 +271,8 @@
throw err; throw err;
}); });
} finally { } finally {
try { if (cancelHandleRid !== null) {
core.close(cancelHandleRid); core.tryClose(cancelHandleRid);
} catch (_) {
// might have already been closed
} }
} }
if (terminator.aborted) return abortedNetworkError(); if (terminator.aborted) return abortedNetworkError();

View file

@ -120,11 +120,7 @@
/** @returns {void} */ /** @returns {void} */
close() { close() {
for (const rid of SetPrototypeValues(this.managedResources)) { for (const rid of SetPrototypeValues(this.managedResources)) {
try { core.tryClose(rid);
core.close(rid);
} catch (_e) {
// pass, might have already been closed
}
} }
core.close(this.#rid); core.close(this.#rid);
} }
@ -284,12 +280,7 @@
const event = new CloseEvent("close"); const event = new CloseEvent("close");
ws.dispatchEvent(event); ws.dispatchEvent(event);
try { core.tryClose(wsRid);
core.close(wsRid);
} catch (err) {
// Ignore error if the socket has already been closed.
if (!(err instanceof Deno.errors.BadResource)) throw err;
}
} else { } else {
ws[_readyState] = WebSocket.OPEN; ws[_readyState] = WebSocket.OPEN;
const event = new Event("open"); const event = new Event("open");

View file

@ -65,19 +65,6 @@
const CLOSING = 2; const CLOSING = 2;
const CLOSED = 3; const CLOSED = 3;
/**
* Tries to close the resource (and ignores BadResource errors).
* @param {number} rid
*/
function tryClose(rid) {
try {
core.close(rid);
} catch (err) {
// Ignore error if the socket has already been closed.
if (!(err instanceof Deno.errors.BadResource)) throw err;
}
}
const handlerSymbol = Symbol("eventHandlers"); const handlerSymbol = Symbol("eventHandlers");
function makeWrappedHandler(handler) { function makeWrappedHandler(handler) {
function wrappedHandler(...args) { function wrappedHandler(...args) {
@ -292,7 +279,7 @@
const event = new CloseEvent("close"); const event = new CloseEvent("close");
this.dispatchEvent(event); this.dispatchEvent(event);
tryClose(this[_rid]); core.tryClose(this[_rid]);
}, },
); );
} else { } else {
@ -430,7 +417,7 @@
reason, reason,
}); });
this.dispatchEvent(event); this.dispatchEvent(event);
tryClose(this[_rid]); core.tryClose(this[_rid]);
}, },
); );
} }
@ -484,7 +471,7 @@
reason: value.reason, reason: value.reason,
}); });
this.dispatchEvent(event); this.dispatchEvent(event);
tryClose(this[_rid]); core.tryClose(this[_rid]);
break; break;
} }
case "error": { case "error": {
@ -497,7 +484,7 @@
const closeEv = new CloseEvent("close"); const closeEv = new CloseEvent("close");
this.dispatchEvent(closeEv); this.dispatchEvent(closeEv);
tryClose(this[_rid]); core.tryClose(this[_rid]);
break; break;
} }
} }

View file

@ -56,19 +56,6 @@
], ],
); );
/**
* Tries to close the resource (and ignores BadResource errors).
* @param {number} rid
*/
function tryClose(rid) {
try {
core.close(rid);
} catch (err) {
// Ignore error if the socket has already been closed.
if (!(err instanceof Deno.errors.BadResource)) throw err;
}
}
const _rid = Symbol("[[rid]]"); const _rid = Symbol("[[rid]]");
const _url = Symbol("[[url]]"); const _url = Symbol("[[url]]");
const _connection = Symbol("[[connection]]"); const _connection = Symbol("[[connection]]");
@ -279,7 +266,7 @@
case "close": { case "close": {
if (this[_closing]) { if (this[_closing]) {
this[_closed].resolve(value); this[_closed].resolve(value);
tryClose(this[_rid]); core.tryClose(this[_rid]);
} else { } else {
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_close", { core.opAsync("op_ws_close", {
@ -288,12 +275,12 @@
}), }),
() => { () => {
this[_closed].resolve(value); this[_closed].resolve(value);
tryClose(this[_rid]); core.tryClose(this[_rid]);
}, },
(err) => { (err) => {
this[_closed].reject(err); this[_closed].reject(err);
controller.error(err); controller.error(err);
tryClose(this[_rid]); core.tryClose(this[_rid]);
}, },
); );
} }
@ -303,7 +290,7 @@
const err = new Error(value); const err = new Error(value);
this[_closed].reject(err); this[_closed].reject(err);
controller.error(err); controller.error(err);
tryClose(this[_rid]); core.tryClose(this[_rid]);
break; break;
} }
} }
@ -327,7 +314,7 @@
} }
}, },
(err) => { (err) => {
tryClose(cancelRid); core.tryClose(cancelRid);
this[_connection].reject(err); this[_connection].reject(err);
this[_closed].reject(err); this[_closed].reject(err);
}, },
@ -393,7 +380,7 @@
reason: closeInfo.reason, reason: closeInfo.reason,
}), }),
(err) => { (err) => {
this[_rid] && tryClose(this[_rid]); this[_rid] && core.tryClose(this[_rid]);
this[_closed].reject(err); this[_closed].reject(err);
}, },
); );