clone request

This commit is contained in:
Brandon Thomas
2020-09-08 00:44:52 -04:00
parent 35687652e7
commit fc99fd9182
2 changed files with 46 additions and 20 deletions
+5 -3
View File
@@ -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)
+41 -17
View File
@@ -35,30 +35,54 @@ struct RequestDetails {
pub speaker: String,
}
/*pub fn copy_request(request: &Request<Body>) -> Request<Body> {
let body = hyper::body::to_bytes(request.body());
pub fn copy_request(request: &Request<Body>) -> Request<Body> {
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<Body>,
remote_addr: SocketAddr,
router: Arc<Router>,
transaction: MaybeNewRelicTransaction,
endpoint: &'static str) -> BoxFut
_transaction: MaybeNewRelicTransaction,
endpoint: &'static str) -> Result<Response<Body>, 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 {