Skip to content

Commit e6b0c0f

Browse files
authored
Merge pull request #226 from G8XSU/vss-retry-test-2
Add RetryPolicy to VssClient
2 parents 8eba327 + 9c1bc67 commit e6b0c0f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ libc = "0.2"
6868
uniffi = { version = "0.25.1", features = ["build"], optional = true }
6969

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

7474
[target.'cfg(windows)'.dependencies]

src/io/vss_store.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io;
33
use std::io::ErrorKind;
44
#[cfg(test)]
55
use std::panic::RefUnwindSafe;
6+
use std::time::Duration;
67

78
use crate::io::utils::check_namespace_key_validity;
89
use lightning::util::persist::KVStore;
@@ -15,21 +16,45 @@ use vss_client::types::{
1516
DeleteObjectRequest, GetObjectRequest, KeyValue, ListKeyVersionsRequest, PutObjectRequest,
1617
Storable,
1718
};
19+
use vss_client::util::retry::{
20+
ExponentialBackoffRetryPolicy, FilteredRetryPolicy, JitteredRetryPolicy,
21+
MaxAttemptsRetryPolicy, MaxTotalDelayRetryPolicy, RetryPolicy,
22+
};
1823
use vss_client::util::storable_builder::{EntropySource, StorableBuilder};
1924

25+
type CustomRetryPolicy = FilteredRetryPolicy<
26+
JitteredRetryPolicy<
27+
MaxTotalDelayRetryPolicy<MaxAttemptsRetryPolicy<ExponentialBackoffRetryPolicy<VssError>>>,
28+
>,
29+
Box<dyn Fn(&VssError) -> bool + 'static + Send + Sync>,
30+
>;
31+
2032
/// A [`KVStore`] implementation that writes to and reads from a [VSS](https://github.com/lightningdevkit/vss-server/blob/main/README.md) backend.
2133
pub struct VssStore {
22-
client: VssClient,
34+
client: VssClient<CustomRetryPolicy>,
2335
store_id: String,
2436
runtime: Runtime,
2537
storable_builder: StorableBuilder<RandEntropySource>,
2638
}
2739

2840
impl VssStore {
2941
pub(crate) fn new(base_url: String, store_id: String, data_encryption_key: [u8; 32]) -> Self {
30-
let client = VssClient::new(base_url.as_str());
3142
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
3243
let storable_builder = StorableBuilder::new(data_encryption_key, RandEntropySource);
44+
let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
45+
.with_max_attempts(3)
46+
.with_max_total_delay(Duration::from_secs(2))
47+
.with_max_jitter(Duration::from_millis(50))
48+
.skip_retry_on_error(Box::new(|e: &VssError| {
49+
matches!(
50+
e,
51+
VssError::NoSuchKeyError(..)
52+
| VssError::InvalidRequestError(..)
53+
| VssError::ConflictError(..)
54+
)
55+
}) as _);
56+
57+
let client = VssClient::new(&base_url, retry_policy);
3358
Self { client, store_id, runtime, storable_builder }
3459
}
3560

0 commit comments

Comments
 (0)