@@ -3,6 +3,7 @@ use std::io;
3
3
use std:: io:: ErrorKind ;
4
4
#[ cfg( test) ]
5
5
use std:: panic:: RefUnwindSafe ;
6
+ use std:: time:: Duration ;
6
7
7
8
use crate :: io:: utils:: check_namespace_key_validity;
8
9
use lightning:: util:: persist:: KVStore ;
@@ -15,21 +16,45 @@ use vss_client::types::{
15
16
DeleteObjectRequest , GetObjectRequest , KeyValue , ListKeyVersionsRequest , PutObjectRequest ,
16
17
Storable ,
17
18
} ;
19
+ use vss_client:: util:: retry:: {
20
+ ExponentialBackoffRetryPolicy , FilteredRetryPolicy , JitteredRetryPolicy ,
21
+ MaxAttemptsRetryPolicy , MaxTotalDelayRetryPolicy , RetryPolicy ,
22
+ } ;
18
23
use vss_client:: util:: storable_builder:: { EntropySource , StorableBuilder } ;
19
24
25
+ type CustomRetryPolicy = FilteredRetryPolicy <
26
+ JitteredRetryPolicy <
27
+ MaxTotalDelayRetryPolicy < MaxAttemptsRetryPolicy < ExponentialBackoffRetryPolicy < VssError > > > ,
28
+ > ,
29
+ Box < dyn Fn ( & VssError ) -> bool + ' static + Send + Sync > ,
30
+ > ;
31
+
20
32
/// A [`KVStore`] implementation that writes to and reads from a [VSS](https://github.com/lightningdevkit/vss-server/blob/main/README.md) backend.
21
33
pub struct VssStore {
22
- client : VssClient ,
34
+ client : VssClient < CustomRetryPolicy > ,
23
35
store_id : String ,
24
36
runtime : Runtime ,
25
37
storable_builder : StorableBuilder < RandEntropySource > ,
26
38
}
27
39
28
40
impl VssStore {
29
41
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 ( ) ) ;
31
42
let runtime = tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ;
32
43
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) ;
33
58
Self { client, store_id, runtime, storable_builder }
34
59
}
35
60
0 commit comments