-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): use a middleware to retry closed connection instead
- Loading branch information
Showing
4 changed files
with
76 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::io; | ||
|
||
use crate::{ | ||
error::Error, | ||
response::Response, | ||
service::{Service, ServiceRequest}, | ||
}; | ||
|
||
/// middleware for retrying closed connection | ||
pub struct RetryClosedConnection<S> { | ||
service: S, | ||
} | ||
|
||
impl<S> RetryClosedConnection<S> { | ||
pub fn new(service: S) -> Self { | ||
Self { service } | ||
} | ||
} | ||
|
||
impl<'r, 'c, S> Service<ServiceRequest<'r, 'c>> for RetryClosedConnection<S> | ||
where | ||
S: for<'r2, 'c2> Service<ServiceRequest<'r2, 'c2>, Response = Response, Error = Error> + Send + Sync, | ||
{ | ||
type Response = Response; | ||
type Error = Error; | ||
|
||
async fn call(&self, req: ServiceRequest<'r, 'c>) -> Result<Self::Response, Self::Error> { | ||
let ServiceRequest { req, client, timeout } = req; | ||
let headers = req.headers().clone(); | ||
let method = req.method().clone(); | ||
let uri = req.uri().clone(); | ||
|
||
loop { | ||
let res = self.service.call(ServiceRequest { req, client, timeout }).await; | ||
|
||
match res { | ||
Err(Error::Io(err)) => { | ||
if err.kind() != io::ErrorKind::UnexpectedEof { | ||
return Err(Error::Io(err)); | ||
} | ||
} | ||
Err(Error::H1(crate::h1::Error::Io(err))) => { | ||
if err.kind() != io::ErrorKind::UnexpectedEof { | ||
return Err(Error::H1(crate::h1::Error::Io(err))); | ||
} | ||
} | ||
Err(Error::H2(crate::h2::Error::Io(err))) => { | ||
if err.kind() != io::ErrorKind::UnexpectedEof { | ||
return Err(Error::H2(crate::h2::Error::Io(err))); | ||
} | ||
} | ||
Err(Error::H3(crate::h3::Error::Io(err))) => { | ||
if err.kind() != io::ErrorKind::UnexpectedEof { | ||
return Err(Error::H3(crate::h3::Error::Io(err))); | ||
} | ||
} | ||
res => return res, | ||
} | ||
|
||
*req.uri_mut() = uri.clone(); | ||
*req.method_mut() = method.clone(); | ||
*req.headers_mut() = headers.clone(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters