diff --git a/examples/pg.rs b/examples/pg.rs new file mode 100644 index 000000000..a1ab0f869 --- /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 = "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); + } + })); +} diff --git a/examples/pooled.rs b/examples/pooled.rs new file mode 100644 index 000000000..3fd885bfb --- /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 = "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() +} 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());