Skip to content

Commit

Permalink
feat: remove WsId from ChatServer and pass it in
Browse files Browse the repository at this point in the history
Allows more separation in prep for refactor
  • Loading branch information
c-git committed Dec 13, 2024
1 parent a27fb49 commit fad556f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
1 change: 1 addition & 0 deletions crates/plugin-chat/src/server_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod server;

pub use connections::{chat_get_token, chat_ws_start_session};
pub use plugin_impl::{ChatPlugin, ChatPluginConfig, ChatSettings};
pub use server::ChatServerHandle; // TODO 1: Remove export once refactoring is completed
31 changes: 13 additions & 18 deletions crates/plugin-chat/src/server_only/client_control_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures_util::StreamExt as _;
use std::{pin::pin, sync::Arc, time::Instant};
use tokio::{select, sync::mpsc};
use tracing::{debug, error, info, instrument, warn, Span};
use ws_auth::{validate_ws_connection, AuthTokenManager, WsConnId};
use ws_auth::{validate_ws_connection, AuthTokenManager, WsConnId, WsId};
use wykies_shared::{
const_config::CHANNEL_BUFFER_SIZE, debug_panic, host_branch::HostId, log_err_as_error,
};
Expand All @@ -23,24 +23,19 @@ pub async fn chat_ws_start_client_handler_loop(
msg_stream: actix_ws::MessageStream,
auth_manager: web::Data<AuthTokenManager>,
client_identifier: HostId,
ws_id: WsId,
) {
let (user_info, msg_stream) = match validate_ws_connection(
msg_stream,
auth_manager,
&client_identifier,
chat_server_handle.ws_id(),
)
.await
{
Ok(msg_stream) => msg_stream,
Err(e) => {
// Connection not validated exit
error!("Failed to validate web socket connection with error: {e:?}");
let _ = session.close(Some(CloseCode::Error.into())).await;
debug_panic!(e);
return;
}
};
let (user_info, msg_stream) =
match validate_ws_connection(msg_stream, auth_manager, &client_identifier, ws_id).await {
Ok(msg_stream) => msg_stream,
Err(e) => {
// Connection not validated exit
error!("Failed to validate web socket connection with error: {e:?}");
let _ = session.close(Some(CloseCode::Error.into())).await;
debug_panic!(e);
return;
}
};

let mut last_heartbeat = Instant::now();
let mut heartbeat_interval = chat_server_handle.heartbeat_config.interval();
Expand Down
4 changes: 3 additions & 1 deletion crates/plugin-chat/src/server_only/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ pub async fn chat_ws_start_session(
chat_server_handle: web::Data<ChatServerHandle>,
auth_manager: web::Data<AuthTokenManager>,
conn: ConnectionInfo,
ws_id: WsId,
) -> Result<HttpResponse, WebSocketAuthError> {
let (session, msg_stream, client_identifier, res) =
create_ws_session(req, stream, conn, &auth_manager, chat_server_handle.ws_id())?;
create_ws_session(req, stream, conn, &auth_manager, ws_id)?;

// spawn websocket handler (don't await) so response is sent immediately
spawn_local(chat_ws_start_client_handler_loop(
Expand All @@ -27,6 +28,7 @@ pub async fn chat_ws_start_session(
msg_stream,
auth_manager,
client_identifier,
ws_id,
));

Ok(res)
Expand Down
9 changes: 2 additions & 7 deletions crates/plugin-chat/src/server_only/plugin_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ impl ServerPlugin for ChatPlugin {
ws_config: &wykies_server::WebSocketSettings,
) -> anyhow::Result<wykies_server::plugin::ServerPluginArtifacts<Self::Task, Self::Handle>>
{
let (chat_server, chat_server_handle) = ChatServer::new(
&config.settings,
config.ws_id,
ws_config,
db_pool,
cancellation_token,
);
let (chat_server, chat_server_handle) =
ChatServer::new(&config.settings, ws_config, db_pool, cancellation_token);
Ok(ServerPluginArtifacts {
task: chat_server,
handle: Arc::new(chat_server_handle),
Expand Down
9 changes: 1 addition & 8 deletions crates/plugin-chat/src/server_only/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::{
};
use tracing::{error, info, instrument, warn};
use tracked_cancellations::TrackedCancellationToken;
use ws_auth::{WsConnId, WsId};
use ws_auth::WsConnId;
use wykies_server::{db_types::DbPool, ws::HeartbeatConfig, ServerTask, WebSocketSettings};
use wykies_shared::{
const_config::CHANNEL_BUFFER_SIZE, debug_panic, log_err_as_error, log_err_as_warn,
Expand Down Expand Up @@ -63,14 +63,12 @@ pub struct ChatServer {
#[derive(Debug, Clone)]
pub struct ChatServerHandle {
cmd_tx: mpsc::Sender<Command>,
ws_id: WsId,
pub heartbeat_config: HeartbeatConfig,
}

impl ChatServer {
pub fn new(
config: &ChatSettings,
ws_id: WsId,
ws_config: &WebSocketSettings,
db_pool: DbPool,
cancellation_token: TrackedCancellationToken,
Expand Down Expand Up @@ -98,7 +96,6 @@ impl ChatServer {
ChatServerHandle {
cmd_tx,
heartbeat_config,
ws_id,
},
)
}
Expand Down Expand Up @@ -352,10 +349,6 @@ impl ChatServerHandle {
.expect("failed to send command")
}

pub fn ws_id(&self) -> WsId {
self.ws_id
}

/// Broadcast message to other users
#[instrument]
pub async fn send_msg_to_clients(&self, skip: Option<WsConnId>, msg: ChatMsg) {
Expand Down

0 comments on commit fad556f

Please sign in to comment.