pub struct ConnectionManager { /* private fields */ }Expand description
连接管理器
管理所有活跃连接,支持按 ID 查询、按用户 ID 查询等功能
Implementations§
Source§impl ConnectionManager
impl ConnectionManager
Sourcepub fn with_send_timeout(send_timeout: Duration) -> Self
pub fn with_send_timeout(send_timeout: Duration) -> Self
使用指定写超时创建连接管理器
Sourcepub fn with_limits(send_timeout: Duration, fanout_concurrency: usize) -> Self
pub fn with_limits(send_timeout: Duration, fanout_concurrency: usize) -> Self
使用指定写超时和 fanout 并发度创建连接管理器
Sourcepub fn with_write_queue_limits(
send_timeout: Duration,
fanout_concurrency: usize,
write_queue_capacity: usize,
) -> Self
pub fn with_write_queue_limits( send_timeout: Duration, fanout_concurrency: usize, write_queue_capacity: usize, ) -> Self
使用指定写超时、fanout 并发度和每连接写队列容量创建连接管理器。
Sourcepub fn add_connection(
&self,
connection_id: String,
connection: Box<dyn Connection>,
user_id: Option<String>,
requires_auth: bool,
) -> Result<()>
pub fn add_connection( &self, connection_id: String, connection: Box<dyn Connection>, user_id: Option<String>, requires_auth: bool, ) -> Result<()>
Sourcepub fn add_connection_with_limit(
&self,
connection_id: String,
connection: Box<dyn Connection>,
user_id: Option<String>,
requires_auth: bool,
max_connections: usize,
) -> Result<()>
pub fn add_connection_with_limit( &self, connection_id: String, connection: Box<dyn Connection>, user_id: Option<String>, requires_auth: bool, max_connections: usize, ) -> Result<()>
添加连接,并在同一个写锁临界区内检查容量。
这个入口用于传输层新连接注册,避免先 connection_count 再 add_connection
在高并发握手完成时产生超额注册。
Sourcepub fn remove_connection(&self, connection_id: &str) -> Result<()>
pub fn remove_connection(&self, connection_id: &str) -> Result<()>
Sourcepub fn get_connection(
&self,
connection_id: &str,
) -> Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>
pub fn get_connection( &self, connection_id: &str, ) -> Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>
Source§impl ConnectionManager
impl ConnectionManager
Sourcepub fn get_user_connections(&self, user_id: &str) -> Vec<String>
pub fn get_user_connections(&self, user_id: &str) -> Vec<String>
Sourcepub fn update_connection_active(&self, connection_id: &str) -> Result<()>
pub fn update_connection_active(&self, connection_id: &str) -> Result<()>
更新连接的最后活跃时间
Sourcepub fn set_connection_authenticated(
&self,
connection_id: &str,
user_id: Option<String>,
) -> Result<()>
pub fn set_connection_authenticated( &self, connection_id: &str, user_id: Option<String>, ) -> Result<()>
设置连接为已验证状态
Sourcepub fn update_connection_negotiation(
&self,
connection_id: &str,
device_info: Option<DeviceInfo>,
serialization_format: SerializationFormat,
compression: CompressionAlgorithm,
encryption: EncryptionAlgorithm,
user_id: Option<String>,
metadata: Option<HashMap<String, String>>,
) -> Result<()>
pub fn update_connection_negotiation( &self, connection_id: &str, device_info: Option<DeviceInfo>, serialization_format: SerializationFormat, compression: CompressionAlgorithm, encryption: EncryptionAlgorithm, user_id: Option<String>, metadata: Option<HashMap<String, String>>, ) -> Result<()>
更新连接的协商信息(设备信息、序列化格式、压缩算法)
Sourcepub fn update_connection_negotiation_with_pipeline(
&self,
connection_id: &str,
device_info: Option<DeviceInfo>,
serialization_format: SerializationFormat,
compression: CompressionAlgorithm,
encryption: EncryptionAlgorithm,
user_id: Option<String>,
parser: MessageParser,
pipeline: Option<Arc<MessagePipeline>>,
) -> Result<()>
pub fn update_connection_negotiation_with_pipeline( &self, connection_id: &str, device_info: Option<DeviceInfo>, serialization_format: SerializationFormat, compression: CompressionAlgorithm, encryption: EncryptionAlgorithm, user_id: Option<String>, parser: MessageParser, pipeline: Option<Arc<MessagePipeline>>, ) -> Result<()>
更新连接的协商信息(设备信息、序列化格式、压缩算法、加密方式)并设置 pipeline
Sourcepub fn mark_negotiation_confirmed(&self, connection_id: &str) -> Result<()>
pub fn mark_negotiation_confirmed(&self, connection_id: &str) -> Result<()>
标记协商已确认(客户端收到 CONNECT_ACK 后发送确认)
Sourcepub fn list_connections(&self) -> Vec<String>
pub fn list_connections(&self) -> Vec<String>
获取所有连接 ID
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
获取连接总数
Sourcepub fn user_count(&self) -> usize
pub fn user_count(&self) -> usize
获取当前绑定了连接的用户数
Sourcepub fn cleanup_timeout_connections(&self, timeout: Duration) -> Vec<String>
pub fn cleanup_timeout_connections(&self, timeout: Duration) -> Vec<String>
Sourcepub fn stats(&self) -> TraitConnectionStats
pub fn stats(&self) -> TraitConnectionStats
获取连接统计信息
Trait Implementations§
Source§impl ConnectionManagerTrait for ConnectionManager
impl ConnectionManagerTrait for ConnectionManager
Source§fn add_connection<'life0, 'async_trait>(
&'life0 self,
connection_id: String,
connection: Arc<Mutex<Box<dyn Connection>>>,
user_id: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn add_connection<'life0, 'async_trait>(
&'life0 self,
connection_id: String,
connection: Arc<Mutex<Box<dyn Connection>>>,
user_id: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
添加连接
Source§fn remove_connection<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn remove_connection<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
移除连接
Source§fn get_connection<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_connection<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
获取连接
Source§fn get_user_connections<'life0, 'life1, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_user_connections<'life0, 'life1, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
获取用户的所有连接 ID
Source§fn bind_user<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
user_id: String,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn bind_user<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
user_id: String,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
绑定用户到连接
Source§fn update_connection_active<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn update_connection_active<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
更新连接的最后活跃时间
Source§fn set_connection_authenticated<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
user_id: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn set_connection_authenticated<'life0, 'life1, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
user_id: Option<String>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
设置连接为已验证状态(认证通过后调用)
Source§fn list_connections<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list_connections<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
获取所有连接 ID
Source§fn connection_count<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = usize> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn connection_count<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = usize> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
获取连接总数
Source§fn connection_count_snapshot(&self) -> usize
fn connection_count_snapshot(&self) -> usize
Returns a synchronous snapshot of the total connection count. Read more
Source§fn user_count_snapshot(&self) -> usize
fn user_count_snapshot(&self) -> usize
Returns a synchronous user-count snapshot with the same constraints as
Self::connection_count_snapshot.Source§fn cleanup_timeout_connections<'life0, 'async_trait>(
&'life0 self,
timeout: Duration,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn cleanup_timeout_connections<'life0, 'async_trait>(
&'life0 self,
timeout: Duration,
) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
清理超时连接
Source§fn send_to_connection<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn send_to_connection<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
向指定连接发送数据(字节数组)
Source§fn send_to_user<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn send_to_user<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
data: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
向指定用户的所有连接发送数据(字节数组)
Source§fn broadcast<'life0, 'life1, 'async_trait>(
&'life0 self,
data: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn broadcast<'life0, 'life1, 'async_trait>(
&'life0 self,
data: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
广播消息到所有连接(字节数组)
Source§fn broadcast_except<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
data: &'life1 [u8],
exclude_connection_id: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn broadcast_except<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
data: &'life1 [u8],
exclude_connection_id: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
广播消息到所有连接,排除指定连接(字节数组)
Source§fn send_frame_to<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
frame: &'life2 Frame,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn send_frame_to<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
connection_id: &'life1 str,
frame: &'life2 Frame,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
向指定连接发送 Frame(自动序列化) Read more
Source§fn send_frame_to_connections<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
connection_ids: &'life1 [String],
frame: &'life2 Frame,
) -> Pin<Box<dyn Future<Output = (i32, i32)> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn send_frame_to_connections<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
connection_ids: &'life1 [String],
frame: &'life2 Frame,
) -> Pin<Box<dyn Future<Output = (i32, i32)> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
同一 Frame 发送到多个连接(群扇出主路径)。 Read more
Source§fn send_frame_to_user<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
frame: &'life2 Frame,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn send_frame_to_user<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
user_id: &'life1 str,
frame: &'life2 Frame,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
向指定用户的所有连接发送 Frame(自动序列化) Read more
Source§fn broadcast_frame<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
frame: &'life1 Frame,
parser: Option<&'life2 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn broadcast_frame<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
frame: &'life1 Frame,
parser: Option<&'life2 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
广播 Frame 到所有连接(自动序列化) Read more
Source§fn broadcast_frame_except<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
frame: &'life1 Frame,
exclude_connection_id: &'life2 str,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn broadcast_frame_except<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
frame: &'life1 Frame,
exclude_connection_id: &'life2 str,
parser: Option<&'life3 MessageParser>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
广播 Frame 到所有连接,排除指定连接(自动序列化) Read more
Auto Trait Implementations§
impl Freeze for ConnectionManager
impl RefUnwindSafe for ConnectionManager
impl Send for ConnectionManager
impl Sync for ConnectionManager
impl Unpin for ConnectionManager
impl UnsafeUnpin for ConnectionManager
impl UnwindSafe for ConnectionManager
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more