This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add two examples: quaint pooled + tokio-postgres with tls
- Loading branch information
Showing
4 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters