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
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},
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
};
}
#[derive(Debug)]
pub(crate) struct ClientSession {
cluster_time: Option<ClusterTime>,
server_session: ServerSession,
client: Client,
is_implicit: bool,
}
impl ClientSession {
pub(crate) fn new_implicit(server_session: ServerSession, client: Client) -> Self {
Self {
client,
server_session,
cluster_time: None,
is_implicit: true,
}
}
pub(crate) fn id(&self) -> &Document {
&self.server_session.id
}
pub(crate) fn is_implicit(&self) -> bool {
self.is_implicit
}
pub(crate) fn cluster_time(&self) -> Option<&ClusterTime> {
self.cluster_time.as_ref()
}
pub(crate) 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());
}
}
pub(crate) fn mark_dirty(&mut self) {
self.server_session.dirty = true;
}
pub(crate) fn update_last_use(&mut self) {
self.server_session.last_use = Instant::now();
}
}
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,
};
RUNTIME.execute(async move {
client.check_in_server_session(server_session).await;
})
}
}
#[derive(Debug)]
pub(crate) struct ServerSession {
id: Document,
last_use: std::time::Instant,
dirty: bool,
}
impl ServerSession {
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,
}
}
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)
}
}