From 051c5909a8249e53c78f7ab1e4263534ceb9d46e Mon Sep 17 00:00:00 2001 From: Flavian Desverne Date: Mon, 3 Apr 2023 18:27:52 +0200 Subject: [PATCH] examples --- examples/pg.rs | 33 +++++++++++++++++++++++++++++++++ examples/pooled.rs | 36 ++++++++++++++++++++++++++++++++++++ rust-toolchain | 1 - src/connector/postgres.rs | 16 ++++++++-------- 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 examples/pg.rs create mode 100644 examples/pooled.rs delete mode 100644 rust-toolchain diff --git a/examples/pg.rs b/examples/pg.rs new file mode 100644 index 000000000..c398f8626 --- /dev/null +++ b/examples/pg.rs @@ -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); + } + })); +} diff --git a/examples/pooled.rs b/examples/pooled.rs new file mode 100644 index 000000000..59253d5c1 --- /dev/null +++ b/examples/pooled.rs @@ -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() +} diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index 4d5fde5bd..000000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -1.60.0 diff --git a/src/connector/postgres.rs b/src/connector/postgres.rs index 76d4c0d83..6fc0c2419 100644 --- a/src/connector/postgres.rs +++ b/src/connector/postgres.rs @@ -37,7 +37,7 @@ pub use tokio_postgres; use super::IsolationLevel; #[derive(Clone)] -struct Hidden(T); +pub struct Hidden(pub T); impl Debug for Hidden { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -81,10 +81,10 @@ pub struct SslParams { } #[derive(Debug)] -struct SslAuth { - certificate: Hidden>, - identity: Hidden>, - ssl_accept_mode: SslAcceptMode, +pub struct SslAuth { + pub certificate: Hidden>, + pub identity: Hidden>, + pub ssl_accept_mode: SslAcceptMode, } impl Default for SslAuth { @@ -115,7 +115,7 @@ impl SslAuth { } impl SslParams { - async fn into_auth(self) -> crate::Result { + pub async fn into_auth(self) -> crate::Result { let mut auth = SslAuth::default(); auth.accept_mode(self.ssl_accept_mode); @@ -457,7 +457,7 @@ impl PostgresUrl { }) } - pub(crate) fn ssl_params(&self) -> &SslParams { + pub fn ssl_params(&self) -> &SslParams { &self.query_params.ssl_params } @@ -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());