use crate::context::Context;
use crate::error::{Error, Result};
use crate::routing::SocketOptions;
use crate::websocket::handshake::{self, HandshakeOptions};
use crate::websocket::socket::WebSocket;
use futures::future::BoxFuture;
use std::time::Duration;
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
pub(crate) type SocketTask = Box<dyn FnOnce(WebSocket) -> BoxFuture<'static, Result<()>> + Send>;
pub(crate) struct PendingUpgrade {
pub(crate) on_upgrade: hyper::upgrade::OnUpgrade,
pub(crate) config: WebSocketConfig,
pub(crate) ping_interval: Option<Duration>,
pub(crate) json_only: bool,
pub(crate) path: String,
pub(crate) task: SocketTask,
pub(crate) permit: ConnectionPermit,
pub(crate) accept_key: String,
pub(crate) subprotocol: Option<String>,
}
impl std::fmt::Debug for PendingUpgrade {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PendingUpgrade")
.field("path", &self.path)
.field("ping_interval", &self.ping_interval)
.finish_non_exhaustive()
}
}
impl PendingUpgrade {
pub(crate) async fn run(self) {
let PendingUpgrade {
on_upgrade,
config,
ping_interval,
json_only,
path,
task,
permit: _permit,
accept_key: _,
subprotocol: _,
} = self;
let upgraded = match on_upgrade.await {
Ok(upgraded) => upgraded,
Err(error) => {
log::warn!("WebSocket upgrade failed for {path}: {error}");
return;
}
};
let socket =
WebSocket::from_upgraded(upgraded, config, ping_interval, json_only).await;
if let Err(error) = task(socket).await {
log::error!("WebSocket handler for {path} ended with an error: {error}");
}
}
}
pub(crate) fn socket_config(
settings: &crate::config::WebSocketSettings,
route: &SocketOptions,
) -> WebSocketConfig {
let max_message_size = route.max_message_size.unwrap_or(settings.max_message_size);
WebSocketConfig::default()
.max_message_size(Some(max_message_size))
.max_frame_size(Some(settings.max_frame_size.min(max_message_size)))
}
impl Context {
pub fn websocket<F, Fut>(&mut self, callback: F) -> Result<()>
where
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: std::future::Future<Output = Result<()>> + Send + 'static,
{
self.accept_upgrade(Box::new(move |socket| Box::pin(callback(socket))))
}
pub fn socket<F>(&mut self, configure: F) -> Result<()>
where
F: FnOnce(&crate::websocket::room::SocketRoom) + Send + 'static,
{
let handshake = crate::websocket::room::Handshake {
params: self.req.params.clone(),
query: self.req.query.clone(),
headers: self.req.headers.clone(),
ip: self.req.client_ip(),
session: self.session_arc().cloned(),
};
let path = self.req.path().to_string();
let json_only = self.socket_options.map(|o| o.json).unwrap_or(false);
let ping_interval = self.configured_ping_interval();
let configure: Box<dyn FnOnce(&crate::websocket::room::SocketRoom) + Send> =
Box::new(configure);
self.accept_upgrade(Box::new(move |socket| {
Box::pin(crate::websocket::room::run(
socket,
path,
handshake,
configure,
ping_interval,
json_only,
))
}))
}
fn configured_ping_interval(&self) -> Option<Duration> {
let seconds = crate::configuration::CONF::all()
.map(|config| config.websocket.ping_interval)
.unwrap_or_else(|| crate::config::WebSocketSettings::default().ping_interval);
match seconds {
0 => None,
seconds => Some(Duration::from_secs(seconds)),
}
}
pub fn is_upgrading(&self) -> bool {
self.pending_upgrade.is_some()
}
pub(crate) fn accept_upgrade(&mut self, task: SocketTask) -> Result<()> {
let settings = crate::configuration::CONF::all()
.map(|config| config.websocket.clone())
.unwrap_or_default();
let options = HandshakeOptions {
allowed_origins: settings.allowed_origins.clone(),
accept_subprotocols: settings.accept_subprotocols.clone(),
require_origin: settings.require_origin,
};
if is_shutting_down() {
self.set_response(service_unavailable(
"The server is shutting down and is not accepting WebSocket connections.",
));
return Ok(());
}
let Some(permit) = ConnectionPermit::acquire(settings.max_connections) else {
log::warn!(
"Refusing a WebSocket handshake for {}: at the configured limit of {} \
connections",
self.req.path(),
settings.max_connections
);
self.set_response(service_unavailable(
"The server is at its WebSocket connection limit.",
));
return Ok(());
};
let accepted = match handshake::validate(&self.req, &options) {
Ok(accepted) => accepted,
Err(response) => {
self.set_response(response);
return Ok(());
}
};
let route_options = match self.socket_options {
Some(options) => options,
None => return Err(not_a_socket_route(self.req.path())),
};
let on_upgrade = match self.req.take_upgrade() {
Some(on_upgrade) => on_upgrade,
None => {
self.set_response(handshake::upgrade_required(
"WebSocket requires HTTP/1.1; this connection cannot be upgraded.",
));
return Ok(());
}
};
self.pending_upgrade = Some(PendingUpgrade {
on_upgrade,
config: socket_config(&settings, &route_options),
json_only: route_options.json,
ping_interval: match settings.ping_interval {
0 => None,
seconds => Some(Duration::from_secs(seconds)),
},
path: self.req.path().to_string(),
task,
permit,
accept_key: accepted.accept_key.clone(),
subprotocol: accepted.subprotocol.clone(),
});
self.set_response(handshake::accept_response(&accepted));
Ok(())
}
}
static LIVE_SOCKETS: once_cell::sync::Lazy<
dashmap::DashMap<u64, tokio::task::JoinHandle<()>>,
> = once_cell::sync::Lazy::new(dashmap::DashMap::new);
static SOCKETS_DRAINED: once_cell::sync::Lazy<tokio::sync::Notify> =
once_cell::sync::Lazy::new(tokio::sync::Notify::new);
static NEXT_SOCKET_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
static RESERVED_SOCKETS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
pub(crate) struct ConnectionPermit;
impl ConnectionPermit {
fn acquire(limit: usize) -> Option<Self> {
if limit == 0 {
return Some(Self);
}
let mut current = RESERVED_SOCKETS.load(std::sync::atomic::Ordering::Acquire);
loop {
if current >= limit {
return None;
}
match RESERVED_SOCKETS.compare_exchange_weak(
current,
current + 1,
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Acquire,
) {
Ok(_) => return Some(Self),
Err(actual) => current = actual,
}
}
}
}
impl Drop for ConnectionPermit {
fn drop(&mut self) {
let mut current = RESERVED_SOCKETS.load(std::sync::atomic::Ordering::Acquire);
loop {
match RESERVED_SOCKETS.compare_exchange_weak(
current,
current.saturating_sub(1),
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Acquire,
) {
Ok(_) => break,
Err(actual) => current = actual,
}
}
}
}
static SHUTTING_DOWN: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
pub fn live_socket_count() -> usize {
LIVE_SOCKETS.len()
}
pub fn is_shutting_down() -> bool {
SHUTTING_DOWN.load(std::sync::atomic::Ordering::SeqCst)
}
pub(crate) fn spawn_tracked(pending: PendingUpgrade) -> bool {
let id = NEXT_SOCKET_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let (ready_tx, ready_rx) = tokio::sync::oneshot::channel::<()>();
let handle = tokio::spawn(async move {
let _ = ready_rx.await;
let _registration = Registration(id);
pending.run().await;
});
LIVE_SOCKETS.insert(id, handle);
if is_shutting_down() {
if let Some((_, handle)) = LIVE_SOCKETS.remove(&id) {
handle.abort();
}
SOCKETS_DRAINED.notify_waiters();
return false;
}
let _ = ready_tx.send(());
true
}
struct Registration(u64);
impl Drop for Registration {
fn drop(&mut self) {
LIVE_SOCKETS.remove(&self.0);
SOCKETS_DRAINED.notify_waiters();
}
}
pub async fn shutdown_sockets(timeout: Duration) {
SHUTTING_DOWN.store(true, std::sync::atomic::Ordering::SeqCst);
crate::websocket::room::SOCKET::shutdown_all();
if live_socket_count() == 0 {
return;
}
log::info!("Closing {} WebSocket connection(s)...", live_socket_count());
let drained = async {
while live_socket_count() > 0 {
let notified = SOCKETS_DRAINED.notified();
if live_socket_count() == 0 {
break;
}
notified.await;
}
};
if tokio::time::timeout(timeout, drained).await.is_err() {
let remaining: Vec<u64> = LIVE_SOCKETS.iter().map(|entry| *entry.key()).collect();
log::warn!(
"{} WebSocket connection(s) did not finish within the shutdown timeout; \
aborting them. A handler written against `ctx.websocket` should return \
when `recv()` yields `None`.",
remaining.len()
);
for id in remaining {
if let Some((_, handle)) = LIVE_SOCKETS.remove(&id) {
handle.abort();
}
}
}
}
pub(crate) fn reset_shutdown_state() {
SHUTTING_DOWN.store(false, std::sync::atomic::Ordering::SeqCst);
}
impl PendingUpgrade {
pub(crate) fn matches(&self, response: &crate::http::Response) -> bool {
let values = |name: &str| -> Vec<&str> {
response
.headers
.iter()
.filter(|(key, _)| key.eq_ignore_ascii_case(name))
.map(|(_, value)| value.as_str())
.collect()
};
fn exactly_one(values: &[&str], expected: &str) -> bool {
values.len() == 1 && values[0].eq_ignore_ascii_case(expected)
}
if response.status != hyper::StatusCode::SWITCHING_PROTOCOLS {
return false;
}
if !exactly_one(&values("Upgrade"), "websocket") {
return false;
}
let accept = values("Sec-WebSocket-Accept");
if accept.len() != 1 || accept[0] != self.accept_key {
return false;
}
let has_upgrade_token = values("Connection")
.iter()
.flat_map(|value| value.split(','))
.any(|token| token.trim().eq_ignore_ascii_case("upgrade"));
if !has_upgrade_token {
return false;
}
let protocol = values("Sec-WebSocket-Protocol");
let protocol_matches = match self.subprotocol.as_deref() {
Some(negotiated) => protocol.len() == 1 && protocol[0] == negotiated,
None => protocol.is_empty(),
};
protocol_matches && values("Sec-WebSocket-Extensions").is_empty()
}
}
fn service_unavailable(message: &str) -> crate::http::Response {
crate::http::Response::new(hyper::StatusCode::SERVICE_UNAVAILABLE)
.with_header("Content-Type", "text/plain; charset=utf-8")
.with_body(message.as_bytes().to_vec())
}
pub(crate) fn not_a_socket_route(path: &str) -> Error {
Error::WebSocket(format!(
"`{path}` is not a SOCKET route; declare it with \
`routes![SOCKET \"{path}\" => handler]`"
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_zero_limit_means_no_limit() {
let permits: Vec<ConnectionPermit> =
(0..1000).filter_map(|_| ConnectionPermit::acquire(0)).collect();
assert_eq!(permits.len(), 1000);
}
#[test]
fn permits_are_capped_and_released_on_drop() {
let base = RESERVED_SOCKETS.load(std::sync::atomic::Ordering::Acquire);
let limit = base + 2;
let first = ConnectionPermit::acquire(limit);
let second = ConnectionPermit::acquire(limit);
assert!(first.is_some() && second.is_some());
assert!(
ConnectionPermit::acquire(limit).is_none(),
"a third connection must be refused at the limit"
);
drop(second);
let third = ConnectionPermit::acquire(limit);
assert!(
third.is_some(),
"dropping a permit must free the slot it held"
);
drop(first);
drop(third);
assert_eq!(
RESERVED_SOCKETS.load(std::sync::atomic::Ordering::Acquire),
base,
"every permit must return its slot"
);
}
}