MCPcopy Create free account
hub / github.com/chirpstack/chirpstack / pg_establish_connection

Function pg_establish_connection

chirpstack/src/storage/postgres.rs:69–94  ·  view source on GitHub ↗

Source: https://github.com/weiznich/diesel_async/blob/main/examples/postgres/pooled-with-rustls/src/main.rs

(config: &str)

Source from the content-addressed store, hash-verified

67// Source:
68// https://github.com/weiznich/diesel_async/blob/main/examples/postgres/pooled-with-rustls/src/main.rs
69fn pg_establish_connection(config: &str) -> BoxFuture<'_, ConnectionResult<AsyncPgConnection>> {
70 let fut = async {
71 let conf = config::get();
72
73 let root_certs = get_root_certs(if conf.postgresql.ca_cert.is_empty() {
74 None
75 } else {
76 Some(conf.postgresql.ca_cert.clone())
77 })
78 .map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
79 let rustls_config = rustls::ClientConfig::builder()
80 .with_root_certificates(root_certs)
81 .with_no_client_auth();
82 let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
83 let (client, conn) = tokio_postgres::connect(config, tls)
84 .await
85 .map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
86 tokio::spawn(async move {
87 if let Err(e) = conn.await {
88 error!(error = %e, "PostgreSQL connection error");
89 }
90 });
91 AsyncPgConnection::try_from(client).await
92 };
93 fut.boxed()
94}
95
96fn get_async_db_pool() -> Result<AsyncPgPool> {
97 let pool_r = ASYNC_PG_POOL.read().unwrap();

Callers

nothing calls this directly

Calls 3

get_root_certsFunction · 0.85
to_stringMethod · 0.80
getFunction · 0.70

Tested by

no test coverage detected