mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
fix(core): error handling in examples (#9867)
This commit is contained in:
parent
c4b21fbff1
commit
d050b491b1
8 changed files with 72 additions and 89 deletions
|
@ -13,15 +13,18 @@ async function handle(conn: Deno.Conn): Promise<void> {
|
|||
const buffer = new Uint8Array(1024);
|
||||
try {
|
||||
while (true) {
|
||||
const r = await conn.read(buffer);
|
||||
if (r === null) {
|
||||
break;
|
||||
}
|
||||
await conn.read(buffer);
|
||||
await conn.write(response);
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
} catch (e) {
|
||||
if (
|
||||
!(e instanceof Deno.errors.BrokenPipe) &&
|
||||
!(e instanceof Deno.errors.ConnectionReset)
|
||||
) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
conn.close();
|
||||
}
|
||||
|
||||
console.log("Listening on", addr);
|
||||
|
|
|
@ -15,14 +15,16 @@ async function handle(conn: Deno.Conn): Promise<void> {
|
|||
});
|
||||
try {
|
||||
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
||||
} catch (err) {
|
||||
if (err.message !== "read error" && err.message !== "write error") {
|
||||
throw err;
|
||||
} catch (e) {
|
||||
if (
|
||||
!(e instanceof Deno.errors.BrokenPipe) &&
|
||||
!(e instanceof Deno.errors.ConnectionReset)
|
||||
) {
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
origin.close();
|
||||
}
|
||||
conn.close();
|
||||
origin.close();
|
||||
}
|
||||
|
||||
console.log(`Proxy listening on http://${addr}/`);
|
||||
|
|
|
@ -107,6 +107,8 @@ fn run(
|
|||
println!("{}", wrk_cmd.join(" "));
|
||||
let output = test_util::run_collect(wrk_cmd, None, None, None, true).0;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(1)); // wait to capture failure. TODO racy.
|
||||
|
||||
println!("{}", output);
|
||||
assert!(
|
||||
server.try_wait()?.map_or(true, |s| s.success()),
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
// Note: this is a keep-alive server.
|
||||
const { Server } = require("net");
|
||||
const port = process.argv[2] || "4544";
|
||||
console.log("port", port);
|
||||
|
||||
const response = Buffer.from(
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
|
||||
);
|
||||
|
||||
function write(socket, buffer) {
|
||||
const p = new Promise((resolve, _) => {
|
||||
socket.write(buffer, resolve);
|
||||
});
|
||||
return Promise.resolve(p);
|
||||
}
|
||||
|
||||
Server(async (socket) => {
|
||||
socket.on("error", (_) => {
|
||||
socket.destroy();
|
||||
});
|
||||
for await (const _ of socket) {
|
||||
await write(socket, response);
|
||||
}
|
||||
}).listen(port);
|
|
@ -32,3 +32,29 @@ declare global {
|
|||
var core: any; // eslint-disable-line no-var
|
||||
}
|
||||
}
|
||||
|
||||
unitTest(async function binOpsAsyncBadResource(): Promise<void> {
|
||||
try {
|
||||
const nonExistingRid = 9999;
|
||||
await Deno.core.binOpAsync(
|
||||
"op_read_async",
|
||||
nonExistingRid,
|
||||
new Uint8Array(0),
|
||||
);
|
||||
} catch (e) {
|
||||
if (!(e instanceof Deno.errors.BadResource)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
unitTest(function binOpsSyncBadResource(): void {
|
||||
try {
|
||||
const nonExistingRid = 9999;
|
||||
Deno.core.binOpSync("op_read_sync", nonExistingRid, new Uint8Array(0));
|
||||
} catch (e) {
|
||||
if (!(e instanceof Deno.errors.BadResource)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -9,19 +9,14 @@ const responseBuf = new Uint8Array(
|
|||
.map((c) => c.charCodeAt(0)),
|
||||
);
|
||||
|
||||
// This buffer exists purely to avoid trigerring the bin-op buf assert
|
||||
// in practice all deno bin ops accept buffers, this bench is an exception
|
||||
// TODO(@AaronO): remove once we drop variadic BufVec compat
|
||||
const nopBuffer = new Uint8Array();
|
||||
|
||||
/** Listens on 0.0.0.0:4500, returns rid. */
|
||||
function listen() {
|
||||
return Deno.core.binOpSync("listen", 0, nopBuffer);
|
||||
return Deno.core.binOpSync("listen", 0);
|
||||
}
|
||||
|
||||
/** Accepts a connection, returns rid. */
|
||||
function accept(rid) {
|
||||
return Deno.core.binOpAsync("accept", rid, nopBuffer);
|
||||
return Deno.core.binOpAsync("accept", rid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,19 +33,21 @@ function write(rid, data) {
|
|||
}
|
||||
|
||||
function close(rid) {
|
||||
Deno.core.binOpSync("close", rid, nopBuffer);
|
||||
Deno.core.binOpSync("close", rid);
|
||||
}
|
||||
|
||||
async function serve(rid) {
|
||||
while (true) {
|
||||
const nread = await read(rid, requestBuf);
|
||||
if (nread <= 0) {
|
||||
break;
|
||||
try {
|
||||
while (true) {
|
||||
await read(rid, requestBuf);
|
||||
await write(rid, responseBuf);
|
||||
}
|
||||
|
||||
const nwritten = await write(rid, responseBuf);
|
||||
if (nwritten < 0) {
|
||||
break;
|
||||
} catch (e) {
|
||||
if (
|
||||
!e.message.includes("Broken pipe") &&
|
||||
!e.message.includes("Connection reset by peer")
|
||||
) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
close(rid);
|
||||
|
@ -65,12 +62,8 @@ async function main() {
|
|||
`http_bench_bin_ops listening on http://127.0.0.1:4544/\n`,
|
||||
);
|
||||
|
||||
for (;;) {
|
||||
while (true) {
|
||||
const rid = await accept(listenerRid);
|
||||
if (rid < 0) {
|
||||
Deno.core.print(`accept error ${rid}`);
|
||||
return;
|
||||
}
|
||||
serve(rid);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,15 +37,17 @@ function close(rid) {
|
|||
}
|
||||
|
||||
async function serve(rid) {
|
||||
while (true) {
|
||||
const nread = await read(rid, requestBuf);
|
||||
if (nread <= 0) {
|
||||
break;
|
||||
try {
|
||||
while (true) {
|
||||
await read(rid, requestBuf);
|
||||
await write(rid, responseBuf);
|
||||
}
|
||||
|
||||
const nwritten = await write(rid, responseBuf);
|
||||
if (nwritten < 0) {
|
||||
break;
|
||||
} catch (e) {
|
||||
if (
|
||||
!e.message.includes("Broken pipe") &&
|
||||
!e.message.includes("Connection reset by peer")
|
||||
) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
close(rid);
|
||||
|
@ -58,12 +60,8 @@ async function main() {
|
|||
const listenerRid = listen();
|
||||
Deno.core.print(`http_bench_json_ops listening on http://127.0.0.1:4544/\n`);
|
||||
|
||||
for (;;) {
|
||||
while (true) {
|
||||
const rid = await accept(listenerRid);
|
||||
if (rid < 0) {
|
||||
Deno.core.print(`accept error ${rid}`);
|
||||
return;
|
||||
}
|
||||
serve(rid);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,9 +82,6 @@
|
|||
}
|
||||
|
||||
const nread = core.binOpSync("op_read_sync", rid, buffer);
|
||||
if (nread < 0) {
|
||||
throw new Error("read error");
|
||||
}
|
||||
|
||||
return nread === 0 ? null : nread;
|
||||
}
|
||||
|
@ -98,29 +95,16 @@
|
|||
}
|
||||
|
||||
const nread = await core.binOpAsync("op_read_async", rid, buffer);
|
||||
if (nread < 0) {
|
||||
throw new Error("read error");
|
||||
}
|
||||
|
||||
return nread === 0 ? null : nread;
|
||||
}
|
||||
|
||||
function writeSync(rid, data) {
|
||||
const result = core.binOpSync("op_write_sync", rid, data);
|
||||
if (result < 0) {
|
||||
throw new Error("write error");
|
||||
}
|
||||
|
||||
return result;
|
||||
return core.binOpSync("op_write_sync", rid, data);
|
||||
}
|
||||
|
||||
async function write(rid, data) {
|
||||
const result = await core.binOpAsync("op_write_async", rid, data);
|
||||
if (result < 0) {
|
||||
throw new Error("write error");
|
||||
}
|
||||
|
||||
return result;
|
||||
return await core.binOpAsync("op_write_async", rid, data);
|
||||
}
|
||||
|
||||
const READ_PER_ITER = 32 * 1024;
|
||||
|
|
Loading…
Add table
Reference in a new issue