Skip to main content

RedisRoomRegistry

Struct RedisRoomRegistry 

Source
pub struct RedisRoomRegistry<C = MultiplexedConnection> { /* private fields */ }
Available on crate feature redis only.
Expand description

Multi-node crate::room::RoomRegistry: local RoomManager + Redis pub/sub fan-out. See the module docs for topology and delivery semantics.

The generic parameter is the publish connection (redis’ ConnectionLike); it defaults to the production MultiplexedConnection and exists so tests can substitute a mock — in application code always write the default RedisRoomRegistry.

Implementations§

Source§

impl RedisRoomRegistry<MultiplexedConnection>

Source

pub async fn connect(url: &str) -> Result<Self, WsError>

Connect with the default channel prefix (DEFAULT_CHANNEL_PREFIX).

§Errors

WsError::Redis when the URL is invalid or the server is unreachable.

Source

pub async fn connect_with_prefix( url: &str, prefix: &str, ) -> Result<Self, WsError>

Connect with a custom channel prefix (rooms of unrelated deployments can share one Redis by differing prefixes).

Source§

impl<C> RedisRoomRegistry<C>
where C: ConnectionLike + Clone + Send + 'static,

Source

pub fn from_parts( local: RoomManager, client: Client, publish: C, prefix: impl Into<String>, ) -> Self

Assemble a registry from a local manager, a Redis client (used by Self::run_subscriber), and an existing publish connection.

Public to let integrators and tests supply custom ConnectionLike implementations (the trait-mocked publish path); prefer Self::connect in application code.

Source§

impl<C> RedisRoomRegistry<C>

Source

pub fn local(&self) -> &RoomManager

The local half of the registry — the ordinary RoomManager API for joins, leaves, local subscription, and cleanup.

Source

pub fn prefix(&self) -> &str

Channel prefix in use.

Source

pub fn channel_name(&self, room_id: &str) -> String

Channel name for a room id.

Source

pub fn room_id_from_channel<'a>(&self, channel: &'a str) -> Option<&'a str>

Room id carried by a channel name, if it matches the prefix.

Source

pub fn node_id(&self) -> [u8; 16]

This registry’s random publisher id (self-echo suppression).

Source§

impl<C> RedisRoomRegistry<C>
where C: ConnectionLike + Clone + Send + 'static,

Source

pub async fn broadcast( &self, room_id: &str, frame: impl Into<Frame>, ) -> Result<usize, WsError>

Broadcast a frame to a room across the fleet.

  1. Fans out to the local room’s receivers (creating the room if this node does not host it yet — matching RoomRegistry::get_or_create semantics).
  2. PUBLISHes the envelope to prefix + room_id for every other node.

Returns the number of local receivers reached. An empty local room is not an error here (unlike crate::room::Room::broadcast): other nodes may still hold receivers, so Ok(0) just means “no local subscriber”. A Redis failure surfaces as WsError::Redis.

At-most-once: see the module docs — delivery on other nodes is best-effort.

Source

pub async fn publish(&self, frame: &Frame, room_id: &str) -> Result<(), WsError>

Publish one frame to room_id’s channel (no local delivery).

Source

pub fn apply_remote( &self, room_id: &str, envelope: &[u8], ) -> Result<Option<usize>, WsError>

Apply a raw pub/sub envelope to the local registry: decode it, drop self-echoes, and deliver to the local room only if this node hosts it (remote traffic never materializes rooms).

Returns Ok(Some(n)) with the local receiver count when delivered, Ok(None) when the message was a self-echo or for an unhosted room, and the decode error for malformed envelopes.

Source§

impl RedisRoomRegistry<MultiplexedConnection>

Source

pub async fn run_subscriber(&self) -> Result<(), WsError>

Run the subscriber loop on the current task: PSUBSCRIBE prefix* and apply every message to the local registry until the Redis connection dies.

Spawn it once per node at startup (see the module example); to survive Redis restarts wrap it in your own retry/backoff — the loop intentionally returns instead of hiding reconnect policy.

§Errors

WsError::Redis on subscription/transport failure. Malformed envelopes from the channel are dropped (fail-closed), not fatal.

Trait Implementations§

Source§

impl<C> RoomRegistry for RedisRoomRegistry<C>
where C: ConnectionLike + Clone + Send + 'static,

Source§

fn get_or_create(&self, room_id: &str) -> Arc<Room> ⓘ

Get an existing room or create it.
Source§

fn get(&self, room_id: &str) -> Option<Arc<Room>>

Get a room if it exists.
Source§

fn remove(&self, room_id: &str) -> Option<Arc<Room>>

Remove a room by id, returning it if present.
Source§

fn room_count(&self) -> usize

Number of rooms.
Source§

fn room_ids(&self) -> Vec<String>

List room ids.
Source§

fn total_participants(&self) -> usize

Total participants across all rooms.
Source§

fn cleanup(&self) -> usize

Remove all empty rooms (no participants, no receivers). Returns number removed.
Source§

fn cleanup_empty(&self) -> usize

Remove all rooms with zero participants (regardless of receivers). Returns number removed.

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<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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.