summaryrefslogtreecommitdiff
path: root/core/examples
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-05-01 17:40:00 +0200
committerGitHub <noreply@github.com>2023-05-01 17:40:00 +0200
commitdcf391ffed3850f9026d88b146e156375c4619d4 (patch)
tree2134755e8abbecaba1d42703fe727ae701ee390c /core/examples
parent6728ad4203d731e555dabf89ec6157f113454ce6 (diff)
refactor: migrate async ops to generated wrappers (#18937)
Migrates some of existing async ops to generated wrappers introduced in https://github.com/denoland/deno/pull/18887. As a result "core.opAsync2" was removed. I will follow up with more PRs that migrate all the async ops to generated wrappers.
Diffstat (limited to 'core/examples')
-rw-r--r--core/examples/http_bench_json_ops/http_bench_json_ops.js32
1 files changed, 14 insertions, 18 deletions
diff --git a/core/examples/http_bench_json_ops/http_bench_json_ops.js b/core/examples/http_bench_json_ops/http_bench_json_ops.js
index 0c3b5be13..beb6c90e4 100644
--- a/core/examples/http_bench_json_ops/http_bench_json_ops.js
+++ b/core/examples/http_bench_json_ops/http_bench_json_ops.js
@@ -3,7 +3,16 @@
// then write this fixed 'responseBuf'. The point of this benchmark is to
// exercise the event loop in a simple yet semi-realistic way.
-const { ops, opAsync, opAsync2 } = Deno.core;
+// deno-lint-ignore-file camelcase
+
+const { op_listen } = Deno.core.ops;
+const {
+ op_accept,
+ op_read_socket,
+} = core.generateAsyncOpHandler(
+ "op_accept",
+ "op_read_socket",
+);
const requestBuf = new Uint8Array(64 * 1024);
const responseBuf = new Uint8Array(
@@ -12,24 +21,10 @@ const responseBuf = new Uint8Array(
.map((c) => c.charCodeAt(0)),
);
-/** Listens on 0.0.0.0:4570, returns rid. */
-function listen() {
- return ops.op_listen();
-}
-
-/** Accepts a connection, returns rid. */
-function accept(serverRid) {
- return opAsync("op_accept", serverRid);
-}
-
-function read(serverRid, buf) {
- return opAsync2("op_read_socket", serverRid, buf);
-}
-
async function serve(rid) {
try {
while (true) {
- await read(rid, requestBuf);
+ await op_read_socket(rid, requestBuf);
if (!ops.op_try_write(rid, responseBuf)) {
await Deno.core.writeAll(rid, responseBuf);
}
@@ -41,11 +36,12 @@ async function serve(rid) {
}
async function main() {
- const listenerRid = listen();
+ /** Listens on 0.0.0.0:4570, returns rid. */
+ const listenerRid = op_listen();
Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4570/\n`);
while (true) {
- const rid = await accept(listenerRid);
+ const rid = await op_accept(listenerRid);
serve(rid);
}
}