diff --git a/delivery-service/src/main.rs b/delivery-service/src/main.rs index 96b1cf0..9caca16 100644 --- a/delivery-service/src/main.rs +++ b/delivery-service/src/main.rs @@ -143,93 +143,6 @@ async fn create_message( } } -#[tracing::instrument(skip(socket, state))] -async fn handle_socket_old( - mut socket: WebSocket, - State(state): State, - client_id: Arc, -) { - debug!("[{}] connected", client_id); - - let _droppochino = Droppochino(client_id.to_string()); - let span = tracing::span!(Level::INFO, "handling"); - let _enter = span.enter(); - //TODO keep alive - //TODO add client authentication to avoid session hijacking - // Hijackers can deny messages to the client and analyze meta data but not read messages if they don't have the clients credentials - - //TODO decide on buffer size - let (sender, mut receiver) = mpsc::channel(8); - let sender = Arc::from(sender); - - // Register sender for this id - // Immediately drop lock after insert to avoid deadlock - let previous_sender = state - .channels - .lock() - .await - .insert(client_id.clone(), sender.clone()); - - if let Some(previous_sender) = previous_sender { - //TODO think about if this is valid - tracing::warn!("[{}] Replacing previous subscriber", client_id); - // This should close the websocket for the other client that used the same id - //TODO test assumption - drop(previous_sender); - tracing::debug!("[{}] dropped previous sender", client_id); - } - - tracing::debug!("[{}] Websocket established", client_id); - loop { - tokio::select! { - Some(message) = receiver.recv() => { - let message = ws::Message::Binary(message.into()); - tracing::debug!("[{}] Sending message through websocket", client_id); - if let Err(error) = socket.send(message).await { - tracing::error!("[{}] Error sending message through websocket: {}", client_id, error); - //TODO remove channel to avoid memory leak. It is the clients responsibility to reestablish a connection - break; - } - }, - // We only use the socket unidirectional for now - // but we want to know when the client closes the socket - Some(Ok(message)) = socket.recv() => { - tracing::debug!("[{}] Received message through websocket: {:?}", client_id, message); - }, - else => { - tracing::debug!("[{}] Handling else", client_id); - break; - }, - } - } - - tracing::debug!("[{}] disconnected", client_id); - - // Try to close gracefully but if not ignore error - if let Err(error) = socket.close().await { - tracing::trace!( - "[{}] Ignoring error from closing disconnected websocket: {}", - client_id, - error - ); - } - // It is the clients responsibility to reestablish a new connection - - let mut channels = state.channels.lock().await; - - // Remove channel to avoid memory leak - if channels - .get(&client_id) - .is_none_or(|current_sender| !Arc::ptr_eq(current_sender, &sender)) - { - tracing::debug!("[{}] already removed or replaced", client_id); - return; - } - - channels.remove(&client_id); - tracing::debug!("[{}] removed", client_id); -} - /// Handles requests for listening to server sent events (SSE) that are used to send incoming messages from the server to the client #[tracing::instrument(skip(state))]