|
| 1 | +// Copyright 2019 The TiKV Project Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +use serde_derive::*; |
| 15 | +use std::{path::PathBuf, time::Duration}; |
| 16 | + |
| 17 | +/// The configuration for either a [`raw::Client`](raw/struct.Client.html) or a |
| 18 | +/// [`transaction::Client`](transaction/struct.Client.html). |
| 19 | +/// |
| 20 | +/// Because TiKV is managed by a [PD](https://github.com/pingcap/pd/) cluster, the endpoints for PD |
| 21 | +/// must be provided, **not** the TiKV nodes. |
| 22 | +/// |
| 23 | +/// It's important to **include more than one PD endpoint** (include all, if possible!) |
| 24 | +/// This helps avoid having a *single point of failure*. |
| 25 | +/// |
| 26 | +/// By default, this client will use an insecure connection over instead of one protected by |
| 27 | +/// Transport Layer Security (TLS). Your deployment may have chosen to rely on security measures |
| 28 | +/// such as a private network, or a VPN layer to provid secure transmission. |
| 29 | +/// |
| 30 | +/// To use a TLS secured connection, use the `with_security` function to set the required |
| 31 | +/// parameters. |
| 32 | +/// |
| 33 | +/// TiKV does not currently offer encrypted storage (or encryption-at-rest). |
| 34 | +#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] |
| 35 | +#[serde(default)] |
| 36 | +#[serde(rename_all = "kebab-case")] |
| 37 | +pub struct Config { |
| 38 | + pub(crate) pd_endpoints: Vec<String>, |
| 39 | + pub(crate) ca_path: Option<PathBuf>, |
| 40 | + pub(crate) cert_path: Option<PathBuf>, |
| 41 | + pub(crate) key_path: Option<PathBuf>, |
| 42 | + pub(crate) timeout: Duration, |
| 43 | +} |
| 44 | + |
| 45 | +const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(2); |
| 46 | + |
| 47 | +impl Config { |
| 48 | + /// Create a new [`Config`](struct.Config.html) which coordinates with the given PD endpoints. |
| 49 | + /// |
| 50 | + /// It's important to **include more than one PD endpoint** (include all, if possible!) |
| 51 | + /// This helps avoid having a *single point of failure*. |
| 52 | + /// |
| 53 | + /// ```rust |
| 54 | + /// # use tikv_client::Config; |
| 55 | + /// let config = Config::new(vec!["192.168.0.100:2379", "192.168.0.101:2379"]); |
| 56 | + /// ``` |
| 57 | + pub fn new(pd_endpoints: impl IntoIterator<Item = impl Into<String>>) -> Self { |
| 58 | + Config { |
| 59 | + pd_endpoints: pd_endpoints.into_iter().map(Into::into).collect(), |
| 60 | + ca_path: None, |
| 61 | + cert_path: None, |
| 62 | + key_path: None, |
| 63 | + timeout: DEFAULT_REQUEST_TIMEOUT, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Set the certificate authority, certificate, and key locations for the |
| 68 | + /// [`Config`](struct.Config.html). |
| 69 | + /// |
| 70 | + /// By default, TiKV connections do not utilize transport layer security. Enable it by setting |
| 71 | + /// these values. |
| 72 | + /// |
| 73 | + /// ```rust |
| 74 | + /// # use tikv_client::Config; |
| 75 | + /// let config = Config::new(vec!["192.168.0.100:2379", "192.168.0.101:2379"]) |
| 76 | + /// .with_security("root.ca", "internal.cert", "internal.key"); |
| 77 | + /// ``` |
| 78 | + pub fn with_security( |
| 79 | + mut self, |
| 80 | + ca_path: impl Into<PathBuf>, |
| 81 | + cert_path: impl Into<PathBuf>, |
| 82 | + key_path: impl Into<PathBuf>, |
| 83 | + ) -> Self { |
| 84 | + self.ca_path = Some(ca_path.into()); |
| 85 | + self.cert_path = Some(cert_path.into()); |
| 86 | + self.key_path = Some(key_path.into()); |
| 87 | + self |
| 88 | + } |
| 89 | + |
| 90 | + pub fn timeout(mut self, timeout: Duration) -> Self { |
| 91 | + self.timeout = timeout; |
| 92 | + self |
| 93 | + } |
| 94 | +} |
0 commit comments