Skip to content

Commit b1ba6bf

Browse files
committed
Provide functions to build default pools
1 parent 9579655 commit b1ba6bf

File tree

1 file changed

+84
-14
lines changed

1 file changed

+84
-14
lines changed

src/pool.rs

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,54 @@
2525
//! To provide connection pooling, we use [`r2d2`] for a sync connection pool while we use
2626
//! [`bb8`](https://docs.rs/bb8) to provide an async connection pool.
2727
//!
28-
//! ## Sync usage
28+
//! ## Basic usage
29+
//!
30+
//! For your convenience, we have provided defaults for you to build connection pools.
31+
//! - [`get()`]: Returns a sync TCP pool
32+
//! - [`get_tls()`]: Returns a sync TLS pool
33+
//! - [`get_async()`]: Returns an async TCP pool
34+
//! - [`get_tls_async()`]: Returns an async TLS pool
35+
//!
36+
//! Below, we have created TCP/TLS pools with a size of 10 (you can choose anything that you need)
37+
//! and run some actions for demonstration.
38+
//!
39+
//! ### Sync Usage
40+
//!
41+
//! ```no_run
42+
//! use skytable::{pool, actions::Actions};
43+
//!
44+
//! let notls_pool = pool::get("127.0.0.1", 2003, 10).unwrap();
45+
//! notls_pool.get().unwrap().set("x", "100").unwrap();
46+
//!
47+
//! let tls_pool = pool::get_tls("127.0.0.1", 2004, "cert.pem", 10).unwrap();
48+
//! let ret: u8 = tls_pool.get().unwrap().get("x").unwrap();
49+
//!
50+
//! assert_eq!(ret, 100);
51+
//! ```
52+
//!
53+
//! ### Async Usage
54+
//!
55+
//! ```no_run
56+
//! use skytable::pool;
57+
//! use skytable::actions::AsyncActions;
58+
//!
59+
//! async fn run() {
60+
//! let notls_pool = pool::get_async("127.0.0.1", 2003, 10).await.unwrap();
61+
//! notls_pool.get().await.unwrap().set("x", "100").await.unwrap();
62+
//!
63+
//! let tls_pool = pool::get_tls_async("127.0.0.1", 2004, "cert.pem", 10).await.unwrap();
64+
//! let ret: u8 = tls_pool.get().await.unwrap().get("x").await.unwrap();
65+
//!
66+
//! assert_eq!(ret, 100);
67+
//! }
68+
//! ```
69+
//!
70+
//! ## Advanced usage
71+
//! If you want to configure a pool with custom settings, then you can use
72+
//! [r2d2's `Builder`](https://docs.rs/r2d2/0.8.9/r2d2/struct.Builder.html) or
73+
//! [bb8's `Builder`](https://docs.rs/bb8/0.7.1/bb8/struct.Builder.html) to configure your pool.
74+
//!
75+
//! ### Sync usage
2976
//!
3077
//! Example usage for TLS and non-TLS connection pools are given below.
3178
//!
@@ -34,23 +81,21 @@
3481
//! use skytable::sync::{Connection, TlsConnection};
3582
//!
3683
//! // non-TLS (TCP pool)
37-
//! let notls_manager = ConnectionManager::new_notls("127.0.0.1".into(), 2003);
84+
//! let notls_manager = ConnectionManager::new_notls("127.0.0.1", 2003);
3885
//! let notls_pool = Pool::builder()
3986
//! .max_size(10)
4087
//! .build(notls_manager)
4188
//! .unwrap();
4289
//!
4390
//! // TLS pool
44-
//! let tls_manager = ConnectionManager::new_tls(
45-
//! "127.0.0.1".into(), 2003, "cert.pem".into()
46-
//! );
91+
//! let tls_manager = ConnectionManager::new_tls("127.0.0.1", 2003, "cert.pem");
4792
//! let notls_pool = TlsPool::builder()
4893
//! .max_size(10)
4994
//! .build(tls_manager)
5095
//! .unwrap();
5196
//!```
5297
//!
53-
//! ## Async usage
98+
//! ### Async usage
5499
//!
55100
//! Example usage for TLS and non-TLS connection pools are given below.
56101
//!
@@ -59,17 +104,15 @@
59104
//! use skytable::aio::{Connection, TlsConnection};
60105
//! async fn run() {
61106
//! // non-TLS (TCP pool)
62-
//! let notls_manager = ConnectionManager::new_notls("127.0.0.1".into(), 2003);
107+
//! let notls_manager = ConnectionManager::new_notls("127.0.0.1", 2003);
63108
//! let notls_pool = AsyncPool::builder()
64109
//! .max_size(10)
65110
//! .build(notls_manager)
66111
//! .await
67112
//! .unwrap();
68113
//!
69114
//! // TLS pool
70-
//! let tls_manager = ConnectionManager::new_tls(
71-
//! "127.0.0.1".into(), 2003, "cert.pem".into()
72-
//! );
115+
//! let tls_manager = ConnectionManager::new_tls("127.0.0.1", 2003, "cert.pem");
73116
//! let notls_pool = AsyncTlsPool::builder()
74117
//! .max_size(10)
75118
//! .build(tls_manager)
@@ -85,17 +128,44 @@ cfg_sync_pool! {
85128
/// [`r2d2`](https://docs.rs/r2d2)'s error type
86129
pub use r2d2::Error as r2d2Error;
87130
pub use self::sync_impls::Pool;
131+
/// Returns a TCP pool of the specified size and provided settings
132+
pub fn get(host: impl ToString, port: u16, max_size: u32) -> Result<Pool, r2d2Error> {
133+
Pool::builder()
134+
.max_size(max_size)
135+
.build(ConnectionManager::new_notls(host.to_string(), port))
136+
}
88137
cfg_sync_ssl_any! {
89138
pub use self::sync_impls::TlsPool;
139+
/// Returns a TLS pool of the specified size and provided settings
140+
pub fn get_tls(host: impl ToString, port: u16, cert: impl ToString, max_size: u32) -> Result<TlsPool, r2d2Error> {
141+
TlsPool::builder()
142+
.max_size(max_size)
143+
.build(
144+
ConnectionManager::new_tls(host.to_string(), port, cert)
145+
)
146+
}
90147
}
91148
}
92149
// async
93150
cfg_async_pool! {
94151
/// [`bb8`](https://docs.rs/bb8)'s error type
95152
pub use bb8::RunError as bb8Error;
96153
pub use self::async_impls::Pool as AsyncPool;
154+
use crate::error::Error;
155+
/// Returns an async TCP pool of the specified size and provided settings
156+
pub async fn get_async(host: impl ToString, port: u16, max_size: u32) -> Result<AsyncPool, Error> {
157+
AsyncPool::builder()
158+
.max_size(max_size)
159+
.build(ConnectionManager::new_notls(host.to_string(), port)).await
160+
}
97161
cfg_async_ssl_any! {
98162
pub use self::async_impls::TlsPool as AsyncTlsPool;
163+
/// Returns an async TLS pool of the specified size and provided settings
164+
pub async fn get_tls_async(host: impl ToString, port: u16, cert: impl ToString, max_size: u32) -> Result<AsyncTlsPool, Error> {
165+
AsyncTlsPool::builder()
166+
.max_size(max_size)
167+
.build(ConnectionManager::new_tls(host.to_string(), port, cert)).await
168+
}
99169
}
100170
}
101171

@@ -124,12 +194,12 @@ impl<C> ConnectionManager<C> {
124194

125195
impl<C> ConnectionManager<C> {
126196
/// Create a new `ConnectionManager` that can be used to configure a non-TLS connection pool
127-
pub fn new_notls(host: String, port: u16) -> ConnectionManager<C> {
128-
Self::_new(host, port, None)
197+
pub fn new_notls(host: impl ToString, port: u16) -> ConnectionManager<C> {
198+
Self::_new(host.to_string(), port, None)
129199
}
130200
/// Create a new `ConnectionManager` that can be used to configure a TLS connection pool
131-
pub fn new_tls(host: String, port: u16, cert: String) -> ConnectionManager<C> {
132-
Self::_new(host, port, Some(cert))
201+
pub fn new_tls(host: impl ToString, port: u16, cert: impl ToString) -> ConnectionManager<C> {
202+
Self::_new(host.to_string(), port, Some(cert.to_string()))
133203
}
134204
}
135205

0 commit comments

Comments
 (0)