forked from mongodb/mongo-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
175 lines (150 loc) · 5.18 KB
/
mod.rs
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
mod cluster_time;
mod pool;
#[cfg(test)]
mod test;
use std::{
collections::HashSet,
time::{Duration, Instant},
};
use lazy_static::lazy_static;
use uuid::Uuid;
use crate::{
bson::{doc, spec::BinarySubtype, Binary, Bson, Document},
options::SessionOptions,
Client,
RUNTIME,
};
pub(crate) use cluster_time::ClusterTime;
pub(super) use pool::ServerSessionPool;
lazy_static! {
pub(crate) static ref SESSIONS_UNSUPPORTED_COMMANDS: HashSet<&'static str> = {
let mut hash_set = HashSet::new();
hash_set.insert("killcursors");
hash_set.insert("parallelcollectionscan");
hash_set
};
}
/// A MongoDB client session. This struct represents a logical session used for ordering sequential
/// operations. To create a `ClientSession`, call `start_session` on a `Client`.
///
/// `ClientSession` instances are not thread safe or fork safe. They can only be used by one thread
/// or process at a time.
#[derive(Debug)]
pub struct ClientSession {
cluster_time: Option<ClusterTime>,
server_session: ServerSession,
client: Client,
is_implicit: bool,
options: Option<SessionOptions>,
}
impl ClientSession {
/// Creates a new `ClientSession` wrapping the provided server session.
pub(crate) fn new(
server_session: ServerSession,
client: Client,
options: Option<SessionOptions>,
is_implicit: bool,
) -> Self {
Self {
client,
server_session,
cluster_time: None,
is_implicit,
options,
}
}
/// The client used to create this session.
pub fn client(&self) -> Client {
self.client.clone()
}
/// The id of this session.
pub fn id(&self) -> &Document {
&self.server_session.id
}
/// Whether this session was created implicitly by the driver or explcitly by the user.
pub(crate) fn is_implicit(&self) -> bool {
self.is_implicit
}
/// The highest seen cluster time this session has seen so far.
/// This will be `None` if this session has not been used in an operation yet.
pub fn cluster_time(&self) -> Option<&ClusterTime> {
self.cluster_time.as_ref()
}
/// The options used to create this session.
pub fn options(&self) -> Option<&SessionOptions> {
self.options.as_ref()
}
/// Set the cluster time to the provided one if it is greater than this session's highest seen
/// cluster time or if this session's cluster time is `None`.
pub fn advance_cluster_time(&mut self, to: &ClusterTime) {
if self.cluster_time().map(|ct| ct < to).unwrap_or(true) {
self.cluster_time = Some(to.clone());
}
}
/// Mark this session (and the underlying server session) as dirty.
pub(crate) fn mark_dirty(&mut self) {
self.server_session.dirty = true;
}
/// Updates the date that the underlying server session was last used as part of an operation
/// sent to the server.
pub(crate) fn update_last_use(&mut self) {
self.server_session.last_use = Instant::now();
}
/// Increments the txn_number and returns the new value.
pub(crate) fn get_and_increment_txn_number(&mut self) -> u64 {
self.server_session.txn_number += 1;
self.server_session.txn_number
}
/// Whether this session is dirty.
#[cfg(test)]
pub(crate) fn is_dirty(&self) -> bool {
self.server_session.dirty
}
}
impl Drop for ClientSession {
fn drop(&mut self) {
let client = self.client.clone();
let server_session = ServerSession {
id: self.server_session.id.clone(),
last_use: self.server_session.last_use,
dirty: self.server_session.dirty,
txn_number: self.server_session.txn_number,
};
RUNTIME.execute(async move {
client.check_in_server_session(server_session).await;
})
}
}
/// Client side abstraction of a server session. These are pooled and may be associated with
/// multiple `ClientSession`s over the course of their lifetime.
#[derive(Clone, Debug)]
pub(crate) struct ServerSession {
/// The id of the server session to which this corresponds.
id: Document,
/// The last time an operation was executed with this session.
last_use: std::time::Instant,
/// Whether a network error was encountered while using this session.
dirty: bool,
/// A monotonically increasing transaction number for this session.
txn_number: u64,
}
impl ServerSession {
/// Creates a new session, generating the id client side.
fn new() -> Self {
let binary = Bson::Binary(Binary {
subtype: BinarySubtype::Uuid,
bytes: Uuid::new_v4().as_bytes().to_vec(),
});
Self {
id: doc! { "id": binary },
last_use: Instant::now(),
dirty: false,
txn_number: 0,
}
}
/// Determines if this server session is about to expire in a short amount of time (1 minute).
fn is_about_to_expire(&self, logical_session_timeout: Duration) -> bool {
let expiration_date = self.last_use + logical_session_timeout;
expiration_date < Instant::now() + Duration::from_secs(60)
}
}