Skip to content

Commit 6c78cae

Browse files
committed
add proper backoff wrapper
1 parent 8518f90 commit 6c78cae

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lazer/sdk/rust/client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-client"
3-
version = "1.0.0"
3+
version = "2.0.0"
44
edition = "2021"
55
description = "A Rust client for Pyth Lazer"
66
license = "Apache-2.0"

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ use backoff::{
55
ExponentialBackoff, ExponentialBackoffBuilder,
66
};
77

8+
#[derive(Debug)]
9+
pub struct PythLazerExponentialBackoff {
10+
initial_interval: Duration,
11+
randomization_factor: f64,
12+
multiplier: f64,
13+
max_interval: Duration,
14+
}
15+
16+
impl From<PythLazerExponentialBackoff> for ExponentialBackoff {
17+
fn from(val: PythLazerExponentialBackoff) -> Self {
18+
ExponentialBackoffBuilder::default()
19+
.with_initial_interval(val.initial_interval)
20+
.with_randomization_factor(val.randomization_factor)
21+
.with_multiplier(val.multiplier)
22+
.with_max_interval(val.max_interval)
23+
.with_max_elapsed_time(None)
24+
.build()
25+
}
26+
}
27+
828
#[derive(Debug)]
929
pub struct PythLazerExponentialBackoffBuilder {
1030
initial_interval: Duration,
@@ -57,13 +77,12 @@ impl PythLazerExponentialBackoffBuilder {
5777
self
5878
}
5979

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()
80+
pub fn build(&self) -> PythLazerExponentialBackoff {
81+
PythLazerExponentialBackoff {
82+
initial_interval: self.initial_interval,
83+
randomization_factor: self.randomization_factor,
84+
multiplier: self.multiplier,
85+
max_interval: self.max_interval,
86+
}
6887
}
6988
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::time::Duration;
22

33
use crate::{
4-
resilient_ws_connection::PythLazerResilientWSConnection, ws_connection::AnyResponse,
4+
backoff::{PythLazerExponentialBackoff, PythLazerExponentialBackoffBuilder},
5+
resilient_ws_connection::PythLazerResilientWSConnection,
6+
ws_connection::AnyResponse,
57
CHANNEL_CAPACITY,
68
};
79
use anyhow::{bail, Result};
@@ -43,13 +45,10 @@ impl PythLazerClient {
4345
endpoints: Vec<Url>,
4446
access_token: String,
4547
num_connections: usize,
46-
backoff: ExponentialBackoff,
48+
backoff: PythLazerExponentialBackoff,
4749
timeout: Duration,
4850
channel_capacity: usize,
4951
) -> Result<Self> {
50-
if backoff.max_elapsed_time.is_some() {
51-
bail!("max_elapsed_time is not supported in Pyth Lazer client");
52-
}
5352
if endpoints.is_empty() {
5453
bail!("At least one endpoint must be provided");
5554
}
@@ -58,7 +57,7 @@ impl PythLazerClient {
5857
access_token,
5958
num_connections,
6059
ws_connections: Vec::with_capacity(num_connections),
61-
backoff,
60+
backoff: backoff.into(),
6261
timeout,
6362
channel_capacity,
6463
})
@@ -128,7 +127,7 @@ pub struct PythLazerClientBuilder {
128127
endpoints: Vec<Url>,
129128
access_token: String,
130129
num_connections: usize,
131-
backoff: ExponentialBackoff,
130+
backoff: PythLazerExponentialBackoff,
132131
timeout: Duration,
133132
channel_capacity: usize,
134133
}
@@ -142,7 +141,7 @@ impl PythLazerClientBuilder {
142141
.collect(),
143142
access_token,
144143
num_connections: DEFAULT_NUM_CONNECTIONS,
145-
backoff: ExponentialBackoff::default(),
144+
backoff: PythLazerExponentialBackoffBuilder::default().build(),
146145
timeout: DEFAULT_TIMEOUT,
147146
channel_capacity: CHANNEL_CAPACITY,
148147
}
@@ -158,7 +157,7 @@ impl PythLazerClientBuilder {
158157
self
159158
}
160159

161-
pub fn with_backoff(mut self, backoff: ExponentialBackoff) -> Self {
160+
pub fn with_backoff(mut self, backoff: PythLazerExponentialBackoff) -> Self {
162161
self.backoff = backoff;
163162
self
164163
}

0 commit comments

Comments
 (0)