forked from mongodb/mongo-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmap.rs
191 lines (168 loc) · 6 KB
/
cmap.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#[cfg(test)]
pub(crate) mod test;
pub(crate) mod conn;
mod connection_requester;
pub(crate) mod establish;
mod manager;
pub(crate) mod options;
mod status;
mod worker;
use derivative::Derivative;
pub use self::conn::ConnectionInfo;
pub(crate) use self::{
conn::{Command, Connection, RawCommand, RawCommandResponse, StreamDescription},
status::PoolGenerationSubscriber,
worker::PoolGeneration,
};
use self::{
connection_requester::ConnectionRequestResult,
establish::ConnectionEstablisher,
options::ConnectionPoolOptions,
};
use crate::{
bson::oid::ObjectId,
error::{Error, Result},
event::cmap::{
CmapEvent,
CmapEventEmitter,
ConnectionCheckoutFailedEvent,
ConnectionCheckoutFailedReason,
ConnectionCheckoutStartedEvent,
PoolCreatedEvent,
},
options::ServerAddress,
runtime::AcknowledgmentReceiver,
sdam::{BroadcastMessage, TopologyUpdater},
};
use connection_requester::ConnectionRequester;
use manager::PoolManager;
use worker::ConnectionPoolWorker;
#[cfg(test)]
use crate::runtime::WorkerHandle;
const DEFAULT_MAX_POOL_SIZE: u32 = 10;
/// A pool of connections implementing the CMAP spec.
/// This type is actually a handle to task that manages the connections and is cheap to clone and
/// pass around.
#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub(crate) struct ConnectionPool {
address: ServerAddress,
manager: PoolManager,
connection_requester: ConnectionRequester,
generation_subscriber: PoolGenerationSubscriber,
#[derivative(Debug = "ignore")]
event_emitter: CmapEventEmitter,
}
impl ConnectionPool {
pub(crate) fn new(
address: ServerAddress,
connection_establisher: ConnectionEstablisher,
server_updater: TopologyUpdater,
topology_id: ObjectId,
options: Option<ConnectionPoolOptions>,
) -> Self {
let event_handler = options
.as_ref()
.and_then(|opts| opts.cmap_event_handler.clone());
let event_emitter = CmapEventEmitter::new(event_handler, topology_id);
let (manager, connection_requester, generation_subscriber) = ConnectionPoolWorker::start(
address.clone(),
connection_establisher,
server_updater,
event_emitter.clone(),
options.clone(),
);
event_emitter.emit_event(|| {
CmapEvent::PoolCreated(PoolCreatedEvent {
address: address.clone(),
options: options.map(|o| o.to_event_options()),
})
});
Self {
address,
manager,
connection_requester,
generation_subscriber,
event_emitter,
}
}
#[cfg(test)]
pub(crate) fn new_mocked(address: ServerAddress) -> Self {
let (manager, _) = manager::channel();
let handle = WorkerHandle::new_mocked();
let (connection_requester, _) = connection_requester::channel(handle);
let (_, generation_subscriber) = status::channel(PoolGeneration::normal());
Self {
address,
manager,
connection_requester,
generation_subscriber,
event_emitter: CmapEventEmitter::new(None, ObjectId::new()),
}
}
/// Checks out a connection from the pool. This method will yield until this thread is at the
/// front of the wait queue, and then will block again if no available connections are in the
/// pool and the total number of connections is not less than the max pool size.
pub(crate) async fn check_out(&self) -> Result<Connection> {
self.event_emitter.emit_event(|| {
ConnectionCheckoutStartedEvent {
address: self.address.clone(),
}
.into()
});
let response = self.connection_requester.request().await;
let conn = match response {
ConnectionRequestResult::Pooled(c) => Ok(*c),
ConnectionRequestResult::Establishing(task) => task.await,
ConnectionRequestResult::PoolCleared(e) => {
Err(Error::pool_cleared_error(&self.address, &e))
}
ConnectionRequestResult::PoolWarmed => {
Err(Error::internal("Invalid result from connection requester"))
}
};
match conn {
Ok(ref conn) => {
self.event_emitter
.emit_event(|| conn.checked_out_event().into());
}
#[cfg(feature = "tracing-unstable")]
Err(ref err) => {
self.event_emitter.emit_event(|| {
ConnectionCheckoutFailedEvent {
address: self.address.clone(),
reason: ConnectionCheckoutFailedReason::ConnectionError,
error: Some(err.clone()),
}
.into()
});
}
#[cfg(not(feature = "tracing-unstable"))]
Err(_) => {
self.event_emitter.emit_event(|| {
ConnectionCheckoutFailedEvent {
address: self.address.clone(),
reason: ConnectionCheckoutFailedReason::ConnectionError,
}
.into()
});
}
}
conn
}
/// Increments the generation of the pool. Rather than eagerly removing stale connections from
/// the pool, they are left for the background thread to clean up.
pub(crate) async fn clear(&self, cause: Error, service_id: Option<ObjectId>) {
self.manager.clear(cause, service_id).await
}
/// Mark the pool as "ready" as per the CMAP specification.
pub(crate) async fn mark_as_ready(&self) {
self.manager.mark_as_ready().await
}
pub(crate) fn generation(&self) -> PoolGeneration {
self.generation_subscriber.generation()
}
pub(crate) fn broadcast(&self, msg: BroadcastMessage) -> AcknowledgmentReceiver<()> {
self.manager.broadcast(msg)
}
}