Skip to main content

tikv_client/
config.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3use std::path::PathBuf;
4use std::time::Duration;
5
6use serde_derive::Deserialize;
7use serde_derive::Serialize;
8
9/// The configuration for either a [`RawClient`](crate::RawClient) or a
10/// [`TransactionClient`](crate::TransactionClient).
11///
12/// See also [`TransactionOptions`](crate::TransactionOptions) which provides more ways to configure
13/// requests.
14///
15/// This struct is marked `#[non_exhaustive]` to allow adding new configuration options in the
16/// future without breaking downstream code. Construct it via [`Config::default`] and then use the
17/// `with_*` methods (or field assignment) to customize it.
18#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
19#[serde(default)]
20#[serde(rename_all = "kebab-case")]
21#[non_exhaustive]
22pub struct Config {
23    pub ca_path: Option<PathBuf>,
24    pub cert_path: Option<PathBuf>,
25    pub key_path: Option<PathBuf>,
26    pub timeout: Duration,
27    pub grpc_max_decoding_message_size: usize,
28    pub keyspace: Option<String>,
29}
30
31const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(2);
32const DEFAULT_GRPC_MAX_DECODING_MESSAGE_SIZE: usize = 4 * 1024 * 1024; // 4MB
33
34impl Default for Config {
35    fn default() -> Self {
36        Config {
37            ca_path: None,
38            cert_path: None,
39            key_path: None,
40            timeout: DEFAULT_REQUEST_TIMEOUT,
41            grpc_max_decoding_message_size: DEFAULT_GRPC_MAX_DECODING_MESSAGE_SIZE,
42            keyspace: None,
43        }
44    }
45}
46
47impl Config {
48    /// Set the certificate authority, certificate, and key locations for clients.
49    ///
50    /// By default, this client will use an insecure connection over instead of one protected by
51    /// Transport Layer Security (TLS). Your deployment may have chosen to rely on security measures
52    /// such as a private network, or a VPN layer to provide secure transmission.
53    ///
54    /// To use a TLS secured connection, use the `with_security` function to set the required
55    /// parameters.
56    ///
57    /// TiKV does not currently offer encrypted storage (or encryption-at-rest).
58    ///
59    /// # Examples
60    /// ```rust
61    /// # use tikv_client::Config;
62    /// let config = Config::default().with_security("root.ca", "internal.cert", "internal.key");
63    /// ```
64    #[must_use]
65    pub fn with_security(
66        mut self,
67        ca_path: impl Into<PathBuf>,
68        cert_path: impl Into<PathBuf>,
69        key_path: impl Into<PathBuf>,
70    ) -> Self {
71        self.ca_path = Some(ca_path.into());
72        self.cert_path = Some(cert_path.into());
73        self.key_path = Some(key_path.into());
74        self
75    }
76
77    /// Set the timeout for clients.
78    ///
79    /// The timeout is used for all requests when using or connecting to a TiKV cluster (including
80    /// PD nodes). If the request does not complete within timeout, the request is cancelled and
81    /// an error returned to the user.
82    ///
83    /// The default timeout is two seconds.
84    ///
85    /// # Examples
86    /// ```rust
87    /// # use tikv_client::Config;
88    /// # use std::time::Duration;
89    /// let config = Config::default().with_timeout(Duration::from_secs(10));
90    /// ```
91    #[must_use]
92    pub fn with_timeout(mut self, timeout: Duration) -> Self {
93        self.timeout = timeout;
94        self
95    }
96
97    /// Set the maximum decoding message size for gRPC.
98    #[must_use]
99    pub fn with_grpc_max_decoding_message_size(mut self, size: usize) -> Self {
100        self.grpc_max_decoding_message_size = size;
101        self
102    }
103
104    /// Set to use default keyspace.
105    ///
106    /// Server should enable `storage.api-version = 2` to use this feature.
107    #[must_use]
108    pub fn with_default_keyspace(self) -> Self {
109        self.with_keyspace("DEFAULT")
110    }
111
112    /// Set the use keyspace for the client.
113    ///
114    /// Server should enable `storage.api-version = 2` to use this feature.
115    #[must_use]
116    pub fn with_keyspace(mut self, keyspace: &str) -> Self {
117        self.keyspace = Some(keyspace.to_owned());
118        self
119    }
120}