@@ -35,10 +35,12 @@ impl PythLazerResilientWSConnection {
3535 endpoint : Url ,
3636 access_token : String ,
3737 backoff : ExponentialBackoff ,
38+ timeout : Duration ,
3839 sender : mpsc:: Sender < AnyResponse > ,
3940 ) -> Self {
4041 let ( request_sender, mut request_receiver) = mpsc:: channel ( CHANNEL_CAPACITY ) ;
41- let mut task = PythLazerResilientWSConnectionTask :: new ( endpoint, access_token, backoff) ;
42+ let mut task =
43+ PythLazerResilientWSConnectionTask :: new ( endpoint, access_token, backoff, timeout) ;
4244
4345 tokio:: spawn ( async move {
4446 if let Err ( e) = task. run ( sender, & mut request_receiver) . await {
@@ -71,15 +73,22 @@ struct PythLazerResilientWSConnectionTask {
7173 access_token : String ,
7274 subscriptions : Vec < SubscribeRequest > ,
7375 backoff : ExponentialBackoff ,
76+ timeout : Duration ,
7477}
7578
7679impl PythLazerResilientWSConnectionTask {
77- pub fn new ( endpoint : Url , access_token : String , backoff : ExponentialBackoff ) -> Self {
80+ pub fn new (
81+ endpoint : Url ,
82+ access_token : String ,
83+ backoff : ExponentialBackoff ,
84+ timeout : Duration ,
85+ ) -> Self {
7886 Self {
7987 endpoint,
8088 access_token,
8189 subscriptions : Vec :: new ( ) ,
8290 backoff,
91+ timeout,
8392 }
8493 }
8594
@@ -130,10 +139,12 @@ impl PythLazerResilientWSConnectionTask {
130139 . await ?;
131140 }
132141 loop {
142+ let timeout_response = tokio:: time:: timeout ( self . timeout , stream. next ( ) ) ;
143+
133144 select ! {
134- response = stream . next ( ) => {
145+ response = timeout_response => {
135146 match response {
136- Some ( response) => match response {
147+ Ok ( Some ( response) ) => match response {
137148 Ok ( response) => {
138149 sender
139150 . send( response)
@@ -144,9 +155,12 @@ impl PythLazerResilientWSConnectionTask {
144155 bail!( "WebSocket stream error: {}" , e) ;
145156 }
146157 } ,
147- None => {
158+ Ok ( None ) => {
148159 bail!( "WebSocket stream ended unexpectedly" ) ;
149160 }
161+ Err ( _elapsed) => {
162+ bail!( "WebSocket stream timed out" ) ;
163+ }
150164 }
151165 }
152166 Some ( request) = request_receiver. recv( ) => {
0 commit comments