@@ -15,13 +15,21 @@ use url::Url;
1515const DEDUP_CACHE_SIZE : usize = 100_000 ;
1616const 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+
1825pub 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
2735impl 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