fix(transport): reconnect lazy connections after first failure (#458)

* fix(transport): reconnect lazy connections after first failure

Channels created with lazy connections never try to reconnect if the
first connection attempt fails. This is because `Reconnect` returns
`Poll::Ready(Err)` on poll_ready and the service is considered dead.

This change passes a flag to Reconnect to signal if the connection
is intended to be lazy, in which case reconnect returns the error on
the next call.

fixes #452
This commit is contained in:
Juan Alvarez
2020-09-23 09:35:42 -05:00
committed by GitHub
parent cea990b1ea
commit e9910d10a7
5 changed files with 75 additions and 26 deletions
+50 -13
View File
@@ -3,7 +3,22 @@ use integration_tests::pb::{test_client::TestClient, test_server, Input, Output}
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::oneshot;
use tonic::{transport::Server, Request, Response, Status};
use tonic::{
transport::{Endpoint, Server},
Request, Response, Status,
};
struct Svc(Arc<Mutex<Option<oneshot::Sender<()>>>>);
#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
let mut l = self.0.lock().unwrap();
l.take().unwrap().send(()).unwrap();
Ok(Response::new(Output {}))
}
}
#[tokio::test]
async fn connect_returns_err() {
@@ -14,18 +29,6 @@ async fn connect_returns_err() {
#[tokio::test]
async fn connect_returns_err_via_call_after_connected() {
struct Svc(Arc<Mutex<Option<oneshot::Sender<()>>>>);
#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
let mut l = self.0.lock().unwrap();
l.take().unwrap().send(()).unwrap();
Ok(Response::new(Output {}))
}
}
let (tx, rx) = oneshot::channel();
let sender = Arc::new(Mutex::new(Some(tx)));
let svc = test_server::TestServer::new(Svc(sender));
@@ -53,3 +56,37 @@ async fn connect_returns_err_via_call_after_connected() {
jh.await.unwrap();
}
#[tokio::test]
async fn connect_lazy_reconnects_after_first_failure() {
let (tx, rx) = oneshot::channel();
let sender = Arc::new(Mutex::new(Some(tx)));
let svc = test_server::TestServer::new(Svc(sender));
let channel = Endpoint::from_static("http://127.0.0.1:1339")
.connect_lazy()
.unwrap();
let mut client = TestClient::new(channel);
// First call should fail, the server is not running
client.unary_call(Request::new(Input {})).await.unwrap_err();
// Start the server now, second call should succeed
let jh = tokio::spawn(async move {
Server::builder()
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1339".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});
tokio::time::delay_for(Duration::from_millis(100)).await;
client.unary_call(Request::new(Input {})).await.unwrap();
// The server shut down, third call should fail
tokio::time::delay_for(Duration::from_millis(100)).await;
client.unary_call(Request::new(Input {})).await.unwrap_err();
jh.await.unwrap();
}