Repository: hyperium/tonic (Rust)

Issue:
Endpoint::connect_lazy should not return a Result<>
## Feature Request

### Crates

`transport`

### Motivation

Most of the object creation and error validation is already done in `Endpoint::new` and as such `Endpoint::connect_lazy` should not be returning any errors since the connection is evaluated on-demand and cannot fail when building a channel. Meanwhile the API gives the wrong impression that the call may fail (does it ping the service beforehand? who knows)

### Proposal

Change the return signature for `connect_lazy` to return the `Channel` to better indicate via API that this call can not fail due to connection errors.

### Alternatives

Add a doc comment specifying that this function never fails when invoked.



Discussion:
I have had the same thought. @LucioFranco do you know if there is a reason it currently returns a result? Future proofing perhaps? 🤷 
Yeah, this should have been fixed in 0.5. For now the API is unfalliable and unwraping there is safe from a panic. We should remove it for the next iteration.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ad45de1f1..3b2a0b48b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -57,7 +57,6 @@ This release includes a new crate `tonic-web` which is versioned at `0.1` and su
 * **transport:** provide generic access to connect info ([#647](https://github.com/hyperium/tonic/issues/647)) ([e5e3118](https://github.com/hyperium/tonic/commit/e5e311853bff347355722bc829d40f54e8954aee))
 
 
-
 # [0.4.3](https://github.com/hyperium/tonic/compare/v0.4.2...v0.4.3) (2021-04-29)
 
 ### Features
diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs
index bc777502d..e0a4965f3 100644
--- a/tonic/src/transport/channel/endpoint.rs
+++ b/tonic/src/transport/channel/endpoint.rs
@@ -284,7 +284,7 @@ impl Endpoint {
     ///
     /// The channel returned by this method does not attempt to connect to the endpoint until first
     /// use.
-    pub fn connect_lazy(&self) -> Result<Channel, Error> {
+    pub fn connect_lazy(&self) -> Channel {
         let mut http = hyper::client::connect::HttpConnector::new();
         http.enforce_http(false);
         http.set_nodelay(self.tcp_nodelay);
@@ -299,9 +299,9 @@ impl Endpoint {
         if let Some(connect_timeout) = self.connect_timeout {
             let mut connector = hyper_timeout::TimeoutConnector::new(connector);
             connector.set_connect_timeout(Some(connect_timeout));
-            Ok(Channel::new(connector, self.clone()))
+            Channel::new(connector, self.clone())
         } else {
-            Ok(Channel::new(connector, self.clone()))
+            Channel::new(connector, self.clone())
         }
     }
 


Test patch:
diff --git a/tests/integration_tests/tests/connection.rs b/tests/integration_tests/tests/connection.rs
index c345a35d9..f27ef09d1 100644
--- a/tests/integration_tests/tests/connection.rs
+++ b/tests/integration_tests/tests/connection.rs
@@ -63,9 +63,7 @@ async fn connect_lazy_reconnects_after_first_failure() {
     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 channel = Endpoint::from_static("http://127.0.0.1:1339").connect_lazy();
 
     let mut client = TestClient::new(channel);