Skip to content

Commit 8b140f1

Browse files
author
DogLooksGood
committed
Restart every 180 seconds if there's no updates from subscription
1 parent ca83402 commit 8b140f1

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

transport/src/solana.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use constants::*;
77
use futures::{Stream, StreamExt};
88
use solana_transaction_status::UiTransactionEncoding;
99
use tokio::sync::Mutex;
10+
use tokio::time::timeout;
1011
use std::time::Duration;
1112
use tracing::{error, info, warn};
1213
use types::*;
@@ -65,6 +66,7 @@ mod nft;
6566
const METAPLEX_PROGRAM_ID: &str = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
6667

6768
const FEE_CAP: u64 = 20000;
69+
const RESTART_SUBSCRIPTION_TIMEOUT_SECS: u64 = 180;
6870

6971
const PLAYERS_REG_HEAD_LEN: usize = 152;
7072
const 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

Comments
 (0)