From fc99fd918207fc9b61736a5b6ffd57d92eb31f0b Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 8 Sep 2020 00:44:52 -0400 Subject: [PATCH] clone request --- src/main.rs | 8 ++++--- src/speak_proxy.rs | 58 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 764cf59..364eff9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![feature(toowned_clone_into)] + #[macro_use] extern crate anyhow; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; @@ -8,7 +10,7 @@ pub mod speak_proxy; use anyhow::Result as AnyhowResult; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; use futures::future::{self, Future}; -use crate::speak_proxy::speak_proxy; +use crate::speak_proxy::{speak_proxy, speak_proxy_with_retry}; use hyper::{Method, StatusCode}; use hyper::header::HeaderValue; use hyper::http::header::HeaderName; @@ -135,7 +137,7 @@ pub async fn handle( match req.uri().path() { "/speak" => { let nr_transaction = newrelic_logger.web_transaction("/speak"); - let result = speak_proxy(client_ip, req, router, nr_transaction, "/speak").await; + let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak").await; match result { Ok(response) => { Ok(response) @@ -150,7 +152,7 @@ pub async fn handle( }, "/speak_spectrogram" => { let nr_transaction = newrelic_logger.web_transaction("/speak_spectrogram"); - let result = speak_proxy(client_ip, req, router, nr_transaction, "/speak_spectrogram").await; + let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak_spectrogram").await; match result { Ok(response) => { Ok(response) diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index 076600b..60700e1 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -35,30 +35,54 @@ struct RequestDetails { pub speaker: String, } -/*pub fn copy_request(request: &Request) -> Request { - let body = hyper::body::to_bytes(request.body()); +pub fn copy_request(request: &Request) -> Request { + let mut cloned = Request::builder() + .body(Body::from("")) + .unwrap(); + + request.clone_into(&mut &cloned); + + + /*let body_bytes = hyper::body::to_bytes(request.body()); + let body = Body::from(body_bytes); + Body::from(request.body()); + Request::builder() .uri(request.uri()) .method(request.method()) - .unwrap() -}*/ + .body(body) + .unwrap()*/ -/*pub fn speak_proxy_with_retry( + cloned +} + +pub async fn speak_proxy_with_retry( + ip_addr: IpAddr, req: Request, - remote_addr: SocketAddr, router: Arc, - transaction: MaybeNewRelicTransaction, - endpoint: &'static str) -> BoxFut + _transaction: MaybeNewRelicTransaction, + endpoint: &'static str) -> Result, ResponseError> { - speak_proxy(req, remote_addr, router, transaction, endpoint) - .then(|result| { - match result { - Ok(t) => future::ok(t), - Err(t) => future::err(t), - } - }) - .boxed() -}*/ + let mut attempt = 0; + + loop { + info!("Attempt #{}", attempt); + + let request = copy_request(&req); + let transaction = MaybeNewRelicTransaction::from_optional(None); + let result = speak_proxy(ip_addr, request, router.clone(), transaction, endpoint).await; + + if result.is_ok() { + return result; + } + + attempt += 1; + + if attempt > 3 { + return Err(ResponseError { reason: "max attempts reached".to_string() }); + } + } +} #[derive(Serialize, Debug)] pub struct ResponseError {