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
pub const DEFAULT_POOL_SIZE: usize = 10;
cfg_sync! {
pub use r2d2::Error as r2d2Error;
pub use self::sync_impls::Pool;
pub fn get(host: impl ToString, port: u16, max_size: u32) -> Result<Pool, r2d2Error> {
Pool::builder()
.max_size(max_size)
.build(ConnectionManager::new_notls(host.to_string(), port))
}
}
cfg_sync_ssl_any! {
pub use self::sync_impls::TlsPool;
pub fn get_tls(host: impl ToString, port: u16, cert: impl ToString, max_size: u32) -> Result<TlsPool, r2d2Error> {
TlsPool::builder()
.max_size(max_size)
.build(
ConnectionManager::new_tls(host.to_string(), port, cert)
)
}
}
cfg_async! {
pub use bb8::RunError as bb8Error;
pub use self::async_impls::Pool as AsyncPool;
use crate::error::Error;
pub async fn get_async(host: impl ToString, port: u16, max_size: u32) -> Result<AsyncPool, Error> {
AsyncPool::builder()
.max_size(max_size)
.build(ConnectionManager::new_notls(host.to_string(), port)).await
}
}
cfg_async_ssl_any! {
pub use self::async_impls::TlsPool as AsyncTlsPool;
pub async fn get_tls_async(host: impl ToString, port: u16, cert: impl ToString, max_size: u32) -> Result<AsyncTlsPool, Error> {
AsyncTlsPool::builder()
.max_size(max_size)
.build(ConnectionManager::new_tls(host.to_string(), port, cert)).await
}
}
use core::marker::PhantomData;
#[derive(Debug, Clone)]
pub struct ConnectionManager<C> {
host: String,
port: u16,
cert: Option<String>,
_m: PhantomData<C>,
}
impl<C> ConnectionManager<C> {
fn _new(host: String, port: u16, cert: Option<String>) -> Self {
Self {
host,
port,
cert,
_m: PhantomData,
}
}
}
impl<C> ConnectionManager<C> {
pub fn new_notls(host: impl ToString, port: u16) -> ConnectionManager<C> {
Self::_new(host.to_string(), port, None)
}
pub fn new_tls(host: impl ToString, port: u16, cert: impl ToString) -> ConnectionManager<C> {
Self::_new(host.to_string(), port, Some(cert.to_string()))
}
}
#[cfg(feature = "sync")]
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
mod sync_impls {
use super::ConnectionManager;
use crate::sync::Connection as SyncConnection;
cfg_sync_ssl_any! {
use crate::sync::TlsConnection as SyncTlsConnection;
}
use crate::{
error::{Error, SkyhashError},
Element, Query, SkyQueryResult, SkyResult,
};
use r2d2::ManageConnection;
pub type Pool = r2d2::Pool<ConnectionManager<SyncConnection>>;
cfg_sync_ssl_any! {
pub type TlsPool = r2d2::Pool<ConnectionManager<SyncTlsConnection>>;
}
pub trait PoolableConnection: Send + Sync + Sized {
fn get_connection(host: &str, port: u16, tls_cert: Option<&String>) -> SkyResult<Self>;
fn run_query(&mut self, q: Query) -> SkyQueryResult;
}
impl PoolableConnection for SyncConnection {
fn get_connection(host: &str, port: u16, _tls_cert: Option<&String>) -> SkyResult<Self> {
let c = Self::new(host, port)?;
Ok(c)
}
fn run_query(&mut self, q: Query) -> SkyQueryResult {
self.run_query_raw(&q)
}
}
cfg_sync_ssl_any! {
impl PoolableConnection for SyncTlsConnection {
fn get_connection(host: &str, port: u16, tls_cert: Option<&String>) -> SkyResult<Self> {
let c = Self::new(
host,
port,
tls_cert.ok_or(Error::ConfigurationError(
"Expected TLS certificate in `ConnectionManager`",
))?,
)?;
Ok(c)
}
fn run_query(&mut self, q: Query) -> SkyQueryResult {
self.run_query_raw(&q)
}
}
}
impl<C: PoolableConnection + 'static> ManageConnection for ConnectionManager<C> {
type Error = Error;
type Connection = C;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
C::get_connection(self.host.as_ref(), self.port, self.cert.as_ref())
}
fn is_valid(&self, con: &mut Self::Connection) -> Result<(), Self::Error> {
let q = crate::query!("HEYA");
match con.run_query(q)? {
Element::String(st) if st.eq("HEY!") => Ok(()),
_ => Err(Error::SkyError(SkyhashError::UnexpectedResponse)),
}
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}
}
#[cfg(feature = "aio")]
#[cfg_attr(docsrs, doc(cfg(feature = "aio")))]
mod async_impls {
use super::ConnectionManager;
cfg_async_ssl_any! {
use crate::aio::TlsConnection as AsyncTlsConnection;
}
use crate::{
aio::Connection as AsyncConnection,
error::{Error, SkyhashError},
Element, Query, SkyQueryResult, SkyResult,
};
use async_trait::async_trait;
use bb8::ManageConnection;
pub type Pool = bb8::Pool<ConnectionManager<AsyncConnection>>;
cfg_async_ssl_any! {
pub type TlsPool = bb8::Pool<ConnectionManager<AsyncTlsConnection>>;
}
#[async_trait]
pub trait PoolableConnection: Send + Sync + Sized {
async fn get_connection(
host: &str,
port: u16,
tls_cert: Option<&String>,
) -> SkyResult<Self>;
async fn run_query(&mut self, q: Query) -> SkyQueryResult;
}
#[async_trait]
impl PoolableConnection for AsyncConnection {
async fn get_connection(
host: &str,
port: u16,
_tls_cert: Option<&String>,
) -> SkyResult<Self> {
let con = AsyncConnection::new(host, port).await?;
Ok(con)
}
async fn run_query(&mut self, q: Query) -> SkyQueryResult {
self.run_query_raw(&q).await
}
}
cfg_async_ssl_any! {
#[async_trait]
impl PoolableConnection for AsyncTlsConnection {
async fn get_connection(
host: &str,
port: u16,
tls_cert: Option<&String>,
) -> SkyResult<Self> {
let con = AsyncTlsConnection::new(
host,
port,
tls_cert.ok_or(Error::ConfigurationError(
"Expected TLS certificate in `ConnectionManager`",
))?,
)
.await?;
Ok(con)
}
async fn run_query(&mut self, q: Query) -> SkyQueryResult {
self.run_query_raw(&q).await
}
}
}
#[async_trait]
impl<C: PoolableConnection + 'static> ManageConnection for ConnectionManager<C> {
type Connection = C;
type Error = Error;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
C::get_connection(&self.host, self.port, self.cert.as_ref()).await
}
async fn is_valid(&self, con: &mut Self::Connection) -> Result<(), Self::Error> {
match con.run_query(crate::query!("HEYA")).await? {
Element::String(st) if st.eq("HEY!") => Ok(()),
_ => Err(Error::SkyError(SkyhashError::UnexpectedResponse)),
}
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}
}