Skip to content

Commit 856a925

Browse files
committed
add expo backoff builder wrapper
1 parent d80ffc6 commit 856a925

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

lazer/sdk/rust/client/examples/subscribe_price_feeds.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use backoff::ExponentialBackoffBuilder;
21
use base64::Engine;
2+
use pyth_lazer_client::backoff::PythLazerExponentialBackoffBuilder;
33
use pyth_lazer_client::client::PythLazerClient;
44
use pyth_lazer_client::ws_connection::AnyResponse;
55
use pyth_lazer_protocol::message::{
@@ -40,9 +40,7 @@ async fn main() -> anyhow::Result<()> {
4040
],
4141
get_lazer_access_token(),
4242
4,
43-
ExponentialBackoffBuilder::default()
44-
.with_max_elapsed_time(None) // max_elapsed_time is not supported in Pyth Lazer client
45-
.build(),
43+
PythLazerExponentialBackoffBuilder::default().build(),
4644
)?;
4745

4846
let stream = client
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::time::Duration;
2+
3+
use backoff::{
4+
default::{INITIAL_INTERVAL_MILLIS, MAX_INTERVAL_MILLIS, MULTIPLIER, RANDOMIZATION_FACTOR},
5+
ExponentialBackoff, ExponentialBackoffBuilder,
6+
};
7+
8+
#[derive(Debug)]
9+
pub struct PythLazerExponentialBackoffBuilder {
10+
initial_interval: Duration,
11+
randomization_factor: f64,
12+
multiplier: f64,
13+
max_interval: Duration,
14+
}
15+
16+
impl Default for PythLazerExponentialBackoffBuilder {
17+
fn default() -> Self {
18+
Self {
19+
initial_interval: Duration::from_millis(INITIAL_INTERVAL_MILLIS),
20+
randomization_factor: RANDOMIZATION_FACTOR,
21+
multiplier: MULTIPLIER,
22+
max_interval: Duration::from_millis(MAX_INTERVAL_MILLIS),
23+
}
24+
}
25+
}
26+
27+
impl PythLazerExponentialBackoffBuilder {
28+
pub fn new() -> Self {
29+
Default::default()
30+
}
31+
32+
/// The initial retry interval.
33+
pub fn with_initial_interval(&mut self, initial_interval: Duration) -> &mut Self {
34+
self.initial_interval = initial_interval;
35+
self
36+
}
37+
38+
/// The randomization factor to use for creating a range around the retry interval.
39+
///
40+
/// A randomization factor of 0.5 results in a random period ranging between 50% below and 50%
41+
/// above the retry interval.
42+
pub fn with_randomization_factor(&mut self, randomization_factor: f64) -> &mut Self {
43+
self.randomization_factor = randomization_factor;
44+
self
45+
}
46+
47+
/// The value to multiply the current interval with for each retry attempt.
48+
pub fn with_multiplier(&mut self, multiplier: f64) -> &mut Self {
49+
self.multiplier = multiplier;
50+
self
51+
}
52+
53+
/// The maximum value of the back off period. Once the retry interval reaches this
54+
/// value it stops increasing.
55+
pub fn with_max_interval(&mut self, max_interval: Duration) -> &mut Self {
56+
self.max_interval = max_interval;
57+
self
58+
}
59+
60+
pub fn build(&self) -> ExponentialBackoff {
61+
ExponentialBackoffBuilder::default()
62+
.with_initial_interval(self.initial_interval)
63+
.with_randomization_factor(self.randomization_factor)
64+
.with_multiplier(self.multiplier)
65+
.with_max_interval(self.max_interval)
66+
.with_max_elapsed_time(None)
67+
.build()
68+
}
69+
}

lazer/sdk/rust/client/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const CHANNEL_CAPACITY: usize = 1000;
22

3+
pub mod backoff;
34
pub mod client;
45
pub mod resilient_ws_connection;
56
pub mod ws_connection;

lazer/sdk/rust/client/src/resilient_ws_connection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl PythLazerResilientWSConnectionTask {
169169
request: SubscribeRequest,
170170
) -> Result<()> {
171171
self.subscriptions.push(request.clone());
172-
return ws_connection.subscribe(request).await;
172+
ws_connection.subscribe(request).await
173173
}
174174

175175
pub async fn unsubscribe(
@@ -189,6 +189,6 @@ impl PythLazerResilientWSConnectionTask {
189189
request.subscription_id
190190
);
191191
}
192-
return ws_connection.unsubscribe(request).await;
192+
ws_connection.unsubscribe(request).await
193193
}
194194
}

0 commit comments

Comments
 (0)