couchbase-core 1.0.1

Couchbase SDK core networking and protocol implementation, not intended for direct use
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 *
 *  * Copyright (c) 2025 Couchbase, Inc.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *    http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

use crate::address::Address;
use crate::auth_mechanism::AuthMechanism;
use crate::authenticator::Authenticator;
use crate::memdx::dispatcher::OrphanResponseHandler;
use crate::tls_config::TlsConfig;
use std::fmt::{Debug, Display};
use std::time::Duration;

#[derive(Clone)]
#[non_exhaustive]
pub struct AgentOptions {
    pub seed_config: SeedConfig,
    pub authenticator: Authenticator,

    // By default, the SDK will default to using the mechanisms provided by the
    // Authenticator, but this can be overridden here.
    pub auth_mechanisms: Vec<AuthMechanism>,
    pub tls_config: Option<TlsConfig>,
    pub bucket_name: Option<String>,
    pub network: Option<String>,

    pub compression_config: CompressionConfig,
    pub config_poller_config: ConfigPollerConfig,
    pub kv_config: KvConfig,
    pub http_config: HttpConfig,
    pub tcp_keep_alive_time: Option<Duration>,
    pub orphan_response_handler: Option<OrphanResponseHandler>,
}

impl Debug for AgentOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AgentOptions")
            .field("seed_config", &self.seed_config)
            .field("auth_mechanisms", &self.auth_mechanisms)
            .field("tls_config", &self.tls_config)
            .field("bucket_name", &self.bucket_name)
            .field("network", &self.network)
            .field("compression_config", &self.compression_config)
            .field("config_poller_config", &self.config_poller_config)
            .field("kv_config", &self.kv_config)
            .field("http_config", &self.http_config)
            .field("tcp_keep_alive_time", &self.tcp_keep_alive_time)
            .finish()
    }
}

impl AgentOptions {
    pub fn new(seed_config: SeedConfig, authenticator: Authenticator) -> Self {
        Self {
            tls_config: None,
            authenticator,
            bucket_name: None,
            network: None,
            seed_config,
            compression_config: CompressionConfig::default(),
            config_poller_config: ConfigPollerConfig::default(),
            auth_mechanisms: vec![],
            kv_config: KvConfig::default(),
            http_config: HttpConfig::default(),
            tcp_keep_alive_time: None,
            orphan_response_handler: None,
        }
    }

    pub fn seed_config(mut self, seed_config: SeedConfig) -> Self {
        self.seed_config = seed_config;
        self
    }

    pub fn authenticator(mut self, authenticator: Authenticator) -> Self {
        self.authenticator = authenticator;
        self
    }

    pub fn tls_config(mut self, tls_config: impl Into<Option<TlsConfig>>) -> Self {
        self.tls_config = tls_config.into();
        self
    }

    pub fn bucket_name(mut self, bucket_name: impl Into<Option<String>>) -> Self {
        self.bucket_name = bucket_name.into();
        self
    }

    pub fn network(mut self, network: impl Into<Option<String>>) -> Self {
        self.network = network.into();
        self
    }

    pub fn compression_config(mut self, compression_config: CompressionConfig) -> Self {
        self.compression_config = compression_config;
        self
    }

    pub fn config_poller_config(mut self, config_poller_config: ConfigPollerConfig) -> Self {
        self.config_poller_config = config_poller_config;
        self
    }

    pub fn auth_mechanisms(mut self, auth_mechanisms: Vec<AuthMechanism>) -> Self {
        self.auth_mechanisms = auth_mechanisms;
        self
    }

    pub fn kv_config(mut self, kv_config: KvConfig) -> Self {
        self.kv_config = kv_config;
        self
    }

    pub fn http_config(mut self, http_config: HttpConfig) -> Self {
        self.http_config = http_config;
        self
    }

    pub fn tcp_keep_alive_time(mut self, tcp_keep_alive: Duration) -> Self {
        self.tcp_keep_alive_time = Some(tcp_keep_alive);
        self
    }

    pub fn orphan_reporter_handler(
        mut self,
        orphan_response_handler: OrphanResponseHandler,
    ) -> Self {
        self.orphan_response_handler = Some(orphan_response_handler);
        self
    }
}

#[derive(Default, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct SeedConfig {
    pub http_addrs: Vec<Address>,
    pub memd_addrs: Vec<Address>,
}

impl SeedConfig {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn http_addrs(mut self, http_addrs: Vec<Address>) -> Self {
        self.http_addrs = http_addrs;
        self
    }

    pub fn memd_addrs(mut self, memd_addrs: Vec<Address>) -> Self {
        self.memd_addrs = memd_addrs;
        self
    }
}

#[derive(Default, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct CompressionConfig {
    pub disable_decompression: bool,
    pub mode: CompressionMode,
}

impl CompressionConfig {
    pub fn new(mode: CompressionMode) -> Self {
        Self {
            disable_decompression: false,
            mode,
        }
    }

    pub fn disable_decompression(mut self, disable_decompression: bool) -> Self {
        self.disable_decompression = disable_decompression;
        self
    }

    pub fn mode(mut self, mode: CompressionMode) -> Self {
        self.mode = mode;
        self
    }
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum CompressionMode {
    Enabled { min_size: usize, min_ratio: f64 },
    Disabled,
}

impl Default for CompressionMode {
    fn default() -> Self {
        Self::Enabled {
            min_size: 32,
            min_ratio: 0.83,
        }
    }
}

impl Display for CompressionMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompressionMode::Enabled {
                min_size,
                min_ratio,
            } => {
                write!(f, "{{ min_size: {}, min_ratio: {} }}", min_size, min_ratio)
            }
            CompressionMode::Disabled => write!(f, "disabled"),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ConfigPollerConfig {
    pub poll_interval: Duration,
    pub fetch_timeout: Duration,
}

impl ConfigPollerConfig {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn poll_interval(mut self, poll_interval: Duration) -> Self {
        self.poll_interval = poll_interval;
        self
    }

    pub fn fetch_timeout(mut self, fetch_timeout: Duration) -> Self {
        self.fetch_timeout = fetch_timeout;
        self
    }
}

impl Default for ConfigPollerConfig {
    fn default() -> Self {
        Self {
            poll_interval: Duration::from_millis(2500),
            fetch_timeout: Duration::from_millis(2500),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct KvConfig {
    pub on_demand_connect: bool,
    pub enable_error_map: bool,
    pub enable_mutation_tokens: bool,
    pub enable_server_durations: bool,
    pub num_connections: usize,
    pub connect_timeout: Duration,
    pub connect_throttle_timeout: Duration,
}

impl KvConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn on_demand_connect(mut self, on_demand_connect: bool) -> Self {
        self.on_demand_connect = on_demand_connect;
        self
    }

    pub fn enable_error_map(mut self, enable: bool) -> Self {
        self.enable_error_map = enable;
        self
    }

    pub fn enable_mutation_tokens(mut self, enable: bool) -> Self {
        self.enable_mutation_tokens = enable;
        self
    }

    pub fn enable_server_durations(mut self, enable: bool) -> Self {
        self.enable_server_durations = enable;
        self
    }

    pub fn connect_timeout(mut self, connect_timeout: Duration) -> Self {
        self.connect_timeout = connect_timeout;
        self
    }

    pub fn connect_throttle_timeout(mut self, connect_throttle_timeout: Duration) -> Self {
        self.connect_throttle_timeout = connect_throttle_timeout;
        self
    }

    pub fn num_connections(mut self, num: usize) -> Self {
        self.num_connections = num;
        self
    }
}

impl Default for KvConfig {
    fn default() -> Self {
        Self {
            on_demand_connect: false,
            enable_error_map: true,
            enable_mutation_tokens: true,
            enable_server_durations: true,
            num_connections: 1,
            connect_timeout: Duration::from_secs(10),
            connect_throttle_timeout: Duration::from_secs(5),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct HttpConfig {
    pub max_idle_connections_per_host: Option<usize>,
    pub idle_connection_timeout: Duration,
}

impl HttpConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn max_idle_connections_per_host(mut self, max_idle_connections_per_host: usize) -> Self {
        self.max_idle_connections_per_host = Some(max_idle_connections_per_host);
        self
    }

    pub fn idle_connection_timeout(mut self, idle_connection_timeout: Duration) -> Self {
        self.idle_connection_timeout = idle_connection_timeout;
        self
    }
}

impl Default for HttpConfig {
    fn default() -> Self {
        Self {
            max_idle_connections_per_host: None,
            idle_connection_timeout: Duration::from_secs(1),
        }
    }
}

#[derive(Clone)]
#[non_exhaustive]
pub struct ReconfigureAgentOptions {
    pub authenticator: Authenticator,
    pub tls_config: Option<TlsConfig>,
}

impl ReconfigureAgentOptions {
    pub fn new(authenticator: Authenticator) -> Self {
        Self {
            tls_config: None,
            authenticator,
        }
    }

    pub fn tls_config(mut self, tls_config: impl Into<Option<TlsConfig>>) -> Self {
        self.tls_config = tls_config.into();
        self
    }
}

impl Display for SeedConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ http_addrs: {:?}, memd_addrs: {:?} }}",
            self.http_addrs, self.memd_addrs
        )
    }
}

impl Display for CompressionConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ disable_decompression: {}, mode: {} }}",
            self.disable_decompression, self.mode
        )
    }
}

impl Display for ConfigPollerConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ poll_interval: {:?}, fetch_timeout: {:?} }}",
            self.poll_interval, self.fetch_timeout
        )
    }
}

impl Display for KvConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ on_demand_connect: {}, enable_error_map: {}, enable_mutation_tokens: {}, enable_server_durations: {}, num_connections: {}, connect_timeout: {:?}, connect_throttle_timeout: {:?} }}",
            self.on_demand_connect,
            self.enable_error_map,
            self.enable_mutation_tokens,
            self.enable_server_durations,
            self.num_connections,
            self.connect_timeout,
            self.connect_throttle_timeout
        )
    }
}

impl Display for HttpConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ max_idle_connections_per_host: {:?}, idle_connection_timeout: {:?} }}",
            self.max_idle_connections_per_host, self.idle_connection_timeout
        )
    }
}

impl Display for AgentOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let tls_config = if cfg!(feature = "rustls-tls") {
            "rustls-tls"
        } else if cfg!(feature = "native-tls") {
            "native-tls"
        } else {
            "none"
        };

        write!(
            f,
            "{{ seed_config: {}, auth_mechanisms: {:?}, tls_config: {}, bucket_name: {:?}, network: {:?}, compression_config: {}, config_poller_config: {}, kv_config: {}, http_config: {}, tcp_keep_alive_time: {:?}, orphan_response_handler: {} }}",
            self.seed_config,
            self.auth_mechanisms,
            tls_config,
            self.bucket_name.clone(),
            self.network.clone(),
            self.compression_config,
            self.config_poller_config,
            self.kv_config,
            self.http_config,
            self.tcp_keep_alive_time,
            if self.orphan_response_handler.is_some() { "Some" } else { "None" },
        )
    }
}