linera_rpc/grpc/
transport.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::NodeOptions;
5
6#[derive(Clone, Debug, Default)]
7pub struct Options {
8    pub connect_timeout: Option<linera_base::time::Duration>,
9    pub timeout: Option<linera_base::time::Duration>,
10}
11
12impl From<&'_ NodeOptions> for Options {
13    fn from(node_options: &NodeOptions) -> Self {
14        Self {
15            connect_timeout: Some(node_options.send_timeout),
16            timeout: Some(node_options.recv_timeout),
17        }
18    }
19}
20
21cfg_if::cfg_if! {
22    if #[cfg(web)] {
23        pub use tonic_web_wasm_client::{Client as Channel, Error};
24
25        pub fn create_channel(address: String, _options: &Options) -> Result<Channel, Error> {
26            // TODO(#1817): this should respect `options`
27            Ok(tonic_web_wasm_client::Client::new(address))
28        }
29    } else {
30        pub use tonic::transport::{Channel, Error};
31
32        pub fn create_channel(
33            address: String,
34            options: &Options,
35        ) -> Result<Channel, Error> {
36            let mut endpoint = tonic::transport::Endpoint::from_shared(address)?
37                .tls_config(tonic::transport::channel::ClientTlsConfig::default().with_webpki_roots())?;
38
39            if let Some(timeout) = options.connect_timeout {
40                endpoint = endpoint.connect_timeout(timeout);
41            }
42            if let Some(timeout) = options.timeout {
43                endpoint = endpoint.timeout(timeout);
44            }
45            Ok(endpoint.connect_lazy())
46        }
47    }
48}