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

add two examples: quaint pooled + tokio-postgres with tls #439

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "DATABASE_URL".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 = "DATABASE_URL".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