From 9ed11a94cdf3d7648f289a33826ab03b8b47f405 Mon Sep 17 00:00:00 2001 From: dekkku Date: Fri, 13 Sep 2024 03:09:27 +0530 Subject: [PATCH] handle error in request batcher --- projects/dekkku/src/main.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/projects/dekkku/src/main.rs b/projects/dekkku/src/main.rs index 655f566..3b560e4 100644 --- a/projects/dekkku/src/main.rs +++ b/projects/dekkku/src/main.rs @@ -328,16 +328,31 @@ impl RequestBatcher { tokio::spawn(async move { let result = fetch_with_retry(&client, &url).await; - - if let Ok(response) = result { - if let Ok(body) = response.bytes().await { - // let's say in queue, we've got around 10k requests and only one is executing right now. - // and if it fails then we won't reach here to remove the all requests from queue - // handle that scenario. - let senders = { on_going_req.remove(&url) }; + let senders = on_going_req.remove(&url); + + match result { + Ok(response) => match response.bytes().await { + Ok(body) => { + if let Some((_, senders)) = senders { + for sender in senders { + let _ = sender.send(Ok(body.clone())); + } + } + } + Err(e) => { + if let Some((_, senders)) = senders { + for sender in senders { + let err = anyhow::anyhow!(e.to_string()); + let _ = sender.send(Err(err)); + } + } + } + }, + Err(e) => { if let Some((_, senders)) = senders { for sender in senders { - let _ = sender.send(Ok(body.clone())); + let err = anyhow::anyhow!(e.to_string()); + let _ = sender.send(Err(err)); } } }