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