mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -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);
|
const buffer = new Uint8Array(1024);
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
const r = await conn.read(buffer);
|
await conn.read(buffer);
|
||||||
if (r === null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
await conn.write(response);
|
await conn.write(response);
|
||||||
}
|
}
|
||||||
} finally {
|
} catch (e) {
|
||||||
conn.close();
|
if (
|
||||||
|
!(e instanceof Deno.errors.BrokenPipe) &&
|
||||||
|
!(e instanceof Deno.errors.ConnectionReset)
|
||||||
|
) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Listening on", addr);
|
console.log("Listening on", addr);
|
||||||
|
|
|
@ -15,14 +15,16 @@ async function handle(conn: Deno.Conn): Promise<void> {
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
||||||
} catch (err) {
|
} catch (e) {
|
||||||
if (err.message !== "read error" && err.message !== "write error") {
|
if (
|
||||||
throw err;
|
!(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}/`);
|
console.log(`Proxy listening on http://${addr}/`);
|
||||||
|
|
|
@ -107,6 +107,8 @@ fn run(
|
||||||
println!("{}", wrk_cmd.join(" "));
|
println!("{}", wrk_cmd.join(" "));
|
||||||
let output = test_util::run_collect(wrk_cmd, None, None, None, true).0;
|
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);
|
println!("{}", output);
|
||||||
assert!(
|
assert!(
|
||||||
server.try_wait()?.map_or(true, |s| s.success()),
|
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
|
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)),
|
.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. */
|
/** Listens on 0.0.0.0:4500, returns rid. */
|
||||||
function listen() {
|
function listen() {
|
||||||
return Deno.core.binOpSync("listen", 0, nopBuffer);
|
return Deno.core.binOpSync("listen", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Accepts a connection, returns rid. */
|
/** Accepts a connection, returns rid. */
|
||||||
function accept(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) {
|
function close(rid) {
|
||||||
Deno.core.binOpSync("close", rid, nopBuffer);
|
Deno.core.binOpSync("close", rid);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function serve(rid) {
|
async function serve(rid) {
|
||||||
while (true) {
|
try {
|
||||||
const nread = await read(rid, requestBuf);
|
while (true) {
|
||||||
if (nread <= 0) {
|
await read(rid, requestBuf);
|
||||||
break;
|
await write(rid, responseBuf);
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
const nwritten = await write(rid, responseBuf);
|
if (
|
||||||
if (nwritten < 0) {
|
!e.message.includes("Broken pipe") &&
|
||||||
break;
|
!e.message.includes("Connection reset by peer")
|
||||||
|
) {
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(rid);
|
close(rid);
|
||||||
|
@ -65,12 +62,8 @@ async function main() {
|
||||||
`http_bench_bin_ops listening on http://127.0.0.1:4544/\n`,
|
`http_bench_bin_ops listening on http://127.0.0.1:4544/\n`,
|
||||||
);
|
);
|
||||||
|
|
||||||
for (;;) {
|
while (true) {
|
||||||
const rid = await accept(listenerRid);
|
const rid = await accept(listenerRid);
|
||||||
if (rid < 0) {
|
|
||||||
Deno.core.print(`accept error ${rid}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
serve(rid);
|
serve(rid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,15 +37,17 @@ function close(rid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function serve(rid) {
|
async function serve(rid) {
|
||||||
while (true) {
|
try {
|
||||||
const nread = await read(rid, requestBuf);
|
while (true) {
|
||||||
if (nread <= 0) {
|
await read(rid, requestBuf);
|
||||||
break;
|
await write(rid, responseBuf);
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
const nwritten = await write(rid, responseBuf);
|
if (
|
||||||
if (nwritten < 0) {
|
!e.message.includes("Broken pipe") &&
|
||||||
break;
|
!e.message.includes("Connection reset by peer")
|
||||||
|
) {
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(rid);
|
close(rid);
|
||||||
|
@ -58,12 +60,8 @@ async function main() {
|
||||||
const listenerRid = listen();
|
const listenerRid = listen();
|
||||||
Deno.core.print(`http_bench_json_ops listening on http://127.0.0.1:4544/\n`);
|
Deno.core.print(`http_bench_json_ops listening on http://127.0.0.1:4544/\n`);
|
||||||
|
|
||||||
for (;;) {
|
while (true) {
|
||||||
const rid = await accept(listenerRid);
|
const rid = await accept(listenerRid);
|
||||||
if (rid < 0) {
|
|
||||||
Deno.core.print(`accept error ${rid}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
serve(rid);
|
serve(rid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,9 +82,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const nread = core.binOpSync("op_read_sync", rid, buffer);
|
const nread = core.binOpSync("op_read_sync", rid, buffer);
|
||||||
if (nread < 0) {
|
|
||||||
throw new Error("read error");
|
|
||||||
}
|
|
||||||
|
|
||||||
return nread === 0 ? null : nread;
|
return nread === 0 ? null : nread;
|
||||||
}
|
}
|
||||||
|
@ -98,29 +95,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const nread = await core.binOpAsync("op_read_async", rid, buffer);
|
const nread = await core.binOpAsync("op_read_async", rid, buffer);
|
||||||
if (nread < 0) {
|
|
||||||
throw new Error("read error");
|
|
||||||
}
|
|
||||||
|
|
||||||
return nread === 0 ? null : nread;
|
return nread === 0 ? null : nread;
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeSync(rid, data) {
|
function writeSync(rid, data) {
|
||||||
const result = core.binOpSync("op_write_sync", rid, data);
|
return core.binOpSync("op_write_sync", rid, data);
|
||||||
if (result < 0) {
|
|
||||||
throw new Error("write error");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function write(rid, data) {
|
async function write(rid, data) {
|
||||||
const result = await core.binOpAsync("op_write_async", rid, data);
|
return await core.binOpAsync("op_write_async", rid, data);
|
||||||
if (result < 0) {
|
|
||||||
throw new Error("write error");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const READ_PER_ITER = 32 * 1024;
|
const READ_PER_ITER = 32 * 1024;
|
||||||
|
|
Loading…
Add table
Reference in a new issue