Fix lint, socket echo

This commit is contained in:
Jai A
2024-12-12 14:36:02 -08:00
parent c970e9c015
commit 1f060b8513
3 changed files with 38 additions and 11 deletions

View File

@@ -180,8 +180,7 @@ pub async fn ws_init(
} }
Ok(AggregatedMessage::Ping(msg)) => { Ok(AggregatedMessage::Ping(msg)) => {
if let Some(mut socket) = if let Some(mut socket) = db.auth_sockets.get_mut(&user.id)
db.auth_sockets.get_mut(&user.id.into())
{ {
let (_, socket) = socket.value_mut(); let (_, socket) = socket.value_mut();
let _ = socket.pong(&msg).await; let _ = socket.pong(&msg).await;

View File

@@ -29,9 +29,7 @@ pub struct FriendsSocket {
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct UserFriend { pub struct UserFriend {
pub id: String, pub id: String,
// TODO: Remove this optional and serde alias on release pub friend_id: String,
pub friend_id: Option<String>,
#[serde(alias = "pending")]
pub accepted: bool, pub accepted: bool,
pub created: DateTime<Utc>, pub created: DateTime<Utc>,
} }
@@ -72,6 +70,7 @@ impl FriendsSocket {
} }
} }
#[tracing::instrument(skip_all)]
pub async fn connect( pub async fn connect(
&self, &self,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy, exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
@@ -144,8 +143,20 @@ impl FriendsSocket {
) )
.ok() .ok()
} }
Message::Ping(_) Message::Ping(bytes) => {
| Message::Pong(_) if let Some(write) = write_handle
.write()
.await
.as_mut()
{
let _ = write
.send(Message::Pong(bytes))
.await;
}
continue;
}
Message::Pong(_)
| Message::Frame(_) => continue, | Message::Frame(_) => continue,
Message::Close(_) => break, Message::Close(_) => break,
}; };
@@ -175,8 +186,7 @@ impl FriendsSocket {
} }
} }
Err(e) => { Err(e) => {
println!("WebSocket error: {:?}", e); tracing::error!("Error handling message from websocket server: {:?}", e);
break;
} }
} }
} }
@@ -198,11 +208,13 @@ impl FriendsSocket {
Ok(()) Ok(())
} }
pub async fn reconnect_task() -> crate::Result<()> { #[tracing::instrument(skip_all)]
pub async fn socket_loop() -> crate::Result<()> {
let state = crate::State::get().await?; let state = crate::State::get().await?;
tokio::task::spawn(async move { tokio::task::spawn(async move {
let mut last_connection = Utc::now(); let mut last_connection = Utc::now();
let mut last_ping = Utc::now();
loop { loop {
let connected = { let connected = {
@@ -215,6 +227,7 @@ impl FriendsSocket {
> chrono::Duration::seconds(30) > chrono::Duration::seconds(30)
{ {
last_connection = Utc::now(); last_connection = Utc::now();
last_ping = Utc::now();
let _ = state let _ = state
.friends_socket .friends_socket
.connect( .connect(
@@ -223,6 +236,15 @@ impl FriendsSocket {
&state.process_manager, &state.process_manager,
) )
.await; .await;
} else if connected
&& Utc::now().signed_duration_since(last_ping)
> chrono::Duration::seconds(10)
{
last_ping = Utc::now();
let mut write = state.friends_socket.write.write().await;
if let Some(write) = write.as_mut() {
let _ = write.send(Message::Ping(Vec::new())).await;
}
} }
tokio::time::sleep(std::time::Duration::from_secs(1)).await; tokio::time::sleep(std::time::Duration::from_secs(1)).await;
@@ -232,6 +254,7 @@ impl FriendsSocket {
Ok(()) Ok(())
} }
#[tracing::instrument(skip(self))]
pub async fn disconnect(&self) -> crate::Result<()> { pub async fn disconnect(&self) -> crate::Result<()> {
let mut write_lock = self.write.write().await; let mut write_lock = self.write.write().await;
if let Some(ref mut write_half) = *write_lock { if let Some(ref mut write_half) = *write_lock {
@@ -241,6 +264,7 @@ impl FriendsSocket {
Ok(()) Ok(())
} }
#[tracing::instrument(skip(self))]
pub async fn update_status( pub async fn update_status(
&self, &self,
profile_name: Option<String>, profile_name: Option<String>,
@@ -257,6 +281,7 @@ impl FriendsSocket {
Ok(()) Ok(())
} }
#[tracing::instrument(skip_all)]
pub async fn friends( pub async fn friends(
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy, exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore, semaphore: &FetchSemaphore,
@@ -272,6 +297,7 @@ impl FriendsSocket {
.await .await
} }
#[tracing::instrument(skip(self))]
pub fn friend_statuses(&self) -> Vec<UserStatus> { pub fn friend_statuses(&self) -> Vec<UserStatus> {
self.user_statuses self.user_statuses
.iter() .iter()
@@ -279,6 +305,7 @@ impl FriendsSocket {
.collect() .collect()
} }
#[tracing::instrument(skip(exec, semaphore))]
pub async fn add_friend( pub async fn add_friend(
user_id: &str, user_id: &str,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy, exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
@@ -299,6 +326,7 @@ impl FriendsSocket {
Ok(()) Ok(())
} }
#[tracing::instrument(skip(exec, semaphore))]
pub async fn remove_friend( pub async fn remove_friend(
user_id: &str, user_id: &str,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy, exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,

View File

@@ -96,7 +96,7 @@ impl State {
&state.process_manager, &state.process_manager,
) )
.await; .await;
let _ = FriendsSocket::reconnect_task().await; let _ = FriendsSocket::socket_loop().await;
}); });
Ok(()) Ok(())