pub struct RelayPool { /* private fields */ }Expand description
The relay pool — manages multiple WebSocket connections with bidirectional messaging.
Inbound events flow through an MPSC ring buffer with deduplication. Outbound messages are broadcast to all relay threads via a lock-free broadcast ring.
Implementations§
Source§impl RelayPool
impl RelayPool
Sourcepub fn new(
ring_capacity: usize,
cache_size: usize,
broadcast_capacity: usize,
max_relays: usize,
) -> Self
pub fn new( ring_capacity: usize, cache_size: usize, broadcast_capacity: usize, max_relays: usize, ) -> Self
Create a new relay pool with bidirectional messaging.
§Arguments
ring_capacity- MPSC ring buffer size for inbound event throughputcache_size- Deduplication cache size (e.g. 10,000)broadcast_capacity- Broadcast ring buffer size for outbound messagesmax_relays- Maximum number of relay connections (broadcast consumer slots)
Sourcepub fn add_relay(&mut self, relay_url: String)
pub fn add_relay(&mut self, relay_url: String)
Add a relay connection to the pool.
Spawns a new thread that connects to the relay, reads inbound events, and sends outbound messages from the broadcast ring.
Automatically cleans up dead connections first to free broadcast slots.
Sourcepub fn remove_relay(&mut self, relay_url: &str) -> bool
pub fn remove_relay(&mut self, relay_url: &str) -> bool
Remove a relay from the pool by URL.
Signals the relay thread to shut down and blocks until it exits (~1-2ms). The broadcast consumer slot is freed immediately.
Returns true if the relay was found and removed.
Sourcepub fn cleanup(&mut self)
pub fn cleanup(&mut self)
Remove dead connections from the pool.
Joins finished threads and frees their broadcast consumer slots.
Called automatically by [add_relay], but can be called explicitly
to update [connection_count].
Sourcepub fn sender(&self) -> PoolSender
pub fn sender(&self) -> PoolSender
Get a cloneable sender handle for broadcasting to all relays.
Multiple threads can hold a PoolSender and send concurrently.
Sourcepub fn recv(&mut self) -> PoolMessage
pub fn recv(&mut self) -> PoolMessage
Receive the next event from any relay (blocking)
Sourcepub fn try_recv(&mut self) -> Option<PoolMessage>
pub fn try_recv(&mut self) -> Option<PoolMessage>
Receive the next event from any relay (non-blocking)
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Get the total number of connections (including dead ones not yet cleaned up).
Sourcepub fn active_connection_count(&self) -> usize
pub fn active_connection_count(&self) -> usize
Get the number of connections whose threads are still running.
Sourcepub fn relay_urls(&self) -> Vec<&str>
pub fn relay_urls(&self) -> Vec<&str>
Get the relay URLs of all connections in the pool.
Sourcepub fn active_relay_urls(&self) -> Vec<&str>
pub fn active_relay_urls(&self) -> Vec<&str>
Get the relay URLs of only active (thread still running) connections.