Line data Source code
1 : use std::time::Duration;
2 : use tonic::codegen::StdError;
3 : use tonic::transport::{ClientTlsConfig, Endpoint};
4 : use tonic::{transport::Channel, Status};
5 : use utils::id::{TenantId, TenantTimelineId, TimelineId};
6 :
7 : use proto::{
8 : broker_service_client::BrokerServiceClient, TenantTimelineId as ProtoTenantTimelineId,
9 : };
10 :
11 : // Code generated by protobuf.
12 : pub mod proto {
13 : // Tonic does derives as `#[derive(Clone, PartialEq, ::prost::Message)]`
14 : // we don't use these types for anything but broker data transmission,
15 : // so it's ok to ignore this one.
16 : #![allow(clippy::derive_partial_eq_without_eq)]
17 : tonic::include_proto!("storage_broker");
18 : }
19 :
20 : pub mod metrics;
21 :
22 : // Re-exports to avoid direct tonic dependency in user crates.
23 : pub use tonic::Code;
24 : pub use tonic::Request;
25 : pub use tonic::Streaming;
26 :
27 : pub use hyper::Uri;
28 :
29 : pub const DEFAULT_LISTEN_ADDR: &str = "127.0.0.1:50051";
30 : pub const DEFAULT_ENDPOINT: &str = const_format::formatcp!("http://{DEFAULT_LISTEN_ADDR}");
31 :
32 : pub const DEFAULT_KEEPALIVE_INTERVAL: &str = "5000 ms";
33 : pub const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_millis(5000);
34 :
35 : // BrokerServiceClient charged with tonic provided Channel transport; helps to
36 : // avoid depending on tonic directly in user crates.
37 : pub type BrokerClientChannel = BrokerServiceClient<Channel>;
38 :
39 : // Create connection object configured to run TLS if schema starts with https://
40 : // and plain text otherwise. Connection is lazy, only endpoint sanity is
41 : // validated here.
42 : //
43 : // NB: this function is not async, but still must be run on a tokio runtime thread
44 : // because that's a requirement of tonic_endpoint.connect_lazy()'s Channel::new call.
45 0 : pub fn connect<U>(endpoint: U, keepalive_interval: Duration) -> anyhow::Result<BrokerClientChannel>
46 0 : where
47 0 : U: std::convert::TryInto<Uri>,
48 0 : U::Error: std::error::Error + Send + Sync + 'static,
49 0 : {
50 0 : let uri: Uri = endpoint.try_into()?;
51 0 : let mut tonic_endpoint: Endpoint = uri.into();
52 : // If schema starts with https, start encrypted connection; do plain text
53 : // otherwise.
54 0 : if let Some("https") = tonic_endpoint.uri().scheme_str() {
55 0 : let tls = ClientTlsConfig::new();
56 0 : tonic_endpoint = tonic_endpoint.tls_config(tls)?;
57 0 : }
58 0 : tonic_endpoint = tonic_endpoint
59 0 : .http2_keep_alive_interval(keepalive_interval)
60 0 : .keep_alive_while_idle(true)
61 0 : .connect_timeout(DEFAULT_CONNECT_TIMEOUT);
62 0 : // keep_alive_timeout is 20s by default on both client and server side
63 0 : let channel = tonic_endpoint.connect_lazy();
64 0 : Ok(BrokerClientChannel::new(channel))
65 0 : }
66 :
67 : impl BrokerClientChannel {
68 : /// Create a new client to the given endpoint, but don't actually connect until the first request.
69 0 : pub async fn connect_lazy<D>(dst: D) -> Result<Self, tonic::transport::Error>
70 0 : where
71 0 : D: std::convert::TryInto<tonic::transport::Endpoint>,
72 0 : D::Error: Into<StdError>,
73 0 : {
74 0 : let conn = tonic::transport::Endpoint::new(dst)?.connect_lazy();
75 0 : Ok(Self::new(conn))
76 0 : }
77 : }
78 :
79 : // parse variable length bytes from protobuf
80 2 : pub fn parse_proto_ttid(proto_ttid: &ProtoTenantTimelineId) -> Result<TenantTimelineId, Status> {
81 2 : let tenant_id = TenantId::from_slice(&proto_ttid.tenant_id)
82 2 : .map_err(|e| Status::new(Code::InvalidArgument, format!("malformed tenant_id: {}", e)))?;
83 2 : let timeline_id = TimelineId::from_slice(&proto_ttid.timeline_id).map_err(|e| {
84 0 : Status::new(
85 0 : Code::InvalidArgument,
86 0 : format!("malformed timeline_id: {}", e),
87 0 : )
88 2 : })?;
89 2 : Ok(TenantTimelineId {
90 2 : tenant_id,
91 2 : timeline_id,
92 2 : })
93 2 : }
|