Skip to main content

ConnectionManager

Struct ConnectionManager 

Source
pub struct ConnectionManager { /* private fields */ }
Expand description

连接管理器

管理所有活跃连接,支持按 ID 查询、按用户 ID 查询等功能

Implementations§

Source§

impl ConnectionManager

Source

pub fn new() -> Self

创建新的连接管理器

Source

pub fn with_send_timeout(send_timeout: Duration) -> Self

使用指定写超时创建连接管理器

Source

pub fn with_limits(send_timeout: Duration, fanout_concurrency: usize) -> Self

使用指定写超时和 fanout 并发度创建连接管理器

Source

pub fn with_write_queue_limits( send_timeout: Duration, fanout_concurrency: usize, write_queue_capacity: usize, ) -> Self

使用指定写超时、fanout 并发度和每连接写队列容量创建连接管理器。

Source

pub fn add_connection( &self, connection_id: String, connection: Box<dyn Connection>, user_id: Option<String>, requires_auth: bool, ) -> Result<()>

添加连接

§参数
  • connection_id: 连接唯一标识符
  • connection: 连接实例
  • user_id: 可选的用户 ID(如果已认证)
  • requires_auth: 是否需要认证(如果为 false,连接直接标记为已验证)
§返回

如果连接 ID 已存在,返回错误

Source

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_countadd_connection 在高并发握手完成时产生超额注册。

Source

pub fn remove_connection(&self, connection_id: &str) -> Result<()>

移除连接

§参数
  • connection_id: 要移除的连接 ID
§返回

如果连接不存在,返回错误

Source

pub fn get_connection( &self, connection_id: &str, ) -> Option<(Arc<Mutex<Box<dyn Connection>>>, ConnectionInfo)>

获取连接

§参数
  • connection_id: 连接 ID
§返回

连接实例和连接信息的元组,如果不存在则返回 None

Source§

impl ConnectionManager

Source

pub fn get_user_connections(&self, user_id: &str) -> Vec<String>

获取用户的所有连接

§参数
  • user_id: 用户 ID
§返回

该用户的所有连接 ID 列表

Source

pub fn bind_user(&self, connection_id: &str, user_id: String) -> Result<()>

更新连接的用户 ID(用于认证后绑定用户)

§参数
  • connection_id: 连接 ID
  • user_id: 新的用户 ID
Source

pub fn update_connection_active(&self, connection_id: &str) -> Result<()>

更新连接的最后活跃时间

Source

pub fn set_connection_authenticated( &self, connection_id: &str, user_id: Option<String>, ) -> Result<()>

设置连接为已验证状态

Source

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<()>

更新连接的协商信息(设备信息、序列化格式、压缩算法)

Source

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

Source

pub fn mark_negotiation_confirmed(&self, connection_id: &str) -> Result<()>

标记协商已确认(客户端收到 CONNECT_ACK 后发送确认)

Source

pub fn list_connections(&self) -> Vec<String>

获取所有连接 ID

Source

pub fn connection_count(&self) -> usize

获取连接总数

Source

pub fn user_count(&self) -> usize

获取当前绑定了连接的用户数

Source

pub fn cleanup_timeout_connections(&self, timeout: Duration) -> Vec<String>

清理超时连接

§参数
  • timeout: 超时时间
§返回

被清理的连接 ID 列表

Source

pub fn stats(&self) -> TraitConnectionStats

获取连接统计信息

Trait Implementations§

Source§

impl ConnectionManagerTrait for ConnectionManager

Source§

fn as_any(&self) -> &dyn Any

获取 Any 引用,用于类型向下转换
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,

添加连接
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,

移除连接
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,

获取连接
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,

获取用户的所有连接 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,

绑定用户到连接
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,

更新连接的最后活跃时间
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,

设置连接为已验证状态(认证通过后调用)
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,

获取所有连接 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,

获取连接总数
Source§

fn connection_count_snapshot(&self) -> usize

Returns a synchronous snapshot of the total connection count. Read more
Source§

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,

清理超时连接
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,

向指定连接发送数据(字节数组)
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,

向指定用户的所有连接发送数据(字节数组)
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,

广播消息到所有连接(字节数组)
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,

广播消息到所有连接,排除指定连接(字节数组)
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,

向指定连接发送 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,

同一 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,

向指定用户的所有连接发送 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,

广播 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,

广播 Frame 到所有连接,排除指定连接(自动序列化) Read more
Source§

impl Default for ConnectionManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more