1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-21 11:54:47 +01:00

Fix formatting

This commit is contained in:
joshbenaron 2021-04-02 19:06:39 +01:00
parent 22e40613e8
commit e87e636e84

View File

@ -60,8 +60,8 @@ impl Retry {
/// .finish(); /// .finish();
///``` ///```
pub fn policy<T>(mut self, p: T) -> Self pub fn policy<T>(mut self, p: T) -> Self
where where
T: IntoRetryPolicy, T: IntoRetryPolicy,
{ {
self.0.policies.push(p.into_policy()); self.0.policies.push(p.into_policy());
self self
@ -79,8 +79,8 @@ pub trait IntoRetryPolicy {
} }
impl<T> IntoRetryPolicy for T impl<T> IntoRetryPolicy for T
where where
T: for<'a> Fn(StatusCode, &'a HeaderMap) -> bool + 'static, T: for<'a> Fn(StatusCode, &'a HeaderMap) -> bool + 'static,
{ {
fn into_policy(self) -> RetryPolicy { fn into_policy(self) -> RetryPolicy {
RetryPolicy::Custom(Box::new(self)) RetryPolicy::Custom(Box::new(self))
@ -94,8 +94,8 @@ impl IntoRetryPolicy for Vec<StatusCode> {
} }
impl<S> Transform<S, ConnectRequest> for Retry impl<S> Transform<S, ConnectRequest> for Retry
where where
S: Service<ConnectRequest, Response=ConnectResponse, Error=SendRequestError> + 'static, S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
{ {
type Transform = RetryService<S>; type Transform = RetryService<S>;
@ -116,8 +116,8 @@ pub struct RetryService<S> {
} }
impl<S> Service<ConnectRequest> for RetryService<S> impl<S> Service<ConnectRequest> for RetryService<S>
where where
S: Service<ConnectRequest, Response=ConnectResponse, Error=SendRequestError> + 'static, S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
{ {
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
@ -135,7 +135,7 @@ impl<S> Service<ConnectRequest> for RetryService<S>
Box::pin(async move { Box::pin(async move {
match req { match req {
ConnectRequest::Client(head, body, addr) => { ConnectRequest::Client(head, body, addr) => {
for _ in 1..max_retry { for _ in 0..max_retry {
let h = clone_request_head_type(&head); let h = clone_request_head_type(&head);
let result = connector let result = connector
@ -168,11 +168,8 @@ impl<S> Service<ConnectRequest> for RetryService<S>
// Exceed max retry so just return whatever response is received // Exceed max retry so just return whatever response is received
log::debug!("Request max retry reached"); log::debug!("Request max retry reached");
connector.call(ConnectRequest::Client( connector
head, .call(ConnectRequest::Client(head, body, addr))
body,
addr,
))
.await .await
} }
ConnectRequest::Tunnel(head, addr) => { ConnectRequest::Tunnel(head, addr) => {
@ -189,13 +186,14 @@ impl<S> Service<ConnectRequest> for RetryService<S>
} }
} }
ConnectResponse::Tunnel(head, _) => { ConnectResponse::Tunnel(head, _) => {
if is_valid_response(&policies, head.status, head.headers()) { if is_valid_response(&policies, head.status, head.headers())
{
return Ok(res); return Ok(res);
} }
} }
} }
} }
}; }
// Exceed max retry so just return whatever response is received // Exceed max retry so just return whatever response is received
log::debug!("Request max retry reached"); log::debug!("Request max retry reached");
@ -210,7 +208,7 @@ fn body_to_retry_body(body: &Body) -> Body {
match body { match body {
Body::Empty => Body::Empty, Body::Empty => Body::Empty,
Body::Bytes(b) => Body::Bytes(b.clone()), Body::Bytes(b) => Body::Bytes(b.clone()),
_ => Body::None _ => Body::None,
} }
} }
@ -280,12 +278,9 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_basic_policy() { async fn test_basic_policy() {
std::env::set_var("RUST_LOG", "RUST_LOG=debug,a=debug");
env_logger::init();
let client = ClientBuilder::new() let client = ClientBuilder::new()
.disable_redirects() .disable_redirects()
.wrap(Retry::new(3).policy(vec![StatusCode::INTERNAL_SERVER_ERROR])) .wrap(Retry::new(1).policy(vec![StatusCode::INTERNAL_SERVER_ERROR]))
.finish(); .finish();
let srv = actix_test::start(|| { let srv = actix_test::start(|| {
@ -304,7 +299,7 @@ mod tests {
let client = ClientBuilder::new() let client = ClientBuilder::new()
.disable_redirects() .disable_redirects()
.wrap( .wrap(
Retry::new(3).policy(|code: StatusCode, headers: &HeaderMap| { Retry::new(2).policy(|code: StatusCode, headers: &HeaderMap| {
code.is_success() && headers.contains_key("SOME_HEADER") code.is_success() && headers.contains_key("SOME_HEADER")
}), }),
) )