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 anyhow;
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
@@ -8,7 +10,7 @@ pub mod speak_proxy;
use anyhow::Result as AnyhowResult; use anyhow::Result as AnyhowResult;
use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction};
use futures::future::{self, Future}; 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::{Method, StatusCode};
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use hyper::http::header::HeaderName; use hyper::http::header::HeaderName;
@@ -135,7 +137,7 @@ pub async fn handle(
match req.uri().path() { match req.uri().path() {
"/speak" => { "/speak" => {
let nr_transaction = newrelic_logger.web_transaction("/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 { match result {
Ok(response) => { Ok(response) => {
Ok(response) Ok(response)
@@ -150,7 +152,7 @@ pub async fn handle(
}, },
"/speak_spectrogram" => { "/speak_spectrogram" => {
let nr_transaction = newrelic_logger.web_transaction("/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 { match result {
Ok(response) => { Ok(response) => {
Ok(response) Ok(response)
+40 -16
View File
@@ -35,30 +35,54 @@ struct RequestDetails {
pub speaker: String, pub speaker: String,
} }
/*pub fn copy_request(request: &Request<Body>) -> Request<Body> { pub fn copy_request(request: &Request<Body>) -> Request<Body> {
let body = hyper::body::to_bytes(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() Request::builder()
.uri(request.uri()) .uri(request.uri())
.method(request.method()) .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>, req: Request<Body>,
remote_addr: SocketAddr,
router: Arc<Router>, router: Arc<Router>,
transaction: MaybeNewRelicTransaction, _transaction: MaybeNewRelicTransaction,
endpoint: &'static str) -> BoxFut endpoint: &'static str) -> Result<Response<Body>, ResponseError>
{ {
speak_proxy(req, remote_addr, router, transaction, endpoint) let mut attempt = 0;
.then(|result| {
match result { loop {
Ok(t) => future::ok(t), info!("Attempt #{}", attempt);
Err(t) => future::err(t),
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;
} }
})
.boxed() attempt += 1;
}*/
if attempt > 3 {
return Err(ResponseError { reason: "max attempts reached".to_string() });
}
}
}
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct ResponseError { pub struct ResponseError {