Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 051c590

Browse files
committed
examples
1 parent 53c756a commit 051c590

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed

examples/pg.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use futures::FutureExt;
2+
use native_tls::TlsConnector;
3+
use postgres_native_tls::MakeTlsConnector;
4+
use quaint::connector::PostgresUrl;
5+
use tokio;
6+
7+
#[tokio::main]
8+
async fn main() -> () {
9+
let url = "DATABASE_URL".to_string();
10+
let url = PostgresUrl::new(url::Url::parse(&url).unwrap()).unwrap();
11+
12+
connect_tls(url).await
13+
}
14+
15+
async fn connect_tls(url: PostgresUrl) -> () {
16+
let config: tokio_postgres::Config = url.to_config();
17+
18+
let mut tls_builder = TlsConnector::builder();
19+
tls_builder.danger_accept_invalid_certs(true);
20+
21+
let tls = MakeTlsConnector::new(tls_builder.build().unwrap());
22+
23+
let now = std::time::Instant::now();
24+
let (_, conn) = config.connect(tls).await.unwrap();
25+
println!("conn: {:?}", now.elapsed());
26+
27+
tokio::spawn(conn.map(|r| match r {
28+
Ok(_) => (),
29+
Err(e) => {
30+
tracing::error!("Error in PostgreSQL connection: {:?}", e);
31+
}
32+
}));
33+
}

examples/pooled.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use quaint::pooled::{PooledConnection, Quaint};
2+
use std::time::Instant;
3+
4+
#[tokio::main]
5+
async fn main() -> () {
6+
let url = "DATABASE_URL".to_string();
7+
8+
pooled_connection(&url).await;
9+
}
10+
11+
async fn pooled_connection(url: &str) -> () {
12+
let now_total = Instant::now();
13+
let now = Instant::now();
14+
let quaint = build_quaint(&url);
15+
let elapsed = now.elapsed();
16+
println!("Quaint building: {:?}", elapsed);
17+
18+
let now = Instant::now();
19+
let _ = get_conn(&quaint).await;
20+
println!("Conn acquired: {:?}", now.elapsed());
21+
22+
println!("Total time: {:?}", now_total.elapsed());
23+
}
24+
25+
async fn get_conn(quaint: &Quaint) -> PooledConnection {
26+
quaint.check_out().await.unwrap()
27+
}
28+
29+
fn build_quaint(postgres_url: &str) -> Quaint {
30+
let mut builder = Quaint::builder(postgres_url).expect("should connect");
31+
32+
builder.health_check_interval(std::time::Duration::from_secs(15));
33+
builder.test_on_check_out(true);
34+
35+
builder.build()
36+
}

rust-toolchain

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/connector/postgres.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use tokio_postgres;
3737
use super::IsolationLevel;
3838

3939
#[derive(Clone)]
40-
struct Hidden<T>(T);
40+
pub struct Hidden<T>(pub T);
4141

4242
impl<T> Debug for Hidden<T> {
4343
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -81,10 +81,10 @@ pub struct SslParams {
8181
}
8282

8383
#[derive(Debug)]
84-
struct SslAuth {
85-
certificate: Hidden<Option<Certificate>>,
86-
identity: Hidden<Option<Identity>>,
87-
ssl_accept_mode: SslAcceptMode,
84+
pub struct SslAuth {
85+
pub certificate: Hidden<Option<Certificate>>,
86+
pub identity: Hidden<Option<Identity>>,
87+
pub ssl_accept_mode: SslAcceptMode,
8888
}
8989

9090
impl Default for SslAuth {
@@ -115,7 +115,7 @@ impl SslAuth {
115115
}
116116

117117
impl SslParams {
118-
async fn into_auth(self) -> crate::Result<SslAuth> {
118+
pub async fn into_auth(self) -> crate::Result<SslAuth> {
119119
let mut auth = SslAuth::default();
120120
auth.accept_mode(self.ssl_accept_mode);
121121

@@ -457,7 +457,7 @@ impl PostgresUrl {
457457
})
458458
}
459459

460-
pub(crate) fn ssl_params(&self) -> &SslParams {
460+
pub fn ssl_params(&self) -> &SslParams {
461461
&self.query_params.ssl_params
462462
}
463463

@@ -466,7 +466,7 @@ impl PostgresUrl {
466466
self.query_params.connection_limit
467467
}
468468

469-
pub(crate) fn to_config(&self) -> Config {
469+
pub fn to_config(&self) -> Config {
470470
let mut config = Config::new();
471471

472472
config.user(self.username().borrow());

0 commit comments

Comments
 (0)