Add automatic websocket reconnection

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-03-11 23:41:06 +00:00
parent d02f958865
commit d2d20e2978
33 changed files with 838 additions and 664 deletions

View File

@@ -1,4 +1,4 @@
use super::Pending;
use super::State;
use crate::{config::Config, database};
use async_trait::async_trait;
use log::{debug, error, info};
@@ -21,7 +21,7 @@ impl super::Handler for Handler {
async fn handle_websocket_message(
&self,
pending: Arc<RwLock<Pending>>,
state: Arc<RwLock<State>>,
message: websocket::data::incoming::Message,
) {
match message {
@@ -32,19 +32,23 @@ impl super::Handler for Handler {
unreachable!()
};
let mut pending = pending.write().await;
let mut state = state.write().await;
let newly_subscribed = pending
.subscriptions
let newly_subscribed = state
.pending_subscriptions
.extract_if(|symbol, _| symbols.contains(symbol))
.collect::<HashMap<_, _>>();
let newly_unsubscribed = pending
.unsubscriptions
let newly_unsubscribed = state
.pending_unsubscriptions
.extract_if(|symbol, _| !symbols.contains(symbol))
.collect::<HashMap<_, _>>();
drop(pending);
state
.active_subscriptions
.extend(newly_subscribed.keys().cloned());
drop(state);
if !newly_subscribed.is_empty() {
info!(
@@ -108,4 +112,8 @@ impl super::Handler for Handler {
_ => unreachable!(),
}
}
fn log_string(&self) -> &'static str {
"news"
}
}