From 4ead6b5a92727d67523ea2b09e73f1c51489c0fe Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 8 Oct 2020 09:52:49 +0100 Subject: [PATCH] increase mpsc cap --- Cargo.toml | 2 +- src/main.rs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60d719e..886cb6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "markov" -version = "0.1.1" +version = "0.1.2" description = "Generate string of text from Markov chain fed by stdin" authors = ["Avril "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index 7fdf177..478f236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,17 +93,18 @@ impl fmt::Display for GenBodyError async fn gen_body(chain: Arc>>, num: Option, mut output: mpsc::Sender) -> Result<(), GenBodyError> { - let chain = chain.read().await; - if !chain.is_empty() { - match num { - Some(num) if num < MAX_GEN_SIZE => { - for string in chain.str_iter_for(num) { - output.send(string).await.map_err(|e| GenBodyError(e.0))?; - } - }, - _ => output.send(chain.generate_str()).await.map_err(|e| GenBodyError(e.0))?, + let chain = chain.read().await; + if !chain.is_empty() { + match num { + Some(num) if num < MAX_GEN_SIZE => { + //This could DoS `full_body` and writes, potentially. + for string in chain.str_iter_for(num) { + output.send(string).await.map_err(|e| GenBodyError(e.0))?; + } + }, + _ => output.send(chain.generate_str()).await.map_err(|e| GenBodyError(e.0))?, + } } - } Ok(()) } @@ -149,7 +150,7 @@ async fn main() { .and(warp::path::param().map(|opt: usize| Some(opt)).or(warp::any().map(|| Option::::None)).unify()) .and_then(|chain: Arc>>, host: IpAddr, num: Option| { async move { - let (tx, rx) = mpsc::channel(16); + let (tx, rx) = mpsc::channel(MAX_GEN_SIZE); tokio::spawn(gen_body(chain, num, tx)); Ok::<_, std::convert::Infallible>(Response::new(Body::wrap_stream(rx.map(move |x| { info!("{} <- {:?}", host, x);