@@ -7,6 +7,7 @@ use constants::*;
77use futures:: { Stream , StreamExt } ;
88use solana_transaction_status:: UiTransactionEncoding ;
99use tokio:: sync:: Mutex ;
10+ use tokio:: time:: timeout;
1011use std:: time:: Duration ;
1112use tracing:: { error, info, warn} ;
1213use types:: * ;
@@ -65,6 +66,7 @@ mod nft;
6566const METAPLEX_PROGRAM_ID : & str = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s" ;
6667
6768const FEE_CAP : u64 = 20000 ;
69+ const RESTART_SUBSCRIPTION_TIMEOUT_SECS : u64 = 180 ;
6870
6971const PLAYERS_REG_HEAD_LEN : usize = 152 ;
7072const PLAYERS_REG_INIT_LEN : usize = PLAYERS_REG_HEAD_LEN + 4 ;
@@ -1314,7 +1316,9 @@ impl TransportT for SolanaTransport {
13141316 }
13151317 } ;
13161318
1317- while let Some ( rpc_response) = stream. next( ) . await {
1319+ while let Ok ( Some ( rpc_response) ) = timeout(
1320+ Duration :: from_secs( RESTART_SUBSCRIPTION_TIMEOUT_SECS ) , stream. next( )
1321+ ) . await {
13181322 let ui_account: UiAccount = rpc_response. value;
13191323
13201324 let Some ( data) = ui_account. data. decode( ) else {
@@ -1363,6 +1367,12 @@ impl TransportT for SolanaTransport {
13631367 } ;
13641368 yield( Ok ( acc) ) ;
13651369 }
1370+
1371+ // We restart subscription regularly to avoid broken connection.
1372+ // A broken connection is the case where the connection is established,
1373+ // no error raises, but no further update is delivered.
1374+ unsub( ) . await ;
1375+ return ;
13661376 } ) )
13671377 }
13681378
0 commit comments