mongodb 3.6.0

The official MongoDB driver for Rust
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
mod command;
pub(crate) mod pooled;
mod stream_description;
pub(crate) mod wire;

use std::{sync::Arc, time::Instant};

use derive_where::derive_where;
use serde::Serialize;
use tokio::{
    io::BufStream,
    sync::{
        broadcast::{self, error::RecvError},
        mpsc,
        Mutex,
    },
};

use self::wire::{Message, MessageFlags};
use super::{conn::pooled::PooledConnection, manager::PoolManager};
use crate::{
    bson::oid::ObjectId,
    cmap::PoolGeneration,
    error::{load_balanced_mode_mismatch, Error, ErrorKind, Result},
    event::cmap::{CmapEventEmitter, ConnectionCreatedEvent},
    options::ServerAddress,
    runtime::AsyncStream,
};
pub(crate) use command::{Command, RawCommandResponse};
pub(crate) use stream_description::StreamDescription;

#[cfg(any(
    feature = "zstd-compression",
    feature = "zlib-compression",
    feature = "snappy-compression"
))]
use crate::options::Compressor;

/// User-facing information about a connection to the database.
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ConnectionInfo {
    /// A driver-generated identifier that uniquely identifies the connection.
    pub id: u32,

    /// A server-generated identifier that uniquely identifies the connection. This may be used to
    /// correlate driver connections with server logs.
    pub server_id: Option<i64>,

    /// The address that the connection is connected to.
    pub address: ServerAddress,
}

/// A wrapper around Stream that contains all the CMAP information needed to maintain a connection.
#[derive_where(Debug)]
pub(crate) struct Connection {
    /// The stream this connection reads from and writes to.
    stream: BufStream<AsyncStream>,

    /// The cached stream description from the connection's handshake.
    pub(crate) stream_description: Option<StreamDescription>,

    /// Driver-generated ID for the connection.
    pub(crate) id: u32,

    /// The server-side ID for this connection.
    pub(crate) server_id: Option<i64>,

    /// The address of the server to which this connection connects.
    pub(crate) address: ServerAddress,

    /// The time at which this connection was created.
    pub(crate) time_created: Instant,

    /// Whether or not a command is currently being run on this connection. This is set to `true`
    /// right before sending bytes to the server and set back to `false` once a full response has
    /// been read.
    command_executing: bool,

    /// Stores a network error encountered while reading or writing. Once the connection has
    /// received an error, it should not be used again and will be closed upon check-in to the
    /// pool.
    error: Option<Error>,

    /// Whether the most recently received message included the moreToCome flag, indicating the
    /// server may send more responses without any additional requests. Attempting to send new
    /// messages on this connection while this value is true will return an error. This value
    /// will remain true until a server response does not include the moreToComeFlag.
    more_to_come: bool,

    /// The token callback for OIDC authentication.
    #[derive_where(skip)]
    pub(crate) oidc_token_gen_id: tokio::sync::Mutex<u32>,

    /// The compressor to use to compress outgoing messages.
    #[cfg(any(
        feature = "zstd-compression",
        feature = "zlib-compression",
        feature = "snappy-compression"
    ))]
    pub(crate) compressor: Option<Compressor>,
}

impl Connection {
    /// Create a new connection.
    pub(crate) fn new(
        address: ServerAddress,
        stream: AsyncStream,
        id: u32,
        time_created: Instant,
    ) -> Self {
        Self {
            stream: BufStream::new(stream),
            stream_description: None,
            address,
            id,
            server_id: None,
            time_created,
            command_executing: false,
            error: None,
            more_to_come: false,
            oidc_token_gen_id: tokio::sync::Mutex::new(0),
            #[cfg(any(
                feature = "zstd-compression",
                feature = "zlib-compression",
                feature = "snappy-compression"
            ))]
            compressor: None,
        }
    }

    pub(crate) fn take(&mut self) -> Self {
        Self {
            stream: std::mem::replace(&mut self.stream, BufStream::new(AsyncStream::Null)),
            stream_description: self.stream_description.take(),
            address: self.address.clone(),
            id: self.id,
            server_id: self.server_id,
            time_created: self.time_created,
            command_executing: self.command_executing,
            error: self.error.take(),
            more_to_come: false,
            oidc_token_gen_id: tokio::sync::Mutex::new(0),
            #[cfg(any(
                feature = "zstd-compression",
                feature = "zlib-compression",
                feature = "snappy-compression"
            ))]
            compressor: self.compressor.clone(),
        }
    }

    pub(crate) fn address(&self) -> &ServerAddress {
        &self.address
    }

    /// Gets the connection's StreamDescription.
    pub(crate) fn stream_description(&self) -> Result<&StreamDescription> {
        self.stream_description.as_ref().ok_or_else(|| {
            ErrorKind::Internal {
                message: "Stream checked out but not handshaked".to_string(),
            }
            .into()
        })
    }

    /// Whether the connection is currently executing an operation.
    pub(super) fn is_executing(&self) -> bool {
        self.command_executing
    }

    /// Whether an error has been encountered on this connection.
    pub(super) fn has_errored(&self) -> bool {
        self.error.is_some()
    }

    pub(crate) async fn send_message_with_cancellation(
        &mut self,
        message: impl TryInto<Message, Error = impl Into<Error>>,
        cancellation_receiver: &mut broadcast::Receiver<()>,
    ) -> Result<RawCommandResponse> {
        tokio::select! {
            biased;

            // A lagged error indicates that more heartbeats failed than the channel's capacity
            // between checking out this connection and executing the operation. If this occurs,
            // then proceed with cancelling the operation. RecvError::Closed can be ignored, as
            // the sender (and by extension the connection pool) dropping does not indicate that
            // the operation should be cancelled.
            Ok(_) | Err(RecvError::Lagged(_)) = cancellation_receiver.recv() => {
                let error: Error = ErrorKind::ConnectionPoolCleared {
                    message: format!(
                        "Connection to {} interrupted due to server monitor timeout",
                        self.address,
                    )
                }.into();
                self.error = Some(error.clone());
                Err(error)
            }
            // This future is not cancellation safe because it contains calls to methods that are
            // not cancellation safe (e.g. AsyncReadExt::read_exact). However, in the case that
            // this future is cancelled because a cancellation message was received, this
            // connection will be closed upon being returned to the pool, so any data loss on its
            // underlying stream is not an issue.
            result = self.send_message(message) => result,
        }
    }

    pub(crate) async fn send_message(
        &mut self,
        message: impl TryInto<Message, Error = impl Into<Error>>,
    ) -> Result<RawCommandResponse> {
        let message = message.try_into().map_err(Into::into)?;

        if self.more_to_come {
            return Err(Error::internal(format!(
                "attempted to send a new message to {} but moreToCome bit was set",
                self.address()
            )));
        }

        self.command_executing = true;

        let max_message_size = self.max_message_size_bytes();
        #[cfg(any(
            feature = "zstd-compression",
            feature = "zlib-compression",
            feature = "snappy-compression"
        ))]
        let write_result = match self.compressor {
            Some(ref compressor) if message.should_compress => {
                message
                    .write_op_compressed_to(&mut self.stream, compressor, max_message_size)
                    .await
            }
            _ => {
                message
                    .write_op_msg_to(&mut self.stream, max_message_size)
                    .await
            }
        };
        #[cfg(all(
            not(feature = "zstd-compression"),
            not(feature = "zlib-compression"),
            not(feature = "snappy-compression")
        ))]
        let write_result = message
            .write_op_msg_to(&mut self.stream, max_message_size)
            .await;

        if let Err(ref err) = write_result {
            self.error = Some(err.clone());
        }
        write_result?;

        let response_message_result = Message::read_from(&mut self.stream, max_message_size).await;
        self.command_executing = false;
        if let Err(ref err) = response_message_result {
            self.error = Some(err.clone());
        }

        let response_message = response_message_result?;
        self.more_to_come = response_message.flags.contains(MessageFlags::MORE_TO_COME);

        Ok(RawCommandResponse::new(
            self.address.clone(),
            response_message,
        ))
    }

    /// Receive the next message from the connection.
    /// This will return an error if the previous response on this connection did not include the
    /// moreToCome flag.
    pub(crate) async fn receive_message(&mut self) -> Result<RawCommandResponse> {
        if !self.more_to_come {
            return Err(Error::internal(format!(
                "attempted to stream response from connection to {} but moreToCome bit was not set",
                self.address()
            )));
        }

        self.command_executing = true;
        let response_message_result = Message::read_from(
            &mut self.stream,
            self.stream_description
                .as_ref()
                .map(|d| d.max_message_size_bytes),
        )
        .await;
        self.command_executing = false;
        if let Err(ref err) = response_message_result {
            self.error = Some(err.clone());
        }

        let response_message = response_message_result?;
        self.more_to_come = response_message.flags.contains(MessageFlags::MORE_TO_COME);

        Ok(RawCommandResponse::new(
            self.address.clone(),
            response_message,
        ))
    }

    /// Whether or not the previous command response indicated that the server may send
    /// more responses without another request.
    pub(crate) fn is_streaming(&self) -> bool {
        self.more_to_come
    }

    fn max_message_size_bytes(&self) -> Option<i32> {
        self.stream_description
            .as_ref()
            .map(|d| d.max_message_size_bytes)
    }
}

/// A handle to a pinned connection - the connection itself can be retrieved or returned to the
/// normal pool via this handle.
#[derive(Debug)]
pub(crate) struct PinnedConnectionHandle {
    id: u32,
    receiver: Arc<Mutex<mpsc::Receiver<PooledConnection>>>,
}

impl PinnedConnectionHandle {
    /// Make a new `PinnedConnectionHandle` that refers to the same connection as this one.
    /// Use with care and only when "lending" a handle in a way that can't be expressed as a
    /// normal borrow.
    pub(crate) fn replicate(&self) -> Self {
        Self {
            id: self.id,
            receiver: self.receiver.clone(),
        }
    }

    /// Retrieve the pinned connection.  Will fail if the connection has been unpinned or is still
    /// in use.
    pub(crate) async fn take_connection(&self) -> Result<PooledConnection> {
        use tokio::sync::mpsc::error::TryRecvError;
        let mut receiver = self.receiver.lock().await;
        let mut connection = match receiver.try_recv() {
            Ok(conn) => conn,
            Err(TryRecvError::Disconnected) => {
                return Err(Error::internal(format!(
                    "cannot take connection after unpin (id={})",
                    self.id
                )))
            }
            Err(TryRecvError::Empty) => {
                return Err(Error::internal(format!(
                    "cannot take in-use connection (id={})",
                    self.id
                )))
            }
        };
        connection.mark_pinned_in_use();
        Ok(connection)
    }

    pub(crate) fn id(&self) -> u32 {
        self.id
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct LoadBalancedGeneration {
    pub(crate) generation: u32,
    pub(crate) service_id: ObjectId,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum ConnectionGeneration {
    Normal(u32),
    LoadBalanced(Option<LoadBalancedGeneration>),
}

impl ConnectionGeneration {
    pub(crate) fn service_id(self) -> Option<ObjectId> {
        match self {
            ConnectionGeneration::LoadBalanced(Some(gen)) => Some(gen.service_id),
            _ => None,
        }
    }

    pub(crate) fn is_stale(self, current_generation: &PoolGeneration) -> bool {
        match (self, current_generation) {
            (ConnectionGeneration::Normal(cgen), PoolGeneration::Normal(pgen)) => cgen != *pgen,
            (ConnectionGeneration::LoadBalanced(cgen), PoolGeneration::LoadBalanced(gen_map)) => {
                if let Some(cgen) = cgen {
                    cgen.generation != *gen_map.get(&cgen.service_id).unwrap_or(&0)
                } else {
                    // In the event that an error occurred during handshake and no serviceId was
                    // returned, just ignore the error for SDAM purposes, since
                    // we won't know which serviceId to clear for.
                    false
                }
            }
            _ => load_balanced_mode_mismatch!(false),
        }
    }
}

impl From<LoadBalancedGeneration> for ConnectionGeneration {
    fn from(gen: LoadBalancedGeneration) -> Self {
        ConnectionGeneration::LoadBalanced(Some(gen))
    }
}

/// Struct encapsulating the information needed to establish a `Connection`.
///
/// Creating a `PendingConnection` contributes towards the total connection count of a pool, despite
/// not actually making a TCP connection to the pool's endpoint. This models a "pending" Connection
/// from the CMAP specification.
pub(crate) struct PendingConnection {
    pub(crate) id: u32,
    pub(crate) address: ServerAddress,
    pub(crate) generation: PoolGeneration,
    pub(crate) event_emitter: CmapEventEmitter,
    pub(crate) time_created: Instant,
    pub(crate) cancellation_receiver: Option<broadcast::Receiver<()>>,
}

impl PendingConnection {
    /// Helper to create a `ConnectionCreatedEvent` for the connection.
    pub(super) fn created_event(&self) -> ConnectionCreatedEvent {
        ConnectionCreatedEvent {
            address: self.address.clone(),
            connection_id: self.id,
        }
    }
}