Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RetryPolicy to VssClient #226

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ libc = "0.2"
uniffi = { version = "0.25.1", features = ["build"], optional = true }

[target.'cfg(vss)'.dependencies]
vss-client = "0.1"
vss-client = "0.2"
prost = { version = "0.11.6", default-features = false}

[target.'cfg(windows)'.dependencies]
Expand Down
29 changes: 27 additions & 2 deletions src/io/vss_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io;
use std::io::ErrorKind;
#[cfg(test)]
use std::panic::RefUnwindSafe;
use std::time::Duration;

use crate::io::utils::check_namespace_key_validity;
use lightning::util::persist::KVStore;
Expand All @@ -15,21 +16,45 @@ use vss_client::types::{
DeleteObjectRequest, GetObjectRequest, KeyValue, ListKeyVersionsRequest, PutObjectRequest,
Storable,
};
use vss_client::util::retry::{
ExponentialBackoffRetryPolicy, FilteredRetryPolicy, JitteredRetryPolicy,
MaxAttemptsRetryPolicy, MaxTotalDelayRetryPolicy, RetryPolicy,
};
use vss_client::util::storable_builder::{EntropySource, StorableBuilder};

type CustomRetryPolicy = FilteredRetryPolicy<
tnull marked this conversation as resolved.
Show resolved Hide resolved
JitteredRetryPolicy<
MaxTotalDelayRetryPolicy<MaxAttemptsRetryPolicy<ExponentialBackoffRetryPolicy<VssError>>>,
>,
Box<dyn Fn(&VssError) -> bool + 'static + Send + Sync>,
>;

/// A [`KVStore`] implementation that writes to and reads from a [VSS](https://github.com/lightningdevkit/vss-server/blob/main/README.md) backend.
pub struct VssStore {
client: VssClient,
client: VssClient<CustomRetryPolicy>,
store_id: String,
runtime: Runtime,
storable_builder: StorableBuilder<RandEntropySource>,
}

impl VssStore {
pub(crate) fn new(base_url: String, store_id: String, data_encryption_key: [u8; 32]) -> Self {
let client = VssClient::new(base_url.as_str());
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
let storable_builder = StorableBuilder::new(data_encryption_key, RandEntropySource);
let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
.with_max_attempts(3)
.with_max_total_delay(Duration::from_secs(2))
.with_max_jitter(Duration::from_millis(50))
.skip_retry_on_error(Box::new(|e: &VssError| {
matches!(
e,
VssError::NoSuchKeyError(..)
| VssError::InvalidRequestError(..)
| VssError::ConflictError(..)
)
}) as _);

let client = VssClient::new(&base_url, retry_policy);
Self { client, store_id, runtime, storable_builder }
}

Expand Down
Loading