From f42caacab28465c8b93dd48d7d76c73c46e2a689 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Tue, 1 Dec 2026 20:20:33 +0100 Subject: [PATCH] refactor: drop the second vss client KVStoreSync and KVStore both run their requests on the store's internal runtime, so the separate async read and write clients, kept to avoid sharing connections across runtimes, no longer serve a purpose. Reads and lists now share one client and writes and removes another, both over one reqwest client, so every request reuses the same connection. Verified: cargo test --lib io::vss_store (25 passed), cargo clippy --lib (no new warnings in vss_store.rs) --- src/io/vss_store.rs | 89 +++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 60 deletions(-) diff --git a/src/io/vss_store.rs b/src/io/vss_store.rs index a3eb34e1b8..4e2d27f0a5 100644 --- a/src/io/vss_store.rs +++ b/src/io/vss_store.rs @@ -313,16 +313,14 @@ impl VssStore { let internal_runtime = Arc::new(StoreRuntime::new("ldk-node-vss-runtime", INTERNAL_RUNTIME_WORKERS, "VSS")?); - // Writes share each side's HTTP client with its reads, so the first write reuses the - // connection the reads opened instead of connecting anew. - let blocking_http_client = build_http_client()?; - let async_http_client = build_http_client()?; + // Writes share the reads' HTTP client, so the first write reuses the connection the reads + // opened instead of connecting anew. + let http_client = build_http_client()?; - let sync_retry_policy = retry_policy(); - let blocking_client = VssClient::from_client_and_headers( + let client = VssClient::from_client_and_headers( base_url.clone(), - blocking_http_client.clone(), - sync_retry_policy, + http_client.clone(), + retry_policy(), header_provider.clone(), ); @@ -331,7 +329,7 @@ impl VssStore { // Not fenced: the schema version it may write is the same for every store. runtime_handle.block_on(async { determine_and_write_schema_version( - &blocking_client, + &client, &store_id, data_encryption_key, &key_obfuscator, @@ -343,34 +341,19 @@ impl VssStore { return Err(Error::new(ErrorKind::Other, "VSS store was replaced while being built")); } - let async_retry_policy = retry_policy(); - let async_client = VssClient::from_client_and_headers( - base_url.clone(), - async_http_client.clone(), - async_retry_policy, - header_provider.clone(), - ); // Writes retry in `fenced_retry`, which bounds each attempt; each call counts in flight from // `StoreFence::begin_write` at its call site. - let blocking_write_client = VssClient::from_client_and_headers( - base_url.clone(), - blocking_http_client, - no_retry_policy(), - header_provider.clone(), - ); - let async_write_client = VssClient::from_client_and_headers( + let write_client = VssClient::from_client_and_headers( base_url, - async_http_client, + http_client, no_retry_policy(), header_provider, ); let inner = Arc::new(VssStoreInner::new( schema_version, - blocking_client, - async_client, - blocking_write_client, - async_write_client, + client, + write_client, store_id, fence, data_encryption_key, @@ -431,9 +414,7 @@ impl KVStoreSync for VssStore { let key = key.to_string(); let inner = Arc::clone(&self.inner); let fut = async move { - inner - .read_internal(&inner.blocking_client, primary_namespace, secondary_namespace, key) - .await + inner.read_internal(&inner.client, primary_namespace, secondary_namespace, key).await }; tokio::task::block_in_place(move || internal_runtime.handle().block_on(fut)) } @@ -457,7 +438,7 @@ impl KVStoreSync for VssStore { let fut = async move { inner .write_internal( - &inner.blocking_write_client, + &inner.write_client, inner_lock_ref, locking_key, version, @@ -491,7 +472,7 @@ impl KVStoreSync for VssStore { let fut = async move { inner .remove_internal( - &inner.blocking_write_client, + &inner.write_client, inner_lock_ref, locking_key, version, @@ -520,9 +501,7 @@ impl KVStoreSync for VssStore { let secondary_namespace = secondary_namespace.to_string(); let inner = Arc::clone(&self.inner); let fut = async move { - inner - .list_internal(&inner.blocking_client, primary_namespace, secondary_namespace) - .await + inner.list_internal(&inner.client, primary_namespace, secondary_namespace).await }; tokio::task::block_in_place(move || internal_runtime.handle().block_on(fut)) } @@ -540,7 +519,7 @@ impl KVStore for VssStore { Box::pin(async move { let task = runtime.spawn(async move { inner - .read_internal(&inner.async_client, primary_namespace, secondary_namespace, key) + .read_internal(&inner.client, primary_namespace, secondary_namespace, key) .await }); task.await.map_err(|e| { @@ -564,7 +543,7 @@ impl KVStore for VssStore { let task = runtime.spawn(async move { inner .write_internal( - &inner.async_write_client, + &inner.write_client, inner_lock_ref, locking_key, version, @@ -596,7 +575,7 @@ impl KVStore for VssStore { let fut = async move { inner .remove_internal( - &inner.async_write_client, + &inner.write_client, inner_lock_ref, locking_key, version, @@ -630,9 +609,7 @@ impl KVStore for VssStore { let runtime = self.internal_runtime(); Box::pin(async move { let task = runtime.spawn(async move { - inner - .list_internal(&inner.async_client, primary_namespace, secondary_namespace) - .await + inner.list_internal(&inner.client, primary_namespace, secondary_namespace).await }); task.await.map_err(|e| { io::Error::new(io::ErrorKind::Other, format!("VSS runtime task failed: {}", e)) @@ -653,13 +630,10 @@ impl Drop for VssStore { struct VssStoreInner { schema_version: VssSchemaVersion, - blocking_client: VssClient, - // A secondary client that will only be used for async persistence via `KVStore`, to ensure TCP - // connections aren't shared between our outer and the internal runtime. - async_client: VssClient, - // Clients for writes and removes, which `fenced_retry` retries. - blocking_write_client: VssClient, - async_write_client: VssClient, + // Reads and lists, from both `KVStoreSync` and `KVStore`, which run on the internal runtime. + client: VssClient, + // Writes and removes, which `fenced_retry` retries. + write_client: VssClient, write_retry_policy: CustomRetryPolicy, store_id: String, fence: StoreFence, @@ -674,19 +648,15 @@ struct VssStoreInner { impl VssStoreInner { #[allow(clippy::too_many_arguments)] pub(crate) fn new( - schema_version: VssSchemaVersion, blocking_client: VssClient, - async_client: VssClient, - blocking_write_client: VssClient, - async_write_client: VssClient, store_id: String, fence: StoreFence, + schema_version: VssSchemaVersion, client: VssClient, + write_client: VssClient, store_id: String, fence: StoreFence, data_encryption_key: [u8; 32], key_obfuscator: KeyObfuscator, logger: Arc, ) -> Self { let locks = Mutex::new(HashMap::new()); Self { schema_version, - blocking_client, - async_client, - blocking_write_client, - async_write_client, + client, + write_client, write_retry_policy: retry_policy(), store_id, fence, @@ -1766,10 +1736,9 @@ mod connection_tests { KVStoreSync::write(&store, "monitors", "", "monitor", vec![1]).unwrap(); assert_eq!(connections.load(Ordering::SeqCst), 1); - // The async side keeps its own connection, which its writes reuse too. + // The async side shares the same connection. KVStore::read(&store, "monitors", "", "missing").await.unwrap_err(); - assert_eq!(connections.load(Ordering::SeqCst), 2); KVStore::write(&store, "monitors", "", "monitor", vec![2]).await.unwrap(); - assert_eq!(connections.load(Ordering::SeqCst), 2); + assert_eq!(connections.load(Ordering::SeqCst), 1); } }