From 40fdb3d2ac363735f6e83c92add06b34e4dce83f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 10 Jan 2022 18:58:46 -0500 Subject: [PATCH] WIP (not building) --- .../obs_gateway_websocket_handler.rs | 3 +- .../src/endpoints_ws/obs_twitch_thread.rs | 79 +++++++++++++------ 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs index b4fc95cdfb..721452d55b 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs @@ -3,6 +3,7 @@ use actix_rt::Runtime; use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web_actors::ws; use container_common::token::random_crockford_token::random_crockford_token; +//use futures::future::{BoxFuture, FutureExt}; use crate::endpoints_ws::obs_twitch_thread::ObsTwitchThread; use crate::server_state::ObsGatewayServerState; use crate::twitch::polling_websocket_client::PollingTwitchWebsocketClient; @@ -90,7 +91,7 @@ impl Actor for ObsGatewayWebSocket { let handle = Handle::current(); let twitch_thread = self.twitch_thread.clone(); - handle.spawn_blocking(move || { + handle.spawn_blocking(async move { let twitch_thread2 = twitch_thread.clone(); async move { twitch_thread2.run_until_exit().await; diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs index 90664cc116..0c7acb8e21 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs @@ -2,10 +2,10 @@ use crate::twitch::polling_websocket_client::PollingTwitchWebsocketClient; use crate::twitch::websocket_client::TwitchWebsocketClient; use log::error; use log::info; -use std::sync::atomic::AtomicBool; -use std::sync::{Arc, RwLock, PoisonError}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, RwLock, PoisonError, RwLockWriteGuard}; use std::thread::sleep; -use std::time::Duration; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::Instant; use tokio::runtime::Handle; use crate::twitch::oauth::oauth_token_refresher::OauthTokenRefresher; @@ -20,10 +20,15 @@ use crate::twitch::oauth::oauth_token_refresher::OauthTokenRefresher; // maybe just kill other sessions ? browser+uuid -> LRU. pub struct ObsTwitchThread { + inner_data: Arc>, +} + +struct InnerData { twitch_user_id: u32, token_refresher: OauthTokenRefresher, - twitch_client: Arc>, - last_requested: Arc>, + is_connected: bool, + twitch_client: TwitchWebsocketClient, + last_ping: SystemTime, } impl ObsTwitchThread { @@ -34,30 +39,60 @@ impl ObsTwitchThread { ) -> Self { let now = Instant::now(); Self { - twitch_user_id, - token_refresher, - twitch_client: Arc::new(RwLock::new(twitch_client)), - last_requested: Arc::new(RwLock::new(now)), + inner_data: Arc::new(RwLock::new(InnerData { + twitch_user_id, + token_refresher, + is_connected: false, + twitch_client, + last_ping: UNIX_EPOCH, + })) } } pub async fn run_until_exit(&self) { loop { - info!("TwitchThread::run_until_exit()"); + //info!("TwitchThread::run_until_exit()"); + //let is_connected = self.is_connected.load(Ordering::Relaxed); + + //if !is_connected { + // info!("Connect to Twitch"); + // self.connect().await; + //} + + self.maybe_send_ping().await; + sleep(Duration::from_millis(1000)); } } - pub async fn send_ping(&self) { - //self.twitch_client.connect().await.unwrap(); + pub async fn connect(&self) { + //let mut write = self.twitch_client.write().unwrap(); + //let _r = write.connect().await.unwrap(); + //self.is_connected.store(true, Ordering::Relaxed); } + pub async fn maybe_send_ping(&self) { + let inner_clone = self.inner_data.clone(); + { + match inner_clone.write() { + Err(_) => {} + Ok(mut inner_data) => { + let now = SystemTime::now(); + let duration = now.duration_since(inner_data.last_ping).unwrap(); + let delta = Duration::from_secs(60); + if duration.lt(&delta) { + return; + } + info!("Sending ping..."); + inner_data.twitch_client.send_ping().await.unwrap(); - - - + inner_data.last_ping = now; + } + }; + } + } pub fn keep_alive_thread(&self) { // TODO(bt, 2022-01-03): builds are failing, not sure when I worked on this last @@ -74,13 +109,13 @@ impl ObsTwitchThread { // TODO: Investigate use of a thread pool instead. pub fn start_thread(&self) { - let mut last_requested_lock = self.last_requested.clone(); - - let handle = Handle::current(); - - handle.spawn(async move { - Self::thread_main_loop(last_requested_lock); - }); +// let mut last_requested_lock = self.last_requested.clone(); +// +// let handle = Handle::current(); +// +// handle.spawn(async move { +// Self::thread_main_loop(last_requested_lock); +// }); } fn thread_main_loop(