-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for Reconnect #566
Comments
Or if someone would be willing to give me some hints, I'd be more than happy to create a PR for an example :) |
Does this help: use std::{
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use tower::{reconnect::Reconnect, service_fn, BoxError, Service, ServiceExt};
#[tokio::main]
async fn main() {
let svc = MyService::default();
let make_service = service_fn(|_target: ()| {
// the future produced by the service must be `Unpin`
Box::pin(async {
println!("reconnecting");
tokio::time::sleep(Duration::from_secs(2)).await;
Ok::<_, BoxError>(svc)
})
});
let mut svc = Reconnect::new::<MyService, String>(make_service, ());
loop {
let response = svc.ready().await.unwrap().call(()).await.unwrap();
dbg!(&response);
}
}
#[derive(Debug, Copy, Clone, Default)]
struct MyService {
counter: usize,
}
impl Service<()> for MyService {
type Response = String;
type Error = BoxError;
type Future = Pin<
Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>,
>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
if self.counter < 10 {
self.counter += 1;
Poll::Ready(Ok(()))
} else {
// `MyService` dies after 10 requests
Poll::Ready(Err(BoxError::from("dead".to_string())))
}
}
fn call(&mut self, _request: ()) -> Self::Future {
let count = self.counter;
Box::pin(async move { Ok(format!("request {}", count)) })
}
}
|
Hey David, sorry for being slow on responding, been AFK. Thank you! I'll take a look at this asap. |
@fredrik-jansson-se I'll close this for now. Feel free to re-open if you have more questions :) |
Hi!
I've tried to use Reconnect on top of a tonic (gRPC) client, but I really cannot figure out how to do it. :(
Would you consider adding an example on how to use Reconnect, could be as simple as reconnecting a TCPStream?
The text was updated successfully, but these errors were encountered: