mz_service/
params.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mz_proto::{ProtoType, RustType, TryFromProtoError};
11use proptest_derive::Arbitrary;
12use serde::{Deserialize, Serialize};
13use std::time::Duration;
14
15include!(concat!(env!("OUT_DIR"), "/mz_service.params.rs"));
16
17/// gRPC client parameters.
18#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, Arbitrary)]
19pub struct GrpcClientParameters {
20    /// Timeout to apply to initial connection establishment.
21    pub connect_timeout: Option<Duration>,
22    /// Time waited after the last received HTTP/2 frame before sending
23    /// a keep-alive PING to the server. Note that this is an HTTP/2
24    /// keep-alive, and is separate from TCP keep-alives.
25    pub http2_keep_alive_interval: Option<Duration>,
26    /// Time waited without response after a keep-alive PING before
27    /// terminating the connection.
28    pub http2_keep_alive_timeout: Option<Duration>,
29}
30
31impl GrpcClientParameters {
32    /// Update the parameter values with the set ones from `other`.
33    pub fn update(&mut self, other: Self) {
34        let Self {
35            connect_timeout,
36            http2_keep_alive_interval,
37            http2_keep_alive_timeout,
38        } = self;
39        let Self {
40            connect_timeout: other_connect_timeout,
41            http2_keep_alive_interval: other_http2_keep_alive_interval,
42            http2_keep_alive_timeout: other_http2_keep_alive_timeout,
43        } = other;
44
45        if let Some(v) = other_connect_timeout {
46            *connect_timeout = Some(v);
47        }
48        if let Some(v) = other_http2_keep_alive_interval {
49            *http2_keep_alive_interval = Some(v);
50        }
51        if let Some(v) = other_http2_keep_alive_timeout {
52            *http2_keep_alive_timeout = Some(v);
53        }
54    }
55
56    /// Return whether all parameters are unset.
57    pub fn all_unset(&self) -> bool {
58        *self == Self::default()
59    }
60}
61
62impl RustType<ProtoGrpcClientParameters> for GrpcClientParameters {
63    fn into_proto(&self) -> ProtoGrpcClientParameters {
64        ProtoGrpcClientParameters {
65            connect_timeout: self.connect_timeout.into_proto(),
66            http2_keep_alive_interval: self.http2_keep_alive_interval.into_proto(),
67            http2_keep_alive_timeout: self.http2_keep_alive_timeout.into_proto(),
68        }
69    }
70
71    fn from_proto(proto: ProtoGrpcClientParameters) -> Result<Self, TryFromProtoError> {
72        Ok(Self {
73            connect_timeout: proto.connect_timeout.into_rust()?,
74            http2_keep_alive_interval: proto.http2_keep_alive_interval.into_rust()?,
75            http2_keep_alive_timeout: proto.http2_keep_alive_timeout.into_rust()?,
76        })
77    }
78}