Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 29 additions & 60 deletions src/io/vss_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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))
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
Expand All @@ -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| {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand All @@ -653,13 +630,10 @@ impl Drop for VssStore {

struct VssStoreInner {
schema_version: VssSchemaVersion,
blocking_client: VssClient<CustomRetryPolicy>,
// 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<CustomRetryPolicy>,
// Clients for writes and removes, which `fenced_retry` retries.
blocking_write_client: VssClient<CustomRetryPolicy>,
async_write_client: VssClient<CustomRetryPolicy>,
// Reads and lists, from both `KVStoreSync` and `KVStore`, which run on the internal runtime.
client: VssClient<CustomRetryPolicy>,
// Writes and removes, which `fenced_retry` retries.
write_client: VssClient<CustomRetryPolicy>,
write_retry_policy: CustomRetryPolicy,
store_id: String,
fence: StoreFence,
Expand All @@ -674,19 +648,15 @@ struct VssStoreInner {
impl VssStoreInner {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
schema_version: VssSchemaVersion, blocking_client: VssClient<CustomRetryPolicy>,
async_client: VssClient<CustomRetryPolicy>,
blocking_write_client: VssClient<CustomRetryPolicy>,
async_write_client: VssClient<CustomRetryPolicy>, store_id: String, fence: StoreFence,
schema_version: VssSchemaVersion, client: VssClient<CustomRetryPolicy>,
write_client: VssClient<CustomRetryPolicy>, store_id: String, fence: StoreFence,
data_encryption_key: [u8; 32], key_obfuscator: KeyObfuscator, logger: Arc<Logger>,
) -> 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,
Expand Down Expand Up @@ -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);
}
}
Loading