increase mpsc cap

serve
Avril 4 years ago
parent 87c8c525cd
commit 4ead6b5a92
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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 <flanchan@cumallover.me>"]
edition = "2018"

@ -93,17 +93,18 @@ impl fmt::Display for GenBodyError
async fn gen_body(chain: Arc<RwLock<Chain<String>>>, num: Option<usize>, mut output: mpsc::Sender<String>) -> 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::<usize>::None)).unify())
.and_then(|chain: Arc<RwLock<Chain<String>>>, host: IpAddr, num: Option<usize>| {
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);

Loading…
Cancel
Save