use crate::error::{Error, Result};
use crate::session::Session;
use crate::websocket::message::{CloseCode, Message};
use crate::websocket::socket::{Received, WebSocket, WebSocketSender, CLOSE_HANDSHAKE_TIMEOUT};
use dashmap::mapref::entry::Entry;
use dashmap::DashMap;
use futures::future::BoxFuture;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::{mpsc, watch};
pub type ClientId = u64;
const CLIENT_QUEUE_CAPACITY: usize = 64;
#[derive(Debug)]
pub(crate) enum Outbound {
Send(Message),
}
#[derive(Debug)]
enum WriterOutcome {
ClosedByUs(CloseCode),
QueueClosed,
Failed(Error),
}
type OpenHandler = Arc<dyn Fn(SocketClient) -> BoxFuture<'static, Result<()>> + Send + Sync>;
type MessageHandler =
Arc<dyn Fn(SocketClient, Message) -> BoxFuture<'static, Result<()>> + Send + Sync>;
type CloseHandler =
Arc<dyn Fn(SocketClient, CloseCode) -> BoxFuture<'static, Result<()>> + Send + Sync>;
type ErrorHandler = Arc<dyn Fn(SocketClient, Error) -> BoxFuture<'static, Result<()>> + Send + Sync>;
#[derive(Default, Clone)]
struct Handlers {
open: Option<OpenHandler>,
message: Option<MessageHandler>,
close: Option<CloseHandler>,
error: Option<ErrorHandler>,
}
pub struct SocketRoom {
path: String,
clients: DashMap<ClientId, SocketClient>,
handlers: Mutex<Handlers>,
autodestroy: AtomicBool,
next_id: AtomicU64,
state: std::sync::atomic::AtomicU8,
ready_notify: tokio::sync::Notify,
}
const CONFIGURING: u8 = 0;
const CONFIGURED: u8 = 1;
const CONFIGURE_FAILED: u8 = 2;
impl std::fmt::Debug for SocketRoom {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SocketRoom")
.field("path", &self.path)
.field("online", &self.online())
.field("autodestroy", &self.autodestroy.load(Ordering::Relaxed))
.finish()
}
}
impl SocketRoom {
fn new(path: String) -> Self {
Self {
path,
clients: DashMap::new(),
handlers: Mutex::new(Handlers::default()),
autodestroy: AtomicBool::new(true),
next_id: AtomicU64::new(1),
state: std::sync::atomic::AtomicU8::new(CONFIGURING),
ready_notify: tokio::sync::Notify::new(),
}
}
fn finish_configuring(&self, state: u8) {
self.state.store(state, Ordering::Release);
self.ready_notify.notify_waiters();
}
async fn wait_configured(&self) -> bool {
loop {
match self.state.load(Ordering::Acquire) {
CONFIGURED => return true,
CONFIGURE_FAILED => return false,
_ => {}
}
let notified = self.ready_notify.notified();
match self.state.load(Ordering::Acquire) {
CONFIGURED => return true,
CONFIGURE_FAILED => return false,
_ => notified.await,
}
}
}
pub fn path(&self) -> &str {
&self.path
}
pub fn online(&self) -> usize {
self.clients.len()
}
pub fn is_empty(&self) -> bool {
self.clients.is_empty()
}
pub fn client_ids(&self) -> Vec<ClientId> {
self.clients.iter().map(|entry| *entry.key()).collect()
}
pub fn find(&self, id: ClientId) -> Option<SocketClient> {
self.clients.get(&id).map(|entry| entry.clone())
}
pub fn autodestroy(&self, enabled: bool) -> &Self {
self.autodestroy.store(enabled, Ordering::Relaxed);
self
}
pub fn on_open<F, Fut>(&self, handler: F) -> &Self
where
F: Fn(SocketClient) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<()>> + Send + 'static,
{
self.lock_handlers().open = Some(Arc::new(move |client| Box::pin(handler(client))));
self
}
pub fn on_message<F, Fut>(&self, handler: F) -> &Self
where
F: Fn(SocketClient, Message) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<()>> + Send + 'static,
{
self.lock_handlers().message = Some(Arc::new(move |client, message| {
Box::pin(handler(client, message))
}));
self
}
pub fn on_close<F, Fut>(&self, handler: F) -> &Self
where
F: Fn(SocketClient, CloseCode) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<()>> + Send + 'static,
{
self.lock_handlers().close =
Some(Arc::new(move |client, code| Box::pin(handler(client, code))));
self
}
pub fn on_error<F, Fut>(&self, handler: F) -> &Self
where
F: Fn(SocketClient, Error) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<()>> + Send + 'static,
{
self.lock_handlers().error =
Some(Arc::new(move |client, error| Box::pin(handler(client, error))));
self
}
pub fn send(&self, message: Message) -> Result<()> {
self.fan_out(message, None)
}
pub fn send_except(&self, message: Message, except: ClientId) -> Result<()> {
self.fan_out(message, Some(except))
}
pub fn send_to(&self, id: ClientId, message: Message) -> Result<()> {
match self.clients.get(&id) {
Some(client) => client.send(message),
None => Ok(()),
}
}
pub fn send_json<T: serde::Serialize + ?Sized>(&self, value: &T) -> Result<()> {
self.send(Message::json(value)?)
}
pub fn send_json_except<T: serde::Serialize + ?Sized>(
&self,
value: &T,
except: ClientId,
) -> Result<()> {
self.send_except(Message::json(value)?, except)
}
pub fn destroy(self: &Arc<Self>) {
let clients: Vec<SocketClient> =
self.clients.iter().map(|entry| entry.value().clone()).collect();
for client in clients {
client.close(CloseCode::GOING_AWAY, "room closed");
}
registry().remove_if(&self.path, |_, current| Arc::ptr_eq(current, self));
}
fn fan_out(&self, message: Message, except: Option<ClientId>) -> Result<()> {
for entry in self.clients.iter() {
if Some(*entry.key()) == except {
continue;
}
let _ = entry.value().send(message.clone());
}
Ok(())
}
fn handlers(&self) -> Handlers {
self.lock_handlers().clone()
}
fn lock_handlers(&self) -> std::sync::MutexGuard<'_, Handlers> {
self.handlers
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn next_client_id(&self) -> ClientId {
self.next_id.fetch_add(1, Ordering::Relaxed)
}
}
#[derive(Clone)]
pub struct SocketClient {
id: ClientId,
room: Arc<SocketRoom>,
handshake: Arc<Handshake>,
outbound: mpsc::Sender<Outbound>,
closing: Arc<watch::Sender<Option<(CloseCode, String)>>>,
}
pub(crate) struct Handshake {
pub(crate) params: HashMap<String, String>,
pub(crate) query: HashMap<String, String>,
pub(crate) headers: HashMap<String, String>,
pub(crate) ip: String,
pub(crate) session: Option<Arc<Session>>,
}
impl std::fmt::Debug for SocketClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SocketClient")
.field("id", &self.id)
.field("room", &self.room.path)
.field("ip", &self.handshake.ip)
.finish()
}
}
impl SocketClient {
pub fn id(&self) -> ClientId {
self.id
}
pub fn room(&self) -> &Arc<SocketRoom> {
&self.room
}
pub fn params(&self) -> &HashMap<String, String> {
&self.handshake.params
}
pub fn param(&self, name: &str) -> Option<&str> {
self.handshake.params.get(name).map(String::as_str)
}
pub fn query(&self) -> &HashMap<String, String> {
&self.handshake.query
}
pub fn query_param(&self, name: &str) -> Option<&str> {
self.handshake.query.get(name).map(String::as_str)
}
pub fn headers(&self) -> &HashMap<String, String> {
&self.handshake.headers
}
pub fn header(&self, name: &str) -> Option<&str> {
self.handshake
.headers
.get(&name.to_ascii_lowercase())
.map(String::as_str)
}
pub fn ip(&self) -> &str {
&self.handshake.ip
}
pub fn session(&self) -> Option<&Session> {
self.handshake.session.as_deref()
}
pub fn send(&self, message: Message) -> Result<()> {
match self.outbound.try_send(Outbound::Send(message)) {
Ok(()) => Ok(()),
Err(mpsc::error::TrySendError::Full(_)) => {
log::warn!(
"WebSocket client {} on {} is not reading; disconnecting",
self.id,
self.room.path
);
self.close(CloseCode::POLICY_VIOLATION, "client is not reading");
Err(Error::WebSocket(format!(
"client {} is not reading its socket",
self.id
)))
}
Err(mpsc::error::TrySendError::Closed(_)) => Err(Error::WebSocket(format!(
"client {} has disconnected",
self.id
))),
}
}
pub fn send_text<S: Into<String>>(&self, text: S) -> Result<()> {
self.send(Message::text(text))
}
pub fn send_json<T: serde::Serialize + ?Sized>(&self, value: &T) -> Result<()> {
self.send(Message::json(value)?)
}
pub fn close(&self, code: CloseCode, reason: &str) {
let _ = self.closing.send(Some((code, reason.to_string())));
}
}
fn registry() -> &'static DashMap<String, Arc<SocketRoom>> {
static ROOMS: Lazy<DashMap<String, Arc<SocketRoom>>> = Lazy::new(DashMap::new);
&ROOMS
}
pub struct SOCKET;
impl SOCKET {
pub fn room(path: &str) -> Option<Arc<SocketRoom>> {
registry().get(path).map(|entry| entry.value().clone())
}
pub fn rooms() -> Vec<Arc<SocketRoom>> {
registry()
.iter()
.map(|entry| entry.value().clone())
.collect()
}
pub fn paths() -> Vec<String> {
registry().iter().map(|entry| entry.key().clone()).collect()
}
pub fn online() -> usize {
registry().iter().map(|entry| entry.value().online()).sum()
}
pub fn shutdown_all() {
for room in Self::rooms() {
let clients: Vec<SocketClient> = room
.clients
.iter()
.map(|entry| entry.value().clone())
.collect();
for client in clients {
client.close(CloseCode::GOING_AWAY, "server shutting down");
}
}
}
}
async fn join(
path: &str,
configure: Box<dyn FnOnce(&SocketRoom) + Send>,
outbound: mpsc::Sender<Outbound>,
closing: Arc<watch::Sender<Option<(CloseCode, String)>>>,
handshake: Handshake,
) -> Result<(Arc<SocketRoom>, SocketClient)> {
let mut configure = Some(configure);
let handshake = Arc::new(handshake);
loop {
let (room, ours) = match registry().entry(path.to_string()) {
Entry::Occupied(occupied) => (occupied.get().clone(), false),
Entry::Vacant(_) if configure.is_none() => {
return Err(Error::WebSocket(format!(
"the room for `{path}` was replaced while it was being configured, and \
this connection's configure closure has already been used; reconnect"
)));
}
Entry::Vacant(vacant) => {
let room = Arc::new(SocketRoom::new(path.to_string()));
(vacant.insert(room).clone(), true)
}
};
if ours {
let finish = FinishConfiguring(&room);
if let Some(configure) = configure.take() {
configure(&room);
}
drop(finish);
} else if !room.wait_configured().await {
continue;
}
let guard = registry().entry(path.to_string());
match guard {
Entry::Occupied(occupied) if Arc::ptr_eq(occupied.get(), &room) => {
let client = SocketClient {
id: room.next_client_id(),
room: Arc::clone(&room),
handshake,
outbound,
closing,
};
room.clients.insert(client.id, client.clone());
drop(occupied);
return Ok((room, client));
}
Entry::Occupied(_) => continue,
Entry::Vacant(_) if configure.is_some() => continue,
Entry::Vacant(_) => {
return Err(Error::WebSocket(format!(
"the room for `{path}` was destroyed while it was being configured, and \
this connection's configure closure has already been used; reconnect"
)));
}
}
}
}
struct FinishConfiguring<'a>(&'a Arc<SocketRoom>);
impl Drop for FinishConfiguring<'_> {
fn drop(&mut self) {
if std::thread::panicking() {
log::error!(
"the configure closure for WebSocket room `{}` panicked; the room is \
withdrawn and the next connection will configure it again",
self.0.path
);
registry().remove_if(&self.0.path, |_, current| Arc::ptr_eq(current, self.0));
self.0.finish_configuring(CONFIGURE_FAILED);
} else {
self.0.finish_configuring(CONFIGURED);
}
}
}
struct Membership {
room: Arc<SocketRoom>,
id: ClientId,
}
impl Drop for Membership {
fn drop(&mut self) {
leave(&self.room, self.id);
}
}
struct AbortOnDrop(tokio::task::JoinHandle<WriterOutcome>);
impl Drop for AbortOnDrop {
fn drop(&mut self) {
self.0.abort();
}
}
fn leave(room: &Arc<SocketRoom>, id: ClientId) {
let entry = registry().entry(room.path.clone());
room.clients.remove(&id);
if let Entry::Occupied(occupied) = entry {
if Arc::ptr_eq(occupied.get(), room)
&& room.autodestroy.load(Ordering::Relaxed)
&& room.clients.is_empty()
{
occupied.remove();
}
}
}
async fn fire_open(client: &SocketClient) {
if let Some(handler) = client.room.handlers().open {
if let Err(error) = handler(client.clone()).await {
report(client, error).await;
}
}
}
async fn fire_message(client: &SocketClient, message: Message) {
if let Some(handler) = client.room.handlers().message {
if let Err(error) = handler(client.clone(), message).await {
report(client, error).await;
}
}
}
async fn fire_close(client: &SocketClient, code: CloseCode) {
if let Some(handler) = client.room.handlers().close {
if let Err(error) = handler(client.clone(), code).await {
log::error!(
"WebSocket on_close for {} ended with an error: {error}",
client.room.path
);
}
}
}
async fn report(client: &SocketClient, error: Error) {
match client.room.handlers().error {
Some(handler) => {
let call = handler(client.clone(), error);
match tokio::time::timeout(CLOSE_HANDSHAKE_TIMEOUT, call).await {
Ok(Ok(())) => {}
Ok(Err(nested)) => {
log::error!(
"WebSocket on_error for {} itself failed: {nested}",
client.room.path
);
}
Err(_) => {
log::error!(
"WebSocket on_error for {} did not finish within {}s; abandoning it",
client.room.path,
CLOSE_HANDSHAKE_TIMEOUT.as_secs()
);
}
}
}
None => {
log::error!(
"WebSocket error on {} (client {}): {error}",
client.room.path,
client.id
);
}
}
}
const WRITE_TIMEOUT: Duration = Duration::from_secs(30);
async fn write_loop(
mut sender: WebSocketSender,
mut queue: mpsc::Receiver<Outbound>,
mut closing: watch::Receiver<Option<(CloseCode, String)>>,
ping_interval: Option<Duration>,
reader_busy: crate::websocket::socket::ReaderBusy,
) -> WriterOutcome {
let mut ping = ping_interval
.filter(|period| !period.is_zero())
.map(|period| tokio::time::interval_at(tokio::time::Instant::now() + period, period));
loop {
let item = tokio::select! {
biased;
changed = closing.changed() => {
if changed.is_err() {
return WriterOutcome::QueueClosed;
}
let requested = closing.borrow_and_update().clone();
if let Some((code, reason)) = requested {
return match timed(sender.close(code, &reason)).await {
Ok(Ok(())) => WriterOutcome::ClosedByUs(code),
Ok(Err(error)) => WriterOutcome::Failed(error),
Err(timeout) => WriterOutcome::Failed(timeout),
};
}
continue;
}
item = queue.recv() => item,
_ = async {
match ping.as_mut() {
Some(interval) => {
interval.tick().await;
}
None => std::future::pending::<()>().await,
}
} => {
match timed(sender.ping_and_check(&reader_busy)).await.unwrap_or_else(Err) {
Ok(true) => continue,
Ok(false) => {
log::debug!("WebSocket peer stopped answering keepalive pings");
return match timed(
sender.close(CloseCode::GOING_AWAY, "no response to keepalive"),
)
.await
{
Ok(Ok(())) => WriterOutcome::ClosedByUs(CloseCode::GOING_AWAY),
Ok(Err(error)) | Err(error) => WriterOutcome::Failed(error),
};
}
Err(error) => return WriterOutcome::Failed(error),
}
}
};
match item {
Some(Outbound::Send(message)) => {
match timed(sender.send(message)).await {
Ok(Ok(())) => {}
Ok(Err(error)) | Err(error) => return WriterOutcome::Failed(error),
}
}
None => return WriterOutcome::QueueClosed,
}
}
}
async fn timed<T>(
operation: impl std::future::Future<Output = Result<T>>,
) -> std::result::Result<Result<T>, Error> {
tokio::time::timeout(WRITE_TIMEOUT, operation)
.await
.map_err(|_| {
Error::WebSocket(format!(
"a WebSocket write did not complete within {}s; the peer is not reading",
WRITE_TIMEOUT.as_secs()
))
})
}
pub(crate) async fn run(
socket: WebSocket,
path: String,
handshake: Handshake,
configure: Box<dyn FnOnce(&SocketRoom) + Send>,
ping_interval: Option<Duration>,
json_only: bool,
) -> Result<()> {
let (outbound, queue) = mpsc::channel(CLIENT_QUEUE_CAPACITY);
let (closing, closing_rx) = watch::channel(None);
let closing = Arc::new(closing);
let (_room, client) =
join(&path, configure, outbound, Arc::clone(&closing), handshake).await?;
let membership = Membership {
room: Arc::clone(client.room()),
id: client.id(),
};
let reader_busy = crate::websocket::socket::ReaderBusy::default();
let (sender, mut receiver) = socket.split();
let mut writer = AbortOnDrop(tokio::spawn(write_loop(
sender,
queue,
closing_rx,
ping_interval,
reader_busy.clone(),
)));
let mut code = CloseCode::from_u16(1006);
let mut writer_finished = false;
let mut peer_closed = false;
{
let _busy = reader_busy.enter();
tokio::select! {
biased;
_ = fire_open(&client) => {}
result = &mut writer.0 => {
writer_finished = true;
code = handle_writer_outcome(&client, result, &mut receiver).await;
}
}
}
while !writer_finished {
let received = tokio::select! {
biased;
received = receiver.receive() => received,
result = &mut writer.0, if !writer_finished => {
writer_finished = true;
code = handle_writer_outcome(&client, result, &mut receiver).await;
break;
}
};
match received {
Received::Message(message) => {
if json_only && !crate::websocket::socket::is_json(&message) {
client.close(
CloseCode::UNSUPPORTED_DATA,
"this endpoint accepts JSON only",
);
code = CloseCode::UNSUPPORTED_DATA;
break;
}
let _busy = reader_busy.enter();
tokio::select! {
biased;
_ = fire_message(&client, message) => {}
result = &mut writer.0, if !writer_finished => {
writer_finished = true;
code = handle_writer_outcome(&client, result, &mut receiver).await;
break;
}
}
}
Received::Closed(peer_code) => {
code = peer_code.unwrap_or(CloseCode::from_u16(1005));
peer_closed = true;
break;
}
Received::Eof => {
peer_closed = true;
break;
}
Received::Failed(error, failure_code) => {
code = failure_code;
client.close(code, reason_for_close(code));
report(&client, error).await;
break;
}
}
}
if !writer_finished {
client.close(code, reason_for_close(code));
}
drop(membership);
if tokio::time::timeout(CLOSE_HANDSHAKE_TIMEOUT, fire_close(&client, code))
.await
.is_err()
{
log::error!(
"WebSocket on_close for {path} did not finish within {}s; abandoning it",
CLOSE_HANDSHAKE_TIMEOUT.as_secs()
);
}
if !writer_finished {
let result = (&mut writer.0).await;
match result {
Ok(WriterOutcome::ClosedByUs(_)) if !peer_closed => {
drain_until_closed(&mut receiver).await;
}
Ok(WriterOutcome::ClosedByUs(_)) | Ok(WriterOutcome::QueueClosed) => {}
Ok(WriterOutcome::Failed(error)) => report(&client, error).await,
Err(join_error) => {
report(
&client,
Error::WebSocket(format!("socket writer task failed: {join_error}")),
)
.await;
}
}
}
drop(client);
Ok(())
}
async fn handle_writer_outcome(
client: &SocketClient,
result: std::result::Result<WriterOutcome, tokio::task::JoinError>,
receiver: &mut crate::websocket::socket::WebSocketReceiver,
) -> CloseCode {
match result {
Ok(WriterOutcome::ClosedByUs(code)) => {
drain_until_closed(receiver).await;
code
}
Ok(WriterOutcome::QueueClosed) => CloseCode::from_u16(1006),
Ok(WriterOutcome::Failed(error)) => {
report(client, error).await;
CloseCode::from_u16(1006)
}
Err(join_error) => {
report(
client,
Error::WebSocket(format!("socket writer task failed: {join_error}")),
)
.await;
CloseCode::INTERNAL_ERROR
}
}
}
fn reason_for_close(code: CloseCode) -> &'static str {
match code {
CloseCode::GOING_AWAY => "server shutting down",
CloseCode::MESSAGE_TOO_BIG => "message exceeds the configured limit",
CloseCode::UNSUPPORTED_DATA => "this endpoint accepts JSON only",
CloseCode::INVALID_PAYLOAD => "text frame was not valid UTF-8",
CloseCode::PROTOCOL_ERROR => "WebSocket protocol violation",
_ => "",
}
}
async fn drain_until_closed(receiver: &mut crate::websocket::socket::WebSocketReceiver) {
let drain = async {
while let Received::Message(_) = receiver.receive().await {}
};
if tokio::time::timeout(CLOSE_HANDSHAKE_TIMEOUT, drain).await.is_err() {
log::debug!("peer did not answer the close handshake within the timeout");
}
}