pub struct RefactoredConnection { /* private fields */ }Expand description
Refactored Connection based on FramedRead + FramedWrite
§Architecture Design
+-----------------------------------------------------------+
| RefactoredConnection |
+-----------------------------------------------------------+
| |
| Read Side (FramedRead): |
| TcpStream -> OwnedReadHalf -> FramedRead<Codec> |
| | | |
| Direct Access Auto Decode |
| |
| Write Side (FramedWrite - Lock-free): |
| TcpStream -> OwnedWriteHalf -> FramedWrite<Codec> |
| | | |
| Zero-copy Write Codec Encode |
| |
+-----------------------------------------------------------+§Core Advantages
- Separated Read/Write: Independent management after
into_split() - Dual Mode Support: Codec encoding + zero-copy direct write
- High Performance: FramedWrite built-in optimization + bypassable
- Flexible Control: Full control over flush timing
- Lock-free Write: Writer doesn’t use Mutex, caller ensures exclusive access
§Concurrency Model
- Reader: No Mutex needed, typically only one receive loop reads
- Writer: Lock-free design, caller ensures exclusive access (via &mut self)
Implementations§
Source§impl RefactoredConnection
impl RefactoredConnection
Sourcepub fn with_capacity(stream: TcpStream, capacity: usize) -> Self
pub fn with_capacity(stream: TcpStream, capacity: usize) -> Self
Create connection with specified buffer capacity
Sourcepub async fn send_command(
&mut self,
command: RemotingCommand,
) -> RocketMQResult<()>
pub async fn send_command( &mut self, command: RemotingCommand, ) -> RocketMQResult<()>
Send RemotingCommand using Codec
RemotingCommand is first encoded to Bytes (containing complete RocketMQ protocol frame), then sent directly via CompositeCodec’s BytesCodec
§Performance Optimization
Reuses internal encode_buffer to avoid allocating new BytesMut on each call
Sourcepub async fn recv_command(&mut self) -> RocketMQResult<Option<RemotingCommand>>
pub async fn recv_command(&mut self) -> RocketMQResult<Option<RemotingCommand>>
Receive and decode message
CompositeCodec automatically decodes to RemotingCommand
Note: This method takes &mut self, ensuring only one receive loop reads
Sourcepub async fn send_bytes(&mut self, bytes: Bytes) -> RocketMQResult<()>
pub async fn send_bytes(&mut self, bytes: Bytes) -> RocketMQResult<()>
Send raw Bytes directly (bypass Codec)
§Note
The input bytes must already contain a complete RocketMQ protocol frame (i.e., data already encoded via fast_header_encode)
To send RemotingCommand, use send_command method instead
Sourcepub async fn send_commands_batch(
&mut self,
commands: Vec<RemotingCommand>,
) -> RocketMQResult<()>
pub async fn send_commands_batch( &mut self, commands: Vec<RemotingCommand>, ) -> RocketMQResult<()>
Batch send RemotingCommand (using feed + flush)
This is the recommended batch sending method, reducing system call count
§Performance Optimization
Reuses internal encode_buffer to avoid allocating new BytesMut each time. split() empties buffer but preserves capacity, achieving zero-allocation reuse.
Sourcepub async fn send_bytes_batch(
&mut self,
chunks: Vec<Bytes>,
) -> RocketMQResult<()>
pub async fn send_bytes_batch( &mut self, chunks: Vec<Bytes>, ) -> RocketMQResult<()>
Sourcepub async fn send_bytes_zero_copy(
&mut self,
chunks: Vec<Bytes>,
) -> RocketMQResult<()>
pub async fn send_bytes_zero_copy( &mut self, chunks: Vec<Bytes>, ) -> RocketMQResult<()>
Sourcepub async fn send_bytes_zero_copy_single(
&mut self,
data: Bytes,
) -> RocketMQResult<()>
pub async fn send_bytes_zero_copy_single( &mut self, data: Bytes, ) -> RocketMQResult<()>
Zero-copy send single chunk
Suitable for sending single large block of data
Sourcepub async fn send_response_hybrid(
&mut self,
response_header: RemotingCommand,
message_bodies: Vec<Bytes>,
) -> RocketMQResult<()>
pub async fn send_response_hybrid( &mut self, response_header: RemotingCommand, message_bodies: Vec<Bytes>, ) -> RocketMQResult<()>
Hybrid mode: Response header (Codec) + Message body (Zero-copy)
This is the recommended implementation for Pull message responses
§Flow
- Send response header (encoded as complete RocketMQ frame via BytesCodec)
- Flush to ensure response header is sent
- Zero-copy send message bodies (bypass Codec, direct write_all)
- Final flush
§Performance Optimization
Reuses internal encode_buffer, split() empties buffer but preserves capacity
Sourcepub async fn send_response_hybrid_vectored(
&mut self,
response_header_bytes: Bytes,
message_bodies: Vec<Bytes>,
) -> RocketMQResult<()>
pub async fn send_response_hybrid_vectored( &mut self, response_header_bytes: Bytes, message_bodies: Vec<Bytes>, ) -> RocketMQResult<()>
Hybrid mode optimized: using write_vectored
Send response header and all message bodies in one shot (scatter-gather I/O)
§Parameters
response_header_bytes: Pre-encoded response header (already encoded via CompositeCodec)message_bodies: Message body list
§Performance
This is the highest performance sending method, requiring only one system call
Sourcepub fn state(&self) -> ConnectionState
pub fn state(&self) -> ConnectionState
Get current connection state
Sourcepub fn mark_degraded(&self)
pub fn mark_degraded(&self)
Mark connection as degraded
Used to indicate connection quality degradation but still usable
Sourcepub fn mark_healthy(&self)
pub fn mark_healthy(&self)
Mark connection as healthy
Sourcepub async fn close(&mut self) -> RocketMQResult<()>
pub async fn close(&mut self) -> RocketMQResult<()>
Close connection
§Flow
- Mark state as Closed
- Flush all pending data
- Shutdown underlying TCP connection
Sourcepub fn subscribe_state(&self) -> Receiver<ConnectionState>
pub fn subscribe_state(&self) -> Receiver<ConnectionState>
Subscribe to connection state changes
Returns a watch::Receiver that can be used to monitor state changes
Sourcepub fn connection_id(&self) -> &CheetahString
pub fn connection_id(&self) -> &CheetahString
Get connection ID
Sourcepub fn framed_reader_mut(
&mut self,
) -> &mut FramedRead<OwnedReadHalf, CompositeCodec>
pub fn framed_reader_mut( &mut self, ) -> &mut FramedRead<OwnedReadHalf, CompositeCodec>
Get mutable reference to FramedRead
Used for advanced read operations
Note: Requires &mut self to ensure exclusive access
Sourcepub fn framed_writer_mut(
&mut self,
) -> &mut FramedWrite<OwnedWriteHalf, CompositeCodec>
pub fn framed_writer_mut( &mut self, ) -> &mut FramedWrite<OwnedWriteHalf, CompositeCodec>
Get mutable reference to FramedWrite
Used for advanced write operations
Note: Requires &mut self to ensure exclusive access
Auto Trait Implementations§
impl !Freeze for RefactoredConnection
impl !RefUnwindSafe for RefactoredConnection
impl Send for RefactoredConnection
impl Sync for RefactoredConnection
impl Unpin for RefactoredConnection
impl !UnwindSafe for RefactoredConnection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.