1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

perf(ext/http): optimize auto cleanup of request resource (#11978)

Fixes #11963.
This commit is contained in:
Bert Belder 2021-09-10 15:47:09 -07:00 committed by GitHub
parent 87052927af
commit fa963909e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 7 deletions

View file

@ -148,6 +148,10 @@
opSync("op_close", rid);
}
function tryClose(rid) {
opSync("op_try_close", rid);
}
function print(str, isErr = false) {
opSync("op_print", str, isErr);
}
@ -175,6 +179,7 @@
opSync,
ops,
close,
tryClose,
print,
resources,
registerErrorBuilder,

View file

@ -17,6 +17,7 @@ pub(crate) fn init_builtins() -> Extension {
))
.ops(vec![
("op_close", op_sync(op_close)),
("op_try_close", op_sync(op_try_close)),
("op_print", op_sync(op_print)),
("op_resources", op_sync(op_resources)),
])
@ -44,10 +45,24 @@ pub fn op_close(
rid: Option<ResourceId>,
_: (),
) -> Result<(), AnyError> {
// TODO(@AaronO): drop Option after improving type-strictness balance in serde_v8
// TODO(@AaronO): drop Option after improving type-strictness balance in
// serde_v8
let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?;
state.resource_table.close(rid)?;
Ok(())
}
/// Try to remove a resource from the resource table. If there is no resource
/// with the specified `rid`, this is a no-op.
pub fn op_try_close(
state: &mut OpState,
rid: Option<ResourceId>,
_: (),
) -> Result<(), AnyError> {
// TODO(@AaronO): drop Option after improving type-strictness balance in
// serde_v8.
let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?;
let _ = state.resource_table.close(rid);
Ok(())
}

View file

@ -297,13 +297,12 @@
ws[_eventLoop]();
}
} else {
} else if (typeof requestRid === "number") {
// Try to close "request" resource. It might have been already consumed,
// but if it hasn't been we need to close it here to avoid resource leak.
try {
SetPrototypeDelete(httpConn.managedResources, requestRid);
core.close(requestRid);
} catch { /* pass */ }
// but if it hasn't been we need to close it here to avoid resource
// leak.
SetPrototypeDelete(httpConn.managedResources, requestRid);
core.tryClose(requestRid);
}
};
}