hubcio commented on code in PR #3192: URL: https://github.com/apache/iggy/pull/3192#discussion_r3179974081
########## core/message_bus/src/transports/ws.rs: ########## @@ -0,0 +1,427 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Plaintext WS transport: `compio_ws::WebSocketStream<TcpStream>` +//! driven by a single connection task per peer. +//! +//! # Architecture +//! +//! Replaces the prior post-handshake unwrap-to-bare-TcpStream + manual +//! RFC 6455 frame parser with the compio-native stack: +//! +//! 1. The HTTP-Upgrade handshake runs externally +//! (`installer::install_client_ws_fd` calls +//! `compio_ws::accept_async`); the resulting +//! `WebSocketStream<TcpStream>` is handed to [`WsTransportConn::new_server`]. +//! 2. A single connection task owns the `WebSocketStream` and +//! `select!`s over the bus shutdown token, the outbound mailbox, +//! and `ws.read()`. +//! 3. On shutdown the task sends a WS Close frame, flushes, and drops. +//! +//! # Cancel safety +//! +//! `compio_ws::WebSocketStream::read` (lib.rs:208-229) loops calling +//! `inner.read()` against a buffered `SyncStream`. The await points are +//! at `fill_read_buf` on the underlying stream; bytes already in the +//! buffer survive a future drop. Tungstenite's frame parser state lives +//! inside the WebSocket struct, also preserved across drops. +//! +//! Sends are run to completion outside any `select!`. The token signals +//! the *start* of close, never cancels an in-flight send. +//! +//! # Pings +//! +//! Inbound `Message::Ping` is queued by tungstenite as an auto-Pong +//! reply; the `WebSocketStream::read` flush before delivery drains the +//! queue, so the application code only needs to ignore Pings. No +//! explicit Pong send required. + +use super::{ActorContext, TransportConn}; +use crate::framing; +use crate::lifecycle::BusMessage; +use compio::net::TcpStream; +use compio::ws::WebSocketStream; +use compio::ws::tungstenite::{self, Message as WsMessage}; +use futures::FutureExt; +use iggy_binary_protocol::consensus::MESSAGE_ALIGN; +use iggy_binary_protocol::{GenericHeader, Message}; +use std::time::Duration; +use tracing::{debug, warn}; + +/// Default wall-clock bound on the WS Close + drop sequence. +const DEFAULT_CLOSE_GRACE: Duration = Duration::from_secs(2); + +/// Errors decoded from a WS `Message::Binary` payload. +#[derive(Debug)] +pub(in crate::transports) enum FrameDecodeError { + BadHeader, + BadSize, +} + +/// Decode one consensus `Message<GenericHeader>` from a raw WS Binary +/// payload. Reused by [`super::wss`] over the TLS-protected channel. +pub(in crate::transports) fn decode_consensus_frame( + body: &[u8], +) -> Result<Message<GenericHeader>, FrameDecodeError> { + if body.len() < iggy_binary_protocol::HEADER_SIZE { + return Err(FrameDecodeError::BadHeader); + } + let total_size = u32::from_le_bytes( Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
