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

Commit

Permalink
add two examples: quaint pooled + tokio-postgres with tls
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Apr 3, 2023
1 parent 53c756a commit ddb90f2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
33 changes: 33 additions & 0 deletions examples/pg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use futures::FutureExt;
use native_tls::TlsConnector;
use postgres_native_tls::MakeTlsConnector;
use quaint::connector::PostgresUrl;
use tokio;

#[tokio::main]
async fn main() -> () {
let url = "postgresql://lime_extensive_agnesse:nzQgUybff4@db-provision-postgres23452b4.c8yxynpcltwd.us-east-1.rds.amazonaws.com:5432/aqua_bug".to_string();
let url = PostgresUrl::new(url::Url::parse(&url).unwrap()).unwrap();

connect_tls(url).await
}

async fn connect_tls(url: PostgresUrl) -> () {
let config: tokio_postgres::Config = url.to_config();

let mut tls_builder = TlsConnector::builder();
tls_builder.danger_accept_invalid_certs(true);

let tls = MakeTlsConnector::new(tls_builder.build().unwrap());

let now = std::time::Instant::now();
let (_, conn) = config.connect(tls).await.unwrap();
println!("conn: {:?}", now.elapsed());

tokio::spawn(conn.map(|r| match r {
Ok(_) => (),
Err(e) => {
tracing::error!("Error in PostgreSQL connection: {:?}", e);
}
}));
}
36 changes: 36 additions & 0 deletions examples/pooled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use quaint::pooled::{PooledConnection, Quaint};
use std::time::Instant;

#[tokio::main]
async fn main() -> () {
let url = "postgresql://lime_extensive_agnesse:nzQgUybff4@db-provision-postgres23452b4.c8yxynpcltwd.us-east-1.rds.amazonaws.com:5432/aqua_bug".to_string();

pooled_connection(&url).await;
}

async fn pooled_connection(url: &str) -> () {
let now_total = Instant::now();
let now = Instant::now();
let quaint = build_quaint(&url);
let elapsed = now.elapsed();
println!("Quaint building: {:?}", elapsed);

let now = Instant::now();
let _ = get_conn(&quaint).await;
println!("Conn acquired: {:?}", now.elapsed());

println!("Total time: {:?}", now_total.elapsed());
}

async fn get_conn(quaint: &Quaint) -> PooledConnection {
quaint.check_out().await.unwrap()
}

fn build_quaint(postgres_url: &str) -> Quaint {
let mut builder = Quaint::builder(postgres_url).expect("should connect");

builder.health_check_interval(std::time::Duration::from_secs(15));
builder.test_on_check_out(true);

builder.build()
}
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.

16 changes: 8 additions & 8 deletions src/connector/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use tokio_postgres;
use super::IsolationLevel;

#[derive(Clone)]
struct Hidden<T>(T);
pub struct Hidden<T>(pub T);

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

#[derive(Debug)]
struct SslAuth {
certificate: Hidden<Option<Certificate>>,
identity: Hidden<Option<Identity>>,
ssl_accept_mode: SslAcceptMode,
pub struct SslAuth {
pub certificate: Hidden<Option<Certificate>>,
pub identity: Hidden<Option<Identity>>,
pub ssl_accept_mode: SslAcceptMode,
}

impl Default for SslAuth {
Expand Down Expand Up @@ -115,7 +115,7 @@ impl SslAuth {
}

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

Expand Down Expand Up @@ -457,7 +457,7 @@ impl PostgresUrl {
})
}

pub(crate) fn ssl_params(&self) -> &SslParams {
pub fn ssl_params(&self) -> &SslParams {
&self.query_params.ssl_params
}

Expand All @@ -466,7 +466,7 @@ impl PostgresUrl {
self.query_params.connection_limit
}

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

config.user(self.username().borrow());
Expand Down

0 comments on commit ddb90f2

Please sign in to comment.