Skip to content

Commit 05ebc28

Browse files
committed
Stop retaining a PostgreSQL runtime
Construct PostgreSQL storage on the caller runtime now that the store only exposes async KVStore operations. This removes the store-owned runtime and its shutdown path. Co-Authored-By: HAL 9000
1 parent ecb2eae commit 05ebc28

1 file changed

Lines changed: 4 additions & 50 deletions

File tree

src/io/postgres_store/mod.rs

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! Objects related to [`PostgresStore`] live here.
99
use std::collections::HashMap;
1010
use std::future::Future;
11-
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
11+
use std::sync::atomic::{AtomicU64, Ordering};
1212
use std::sync::{Arc, Mutex};
1313

1414
use lightning::io;
@@ -38,9 +38,6 @@ const SCHEMA_VERSION: u16 = 1;
3838
// The number of entries returned per page in paginated list operations.
3939
const PAGE_SIZE: usize = 50;
4040

41-
// Keep this small while still allowing progress if one runtime worker blocks on sync store access.
42-
const INTERNAL_RUNTIME_WORKERS: usize = 2;
43-
4441
fn sql_identifier(identifier: &str) -> io::Result<String> {
4542
if identifier.is_empty() || identifier.contains('\0') {
4643
return Err(io::Error::new(
@@ -91,18 +88,13 @@ macro_rules! query_with_retry {
9188

9289
/// A [`KVStore`] implementation that writes to and reads from a [PostgreSQL] database.
9390
///
94-
/// Maintains an internal runtime for the underlying tokio-postgres connection drivers.
95-
///
9691
/// [PostgreSQL]: https://www.postgresql.org
9792
pub struct PostgresStore {
9893
inner: Arc<PostgresStoreInner>,
9994

10095
// Version counter to ensure that writes are applied in the correct order. It is assumed that read and list
10196
// operations aren't sensitive to the order of execution.
10297
next_write_version: AtomicU64,
103-
104-
// A store-internal runtime used for setup and connection driver tasks.
105-
internal_runtime: Option<tokio::runtime::Runtime>,
10698
}
10799

108100
// tokio::sync::Mutex (used for the DB client) contains UnsafeCell which opts out of
@@ -145,33 +137,12 @@ impl PostgresStore {
145137
connection_string: String, db_name: Option<String>, kv_table_name: Option<String>,
146138
certificate_pem: Option<String>, logger: Option<Arc<Logger>>,
147139
) -> io::Result<Self> {
148-
let internal_runtime = tokio::runtime::Builder::new_multi_thread()
149-
.enable_all()
150-
.thread_name_fn(|| {
151-
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
152-
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
153-
format!("ldk-node-postgres-runtime-{}", id)
154-
})
155-
.worker_threads(INTERNAL_RUNTIME_WORKERS)
156-
.max_blocking_threads(INTERNAL_RUNTIME_WORKERS)
157-
.build()
158-
.map_err(|e| {
159-
io::Error::new(
160-
io::ErrorKind::Other,
161-
format!("Failed to build PostgreSQL runtime: {e}"),
162-
)
163-
})?;
164140
let tls = Self::build_tls_connector(certificate_pem)?;
165-
let runtime_handle = internal_runtime.handle();
166-
let inner = tokio::task::block_in_place(|| {
167-
runtime_handle.block_on(async {
168-
PostgresStoreInner::new(connection_string, db_name, kv_table_name, tls, logger)
169-
.await
170-
})
171-
})?;
141+
let inner =
142+
PostgresStoreInner::new(connection_string, db_name, kv_table_name, tls, logger).await?;
172143
let inner = Arc::new(inner);
173144
let next_write_version = AtomicU64::new(1);
174-
Ok(Self { inner, next_write_version, internal_runtime: Some(internal_runtime) })
145+
Ok(Self { inner, next_write_version })
175146
}
176147

177148
fn build_tls_connector(certificate_pem: Option<String>) -> io::Result<PgTlsConnector> {
@@ -216,14 +187,6 @@ impl PostgresStore {
216187
}
217188
}
218189

219-
impl Drop for PostgresStore {
220-
fn drop(&mut self) {
221-
if let Some(internal_runtime) = self.internal_runtime.take() {
222-
internal_runtime.shutdown_background();
223-
}
224-
}
225-
}
226-
227190
impl KVStore for PostgresStore {
228191
fn read(
229192
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
@@ -292,15 +255,6 @@ impl KVStore for PostgresStore {
292255
}
293256
}
294257

295-
impl PostgresStore {
296-
fn internal_runtime(&self) -> io::Result<&tokio::runtime::Runtime> {
297-
self.internal_runtime.as_ref().ok_or_else(|| {
298-
debug_assert!(false, "Failed to access internal PostgreSQL runtime");
299-
io::Error::new(io::ErrorKind::Other, "Failed to access internal PostgreSQL runtime")
300-
})
301-
}
302-
}
303-
304258
impl PaginatedKVStore for PostgresStore {
305259
fn list_paginated(
306260
&self, primary_namespace: &str, secondary_namespace: &str, page_token: Option<PageToken>,

0 commit comments

Comments
 (0)