@@ -6,7 +6,9 @@ use std::thread::sleep;
6
6
use std:: time:: Duration ;
7
7
8
8
use async_h1:: client;
9
+ use async_std:: future:: timeout;
9
10
use async_std:: net:: TcpStream ;
11
+ use async_std:: task;
10
12
use clarity:: vm:: analysis:: contract_interface_builder:: build_contract_interface;
11
13
use clarity:: vm:: costs:: ExecutionCost ;
12
14
use clarity:: vm:: events:: { FTEventType , NFTEventType , STXEventType } ;
@@ -318,27 +320,33 @@ impl EventObserver {
318
320
} ;
319
321
320
322
let backoff = Duration :: from_millis ( ( 1.0 * 1_000.0 ) as u64 ) ;
323
+ let connection_timeout = Duration :: from_secs ( 5 ) ;
321
324
322
325
loop {
323
326
let body = body. clone ( ) ;
324
327
let mut req = Request :: new ( Method :: Post , url. clone ( ) ) ;
325
328
req. append_header ( "Content-Type" , "application/json" ) ;
326
329
req. set_body ( body) ;
327
330
328
- let response = async_std:: task:: block_on ( async {
329
- let stream = match TcpStream :: connect ( self . endpoint . clone ( ) ) . await {
330
- Ok ( stream) => stream,
331
- Err ( err) => {
332
- warn ! ( "Event dispatcher: connection failed - {:?}" , err) ;
333
- return None ;
334
- }
335
- } ;
331
+ let response = task:: block_on ( async {
332
+ let stream =
333
+ match timeout ( connection_timeout, TcpStream :: connect ( & self . endpoint ) ) . await {
334
+ Ok ( Ok ( stream) ) => stream,
335
+ Ok ( Err ( err) ) => {
336
+ warn ! ( "Event dispatcher: connection failed - {:?}" , err) ;
337
+ return None ;
338
+ }
339
+ Err ( _) => {
340
+ error ! ( "Event dispatcher: connection attempt timed out" ) ;
341
+ return None ;
342
+ }
343
+ } ;
336
344
337
345
match client:: connect ( stream, req) . await {
338
346
Ok ( response) => Some ( response) ,
339
347
Err ( err) => {
340
348
warn ! ( "Event dispatcher: rpc invocation failed - {:?}" , err) ;
341
- return None ;
349
+ None
342
350
}
343
351
}
344
352
} ) ;
0 commit comments