Skip to content

Commit 0931845

Browse files
committed
add pyth lazer client builder
1 parent 008f025 commit 0931845

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Duration;
22

33
use base64::Engine;
44
use pyth_lazer_client::backoff::PythLazerExponentialBackoffBuilder;
5-
use pyth_lazer_client::client::PythLazerClient;
5+
use pyth_lazer_client::client::PythLazerClientBuilder;
66
use pyth_lazer_client::ws_connection::AnyResponse;
77
use pyth_lazer_protocol::message::{
88
EvmMessage, LeEcdsaMessage, LeUnsignedMessage, Message, SolanaMessage,
@@ -35,22 +35,23 @@ async fn main() -> anyhow::Result<()> {
3535
.init();
3636

3737
// Create and start the client
38-
let mut client = PythLazerClient::new(
39-
vec![
38+
let mut client = PythLazerClientBuilder::new(get_lazer_access_token())
39+
// Optionally override the default endpoints
40+
.with_endpoints(vec![
4041
"wss://pyth-lazer-0.dourolabs.app/v1/stream".parse()?,
4142
"wss://pyth-lazer-1.dourolabs.app/v1/stream".parse()?,
42-
],
43-
get_lazer_access_token(),
44-
4,
45-
PythLazerExponentialBackoffBuilder::default().build(),
46-
Duration::from_secs(5), // Timeout for each connection
47-
)?;
43+
])
44+
// Optionally set the number of connections
45+
.with_num_connections(4)
46+
// Optionally set the backoff strategy
47+
.with_backoff(PythLazerExponentialBackoffBuilder::default().build())
48+
// Optionally set the timeout for each connection
49+
.with_timeout(Duration::from_secs(5))
50+
// Optionally set the channel capacity for responses
51+
.with_channel_capacity(1000)
52+
.build()?;
4853

49-
let stream = client
50-
.start(
51-
1000, // Use a channel capacity of 1000
52-
)
53-
.await?;
54+
let stream = client.start().await?;
5455
pin!(stream);
5556

5657
let subscription_requests = vec![

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ use url::Url;
1515
const DEDUP_CACHE_SIZE: usize = 100_000;
1616
const DEDUP_TTL: Duration = Duration::from_secs(10);
1717

18+
const DEFAULT_ENDPOINTS: [&str; 2] = [
19+
"wss://pyth-lazer-0.dourolabs.app/v1/stream",
20+
"wss://pyth-lazer-1.dourolabs.app/v1/stream",
21+
];
22+
const DEFAULT_NUM_CONNECTIONS: usize = 4;
23+
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
24+
1825
pub struct PythLazerClient {
1926
endpoints: Vec<Url>,
2027
access_token: String,
2128
num_connections: usize,
2229
ws_connections: Vec<PythLazerResilientWSConnection>,
2330
backoff: ExponentialBackoff,
2431
timeout: Duration,
32+
channel_capacity: usize,
2533
}
2634

2735
impl PythLazerClient {
@@ -37,6 +45,7 @@ impl PythLazerClient {
3745
num_connections: usize,
3846
backoff: ExponentialBackoff,
3947
timeout: Duration,
48+
channel_capacity: usize,
4049
) -> Result<Self> {
4150
if backoff.max_elapsed_time.is_some() {
4251
bail!("max_elapsed_time is not supported in Pyth Lazer client");
@@ -51,11 +60,12 @@ impl PythLazerClient {
5160
ws_connections: Vec::with_capacity(num_connections),
5261
backoff,
5362
timeout,
63+
channel_capacity,
5464
})
5565
}
5666

57-
pub async fn start(&mut self, channel_capacity: usize) -> Result<mpsc::Receiver<AnyResponse>> {
58-
let (sender, receiver) = mpsc::channel::<AnyResponse>(channel_capacity);
67+
pub async fn start(&mut self) -> Result<mpsc::Receiver<AnyResponse>> {
68+
let (sender, receiver) = mpsc::channel::<AnyResponse>(self.channel_capacity);
5969
let (ws_connection_sender, mut ws_connection_receiver) =
6070
mpsc::channel::<AnyResponse>(CHANNEL_CAPACITY);
6171

@@ -113,3 +123,64 @@ impl PythLazerClient {
113123
Ok(())
114124
}
115125
}
126+
127+
pub struct PythLazerClientBuilder {
128+
endpoints: Vec<Url>,
129+
access_token: String,
130+
num_connections: usize,
131+
backoff: ExponentialBackoff,
132+
timeout: Duration,
133+
channel_capacity: usize,
134+
}
135+
136+
impl PythLazerClientBuilder {
137+
pub fn new(access_token: String) -> Self {
138+
Self {
139+
endpoints: DEFAULT_ENDPOINTS
140+
.iter()
141+
.map(|&s| s.parse().unwrap())
142+
.collect(),
143+
access_token,
144+
num_connections: DEFAULT_NUM_CONNECTIONS,
145+
backoff: ExponentialBackoff::default(),
146+
timeout: DEFAULT_TIMEOUT,
147+
channel_capacity: CHANNEL_CAPACITY,
148+
}
149+
}
150+
151+
pub fn with_endpoints(mut self, endpoints: Vec<Url>) -> Self {
152+
self.endpoints = endpoints;
153+
self
154+
}
155+
156+
pub fn with_num_connections(mut self, num_connections: usize) -> Self {
157+
self.num_connections = num_connections;
158+
self
159+
}
160+
161+
pub fn with_backoff(mut self, backoff: ExponentialBackoff) -> Self {
162+
self.backoff = backoff;
163+
self
164+
}
165+
166+
pub fn with_timeout(mut self, timeout: Duration) -> Self {
167+
self.timeout = timeout;
168+
self
169+
}
170+
171+
pub fn with_channel_capacity(mut self, channel_capacity: usize) -> Self {
172+
self.channel_capacity = channel_capacity;
173+
self
174+
}
175+
176+
pub fn build(self) -> Result<PythLazerClient> {
177+
PythLazerClient::new(
178+
self.endpoints,
179+
self.access_token,
180+
self.num_connections,
181+
self.backoff,
182+
self.timeout,
183+
self.channel_capacity,
184+
)
185+
}
186+
}

0 commit comments

Comments
 (0)