Skip to main content

typedb_driver/connection/
driver_options.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::time::Duration;
21
22use crate::connection::driver_tls_config::DriverTlsConfig;
23
24// When changing these numbers, also update docs in DriverOptions
25const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(2 * 60 * 60); // 2 hours
26const DEFAULT_REDIRECT_FAILOVER_RETRIES: usize = 1;
27
28/// TypeDB driver connection options.
29/// `DriverOptions` object can be used to override the default driver behavior while connecting to
30/// TypeDB.
31///
32/// # Examples
33///
34/// ```rust
35/// let options = DriverOptions::new(DriverTlsConfig::default()).request_timeout(Duration::from_secs(30));
36/// ```
37#[derive(Debug, Clone)]
38pub struct DriverOptions {
39    /// Specifies the TLS configuration of the connection to TypeDB.
40    /// WARNING: Disabled TLS settings will make the driver sending passwords as plaintext.
41    /// Defaults to an enabled TLS configuration based on the system's native trust roots.
42    pub tls_config: DriverTlsConfig,
43    /// Specifies the maximum time to wait for a response to a unary RPC request.
44    /// This applies to operations like database creation, user management, and initial
45    /// transaction opening. It does NOT apply to operations within transactions (queries, commits).
46    /// Defaults to 2 hours.
47    // TODO: What if the server does not respond on queries/commits?
48    // Shall we apply the same or a different timeout?
49    pub request_timeout: Duration,
50    /// Sets the number of times the driver retries finding and re-routing to the primary server
51    /// on connection failures. This value is used both for polling during leader election (up to
52    /// N+1 attempts with a 2-second sleep between each) and for re-executing a failed request on
53    /// a newly discovered primary. Defaults to 1.
54    pub primary_failover_retries: usize,
55}
56
57impl DriverOptions {
58    /// Creates new `DriverOptions` to configure connections to TypeDB using custom TLS settings.
59    /// WARNING: Disabled TLS settings will make the driver sending passwords as plaintext.
60    pub fn new(tls_config: DriverTlsConfig) -> Self {
61        Self { tls_config, ..Default::default() }
62    }
63
64    /// Override the existing TLS configuration.
65    /// WARNING: Disabled TLS settings will make the driver sending passwords as plaintext.
66    pub fn tls_config(self, tls_config: DriverTlsConfig) -> Self {
67        Self { tls_config, ..self }
68    }
69
70    /// Specifies the maximum time to wait for a response to a unary RPC request.
71    /// This applies to operations like database creation, user management, and initial
72    /// transaction opening. It does NOT apply to operations within transactions (queries, commits).
73    /// Defaults to 2 hours.
74    pub fn request_timeout(self, request_timeout: Duration) -> Self {
75        Self { request_timeout, ..self }
76    }
77
78    /// Sets the number of times the driver retries finding and re-routing to the primary server
79    /// on connection failures. This value is used both for polling during leader election (up to
80    /// N+1 attempts with a 2-second sleep between each) and for re-executing a failed request on
81    /// a newly discovered primary. Defaults to 1.
82    pub fn primary_failover_retries(self, primary_failover_retries: usize) -> Self {
83        Self { primary_failover_retries, ..self }
84    }
85}
86
87impl Default for DriverOptions {
88    fn default() -> Self {
89        Self {
90            tls_config: DriverTlsConfig::default(),
91            request_timeout: DEFAULT_REQUEST_TIMEOUT,
92            primary_failover_retries: DEFAULT_REDIRECT_FAILOVER_RETRIES,
93        }
94    }
95}