pub struct ConnectionRegistry { /* private fields */ }Expand description
Sharded, lock-free-on-the-hot-path connection registry.
One instance is created at launch, leaked to &'static, and shared by every
gateway route. All mutating operations are O(1) amortised and touch only a
single shard; broadcast is O(connections) with no global lock.
Implementations§
Source§impl ConnectionRegistry
impl ConnectionRegistry
pub fn new() -> Self
Sourcepub fn register(
&self,
tx: UnboundedSender<WsMessage>,
claims: Option<Arc<Claims>>,
) -> ConnId
pub fn register( &self, tx: UnboundedSender<WsMessage>, claims: Option<Arc<Claims>>, ) -> ConnId
Register a freshly-upgraded socket. Returns its assigned ConnId.
Sourcepub fn unregister(&self, id: ConnId)
pub fn unregister(&self, id: ConnId)
Remove a socket and evict it from every room it had joined.
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Number of live connections. O(1).
Sourcepub fn join_room(&self, id: ConnId, room: String)
pub fn join_room(&self, id: ConnId, room: String)
Add a connection to a room (Socket.IO-style logical channel).
§TOCTOU safety
Both member_rooms and rooms are updated while holding the DashMap
shard read-lock on conns (the Ref keeps the shard locked until it is
dropped). This prevents a concurrent unregister() — which needs the
shard write-lock — from removing the entry between the two writes,
which would otherwise leave a stale ConnId in rooms forever.
Sourcepub fn leave_room(&self, id: ConnId, room: &str)
pub fn leave_room(&self, id: ConnId, room: &str)
Remove a connection from a room.
Mirrors join_room’s lock order: hold the conns shard read-lock first
so a concurrent unregister() (which needs the write-lock) cannot remove
the entry between the two writes and leave a stale id in rooms.
Sourcepub fn send_text(&self, id: ConnId, text: Arc<str>)
pub fn send_text(&self, id: ConnId, text: Arc<str>)
Enqueue a text frame to one connection. Non-blocking; a dead receiver is silently dropped (the reader loop will unregister it shortly).
Sourcepub fn broadcast_text(&self, text: &str)
pub fn broadcast_text(&self, text: &str)
Fan a text frame out to every live connection. Creates one Arc<str>
allocation then enqueues N pointer copies — no per-connection memcpy.
Sourcepub fn broadcast_room_text(&self, room: &str, text: &str)
pub fn broadcast_room_text(&self, room: &str, text: &str)
Fan a text frame out to the members of one room.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ConnectionRegistry
impl !RefUnwindSafe for ConnectionRegistry
impl Send for ConnectionRegistry
impl Sync for ConnectionRegistry
impl Unpin for ConnectionRegistry
impl UnsafeUnpin for ConnectionRegistry
impl UnwindSafe for ConnectionRegistry
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request