sync
or pool
or aio
or aio-pool
only.Expand description
Connection pooling
This module provides utilities to use connection pooling. As we already know, it is far more efficient to maintain a number of live connections to a database and share them across multiple “worker threads”, using a “connection pool” because creating individual connections whenever a worker receives a task is slow while maintaining a connection per worker might be cumbersome to implement.
To provide connection pooling, we use r2d2
for a sync connection pool while we use
bb8
to provide an async connection pool.
Basic usage
For your convenience, we have provided defaults for you to build connection pools.
get()
: Returns a sync TCP poolget_tls()
: Returns a sync TLS poolget_async()
: Returns an async TCP poolget_tls_async()
: Returns an async TLS pool
Below, we have created TCP/TLS pools with a size of 10 (you can choose anything that you need) and run some actions for demonstration.
Sync Usage
use skytable::{pool, actions::Actions};
let notls_pool = pool::get("127.0.0.1", 2003, 10).unwrap();
notls_pool.get().unwrap().set("x", "100").unwrap();
let tls_pool = pool::get_tls("127.0.0.1", 2004, "cert.pem", 10).unwrap();
let ret: u8 = tls_pool.get().unwrap().get("x").unwrap();
assert_eq!(ret, 100);
Async Usage
use skytable::pool;
use skytable::actions::AsyncActions;
async fn run() {
let notls_pool = pool::get_async("127.0.0.1", 2003, 10).await.unwrap();
notls_pool.get().await.unwrap().set("x", "100").await.unwrap();
let tls_pool = pool::get_tls_async("127.0.0.1", 2004, "cert.pem", 10).await.unwrap();
let ret: u8 = tls_pool.get().await.unwrap().get("x").await.unwrap();
assert_eq!(ret, 100);
}
Advanced usage
If you want to configure a pool with custom settings, then you can use
r2d2’s Builder
or
bb8’s Builder
to configure your pool.
Sync usage
Example usage for TLS and non-TLS connection pools are given below.
use skytable::pool::{ConnectionManager, Pool, TlsPool};
use skytable::sync::{Connection, TlsConnection};
// non-TLS (TCP pool)
let notls_manager = ConnectionManager::new_notls("127.0.0.1", 2003);
let notls_pool = Pool::builder()
.max_size(10)
.build(notls_manager)
.unwrap();
// TLS pool
let tls_manager = ConnectionManager::new_tls("127.0.0.1", 2003, "cert.pem");
let notls_pool = TlsPool::builder()
.max_size(10)
.build(tls_manager)
.unwrap();
Async usage
Example usage for TLS and non-TLS connection pools are given below.
use skytable::pool::{ConnectionManager, AsyncPool, AsyncTlsPool};
use skytable::aio::{Connection, TlsConnection};
async fn run() {
// non-TLS (TCP pool)
let notls_manager = ConnectionManager::new_notls("127.0.0.1", 2003);
let notls_pool = AsyncPool::builder()
.max_size(10)
.build(notls_manager)
.await
.unwrap();
// TLS pool
let tls_manager = ConnectionManager::new_tls("127.0.0.1", 2003, "cert.pem");
let notls_pool = AsyncTlsPool::builder()
.max_size(10)
.build(tls_manager)
.await
.unwrap();
}
Structs
A ConnectionManager
for connection pools. See the module level documentation
for examples and more information
Enums
Functions
sync
and pool
Returns a TCP pool of the specified size and provided settings
aio
and aio-pool
Returns an async TCP pool of the specified size and provided settings
sync
and (ssl
or sslv
)Returns a TLS pool of the specified size and provided settings
aio
and (aio-ssl
or aio-sslv
)Returns an async TLS pool of the specified size and provided settings
Type Definitions
An asynchronous non-TLS connection pool to Skytable
aio
and (aio-ssl
or aio-sslv
)An asynchronous TLS connection pool to Skytable
A non-TLS connection pool to Skytable
sync
and (ssl
or sslv
)A TLS connection pool to Skytable