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_PRIMARY_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 /// Specifies the number of retries the driver performs to find and reach the cluster primary
51 /// after a failed request, before giving up. Total attempts per user request = `N + 1`.
52 /// Each retry either follows the server's redirect address (fast path) or polls the
53 /// known replicas with a 2-second sleep between polls (slow path).
54 /// Set to `0` to disable failover. Defaults to 1.
55 pub primary_failover_retries: usize,
56}
57
58impl DriverOptions {
59 /// Creates new `DriverOptions` to configure connections to TypeDB using custom TLS settings.
60 /// WARNING: Disabled TLS settings will make the driver sending passwords as plaintext.
61 pub fn new(tls_config: DriverTlsConfig) -> Self {
62 Self { tls_config, ..Default::default() }
63 }
64
65 /// Override the existing TLS configuration.
66 /// WARNING: Disabled TLS settings will make the driver sending passwords as plaintext.
67 pub fn tls_config(self, tls_config: DriverTlsConfig) -> Self {
68 Self { tls_config, ..self }
69 }
70
71 /// Specifies the maximum time to wait for a response to a unary RPC request.
72 /// This applies to operations like database creation, user management, and initial
73 /// transaction opening. It does NOT apply to operations within transactions (queries, commits).
74 /// Defaults to 2 hours.
75 pub fn request_timeout(self, request_timeout: Duration) -> Self {
76 Self { request_timeout, ..self }
77 }
78
79 /// Specifies the number of retries the driver performs to find and reach the cluster primary
80 /// after a failed request, before giving up. Total attempts per user request = `N + 1`.
81 /// Each retry either follows the server's redirect address (fast path) or polls the
82 /// known replicas with a 2-second sleep between polls (slow path).
83 /// Set to `0` to disable failover. Defaults to 1.
84 pub fn primary_failover_retries(self, primary_failover_retries: usize) -> Self {
85 Self { primary_failover_retries, ..self }
86 }
87}
88
89impl Default for DriverOptions {
90 fn default() -> Self {
91 Self {
92 tls_config: DriverTlsConfig::default(),
93 request_timeout: DEFAULT_REQUEST_TIMEOUT,
94 primary_failover_retries: DEFAULT_PRIMARY_FAILOVER_RETRIES,
95 }
96 }
97}