From 09464c548ad940d3db357ca7152d8d92be234ef3 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 20 Feb 2021 17:08:38 +0000 Subject: [PATCH] update: add endless data source for debugging --- Cargo.lock | 1 + crates/ptth_relay/Cargo.toml | 1 + crates/ptth_relay/src/lib.rs | 38 ++++++++++++++++++++++++++++++------ handlebars/relay/debug.hbs | 12 ++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1a406a..6226349 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1717,6 +1717,7 @@ dependencies = [ "hyper", "itertools", "ptth_core", + "rand 0.6.5", "rmp-serde", "serde", "serde_json", diff --git a/crates/ptth_relay/Cargo.toml b/crates/ptth_relay/Cargo.toml index 6619475..ed59393 100644 --- a/crates/ptth_relay/Cargo.toml +++ b/crates/ptth_relay/Cargo.toml @@ -18,6 +18,7 @@ handlebars = "3.5.1" http = "0.2.1" hyper = "0.13.8" itertools = "0.9.0" +rand = "0.6.5" rmp-serde = "0.14.4" serde = {version = "1.0.117", features = ["derive"]} serde_json = "1.0.60" diff --git a/crates/ptth_relay/src/lib.rs b/crates/ptth_relay/src/lib.rs index dfcd0d0..3558370 100644 --- a/crates/ptth_relay/src/lib.rs +++ b/crates/ptth_relay/src/lib.rs @@ -16,6 +16,7 @@ use std::{ borrow::Cow, + convert::Infallible, net::SocketAddr, path::{Path, PathBuf}, sync::Arc, @@ -302,6 +303,24 @@ async fn handle_server_list ( Ok (ok_reply (s)?) } +async fn handle_endless_source (gib: usize) -> Result , http::Error> { + use futures::stream::StreamExt; + + let random_block = { + use rand::RngCore; + + let mut rng = rand::thread_rng (); + let mut block = vec! [0u8; 64 * 1024]; + rng.fill_bytes (&mut block); + block + }; + + Response::builder () + .status (StatusCode::OK) + .header ("content-type", "application/octet-stream") + .body (Body::wrap_stream (futures::stream::repeat (Ok::<_, Infallible> (random_block)).take (gib * 1024 * 1024 / 64))) +} + #[instrument (level = "trace", skip (req, state, handlebars))] async fn handle_all ( req: Request , @@ -355,8 +374,16 @@ async fn handle_all ( } } else if let Some (rest) = prefix_match ("/frontend/debug/", &path) { - let s = handlebars.render ("debug", &())?; - Ok (ok_reply (s)?) + if rest == "" { + let s = handlebars.render ("debug", &())?; + Ok (ok_reply (s)?) + } + else if rest == "endless_source" { + Ok (handle_endless_source (1).await?) + } + else { + Ok (error_reply (StatusCode::OK, "Can't route URL")?) + } } else if path == "/" { let s = handlebars.render ("root", &())?; @@ -372,7 +399,7 @@ async fn handle_all ( scraper_api::handle (req, state, rest).await } else { - Ok (error_reply (StatusCode::OK, "Hi")?) + Ok (error_reply (StatusCode::OK, "Can't route URL")?) } } @@ -385,8 +412,9 @@ pub fn load_templates (asset_root: &Path) let asset_root = asset_root.join ("handlebars/relay"); for (k, v) in &[ - ("server_list", "server_list.hbs"), + ("debug", "debug.hbs"), ("root", "root.hbs"), + ("server_list", "server_list.hbs"), ] { handlebars.register_template_file (k, &asset_root.join (v))?; } @@ -439,8 +467,6 @@ pub async fn run_relay ( } let make_svc = make_service_fn (|_conn| { - use std::convert::Infallible; - let state = state.clone (); let handlebars = handlebars.clone (); diff --git a/handlebars/relay/debug.hbs b/handlebars/relay/debug.hbs index c8c7080..0b0a8a0 100644 --- a/handlebars/relay/debug.hbs +++ b/handlebars/relay/debug.hbs @@ -34,11 +34,19 @@ AIABAACAAQAAgAEAAIABAACAAQAAgAEAAIABAACAAQAA" rel="icon" type="image/x-icon" /> background-color: #ddd; } -ptth_relay debugging tools +Debugging tools (relay) -

ptth_relay debugging tools

+

Debugging tools (relay)

+ +

+ +Lorem ipsum dolor set amet + +

+ +1 GiB random garbage data source