Skip to content

Commit

Permalink
Add RetryPolicy to VssClient
Browse files Browse the repository at this point in the history
  • Loading branch information
G8XSU committed Jan 8, 2024
1 parent 860a1d5 commit 9d64f3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
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<
JitteredRetryPolicy<
MaxTotalDelayRetryPolicy<MaxAttemptsRetryPolicy<ExponentialBackoffRetryPolicy<VssError>>>,
>,
Box<dyn 'static + Send + Sync + Fn(&VssError) -> bool>,
>;

/// 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

0 comments on commit 9d64f3c

Please sign in to comment.