Timeout if we haven't received any messages in 60 seconds

Sometimes, it seems, Bluesky just stops sending us messages. I do not know why.
Let's just try to timeout if that ever happens again?
This commit is contained in:
Aleksei Voronov 2024-01-15 15:01:22 +01:00
parent 1555a803e9
commit 10d4556ff3
3 changed files with 9 additions and 5 deletions

2
Cargo.lock generated
View File

@ -2365,7 +2365,6 @@ dependencies = [
"clap",
"dotenv",
"env_logger",
"futures",
"http 1.0.0",
"libipld-core",
"lingua",
@ -2378,6 +2377,7 @@ dependencies = [
"sk-cbor",
"sqlx",
"tokio",
"tokio-stream",
"tokio-tungstenite",
]

View File

@ -18,7 +18,6 @@ chrono = "0.4.31"
clap = { version = "4.4.16", features = ["derive"] }
dotenv = "0.15.0"
env_logger = "0.10.1"
futures = "0.3.30"
http = "1.0.0"
libipld-core = { version = "0.16.0", features = ["serde-codec"] }
lingua = "1.6.2"
@ -31,4 +30,5 @@ serde_ipld_dagcbor = "0.4.2"
sk-cbor = "0.1.2"
sqlx = { version = "0.7.3", default-features = false, features = ["postgres", "runtime-tokio-native-tls", "chrono"] }
tokio = { version = "1.35.1", features = ["full"] }
tokio-stream = "0.1.14"
tokio-tungstenite = { version = "0.21.0", features = ["native-tls"] }

View File

@ -1,4 +1,5 @@
use std::matches;
use std::time::Duration;
use anyhow::{anyhow, Result};
use atrium_api::agent::{store::MemorySessionStore, AtpAgent};
@ -6,9 +7,9 @@ use atrium_api::blob::BlobRef;
use atrium_api::records::Record;
use atrium_xrpc_client::reqwest::ReqwestClient;
use chrono::Utc;
use futures::StreamExt;
use http::StatusCode;
use log::error;
use tokio_stream::StreamExt;
use tokio_tungstenite::{connect_async, tungstenite};
use super::entities::ProfileDetails;
@ -21,6 +22,7 @@ pub struct Bluesky {
impl Bluesky {
pub const XRPC_HOST: &'static str = "https://bsky.social";
pub const FIREHOSE_HOST: &'static str = "wss://bsky.network";
pub const STREAMING_TIMEOUT: Duration = Duration::from_secs(60);
pub fn unauthenticated() -> Self {
Self {
@ -152,9 +154,11 @@ impl Bluesky {
),
};
let (mut stream, _) = connect_async(url).await?;
let (stream, _) = connect_async(url).await?;
let stream = stream.timeout(Self::STREAMING_TIMEOUT);
let mut stream = Box::pin(stream);
while let Some(Ok(tungstenite::Message::Binary(message))) = stream.next().await {
while let Some(Ok(tungstenite::Message::Binary(message))) = stream.try_next().await? {
if let Err(e) = handle_message(&message, processor).await {
error!("Error handling a message: {:?}", e);
}