Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion tako-rs-core/src/graphql/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ where
keepalive_timeout,
} = self;
if let Some(on_upgrade) = request.extensions_mut().remove::<OnUpgrade>() {
ws::spawn_session(on_upgrade, move |socket| {
let lease = match request
.extensions()
.get::<crate::server_support::UpgradeRegistry>()
{
Some(registry) => match registry.register() {
Some(lease) => Some(lease),
None => return ws::rejection(StatusCode::SERVICE_UNAVAILABLE),
},
None => None,
};

ws::spawn_session(on_upgrade, lease, move |socket| {
GraphQLWebSocket::new(socket, executor, protocol)
.with_data(data)
.on_connection_init(on_connection_init)
Expand Down
6 changes: 6 additions & 0 deletions tako-rs-core/src/server_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod http1_io;
mod lease;
mod shutdown;
mod timer;
mod upgrades;

use std::future::Future;
use std::pin::Pin;
Expand All @@ -20,10 +21,15 @@ pub use driver::ConnDriver;
use futures_util::future::Either;
pub use headers::sanitize_response;
pub use http::http_service;
pub use http::http_service_with_upgrades;
pub use http1_io::Http1Io;
pub use lease::Leased;
pub use shutdown::ShutdownSignal;
pub use timer::ConnectionTimer;
pub use upgrades::UpgradeRegistry;
#[cfg(feature = "ws")]
pub(crate) use upgrades::UpgradeSession;
pub use upgrades::UpgradeSupervisor;

use crate::router::Router;

Expand Down
12 changes: 11 additions & 1 deletion tako-rs-core/src/server_support/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl ConnActivity {
pub struct ConnCtx {
router: Arc<Router>,
peer: SocketAddr,
upgrades: Option<super::UpgradeRegistry>,
/// Request activity the header deadline watches; shared with the bodies of
/// streamed responses.
pub activity: Arc<ConnActivity>,
Expand All @@ -79,9 +80,15 @@ impl ConnCtx {
Self {
router,
peer,
upgrades: None,
activity: Arc::default(),
}
}

pub fn with_upgrades(mut self, upgrades: super::UpgradeRegistry) -> Self {
self.upgrades = Some(upgrades);
self
}
}

/// Response body that keeps the deadline suspended until Hyper consumes and flushes it.
Expand Down Expand Up @@ -135,7 +142,10 @@ where
hyper::service::service_fn(move |req: hyper::Request<Incoming>| {
let ctx = ctx.clone();
ctx.activity.request_started();
let req = req.map(TakoBody::incoming);
let mut req = req.map(TakoBody::incoming);
if let Some(upgrades) = &ctx.upgrades {
upgrades.attach(&mut req);
}
// Boxing the full pipeline keeps this future small: hyper moves it into
// place on every request, and the full pipeline's state is large.
let pending = match ctx.router.begin(req, Some(ctx.peer)) {
Expand Down
32 changes: 31 additions & 1 deletion tako-rs-core/src/server_support/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,39 @@ pub fn http_service(
Response = Response,
Error = Infallible,
Future = impl Future<Output = Result<Response, Infallible>>,
> {
service(router, info, None)
}

/// HTTP service with supervised WebSocket sessions.
pub fn http_service_with_upgrades(
router: Arc<Router>,
info: ConnInfo,
upgrades: super::UpgradeRegistry,
) -> impl Service<
hyper::Request<Incoming>,
Response = Response,
Error = Infallible,
Future = impl Future<Output = Result<Response, Infallible>>,
> {
service(router, info, Some(upgrades))
}

fn service(
router: Arc<Router>,
info: ConnInfo,
upgrades: Option<super::UpgradeRegistry>,
) -> impl Service<
hyper::Request<Incoming>,
Response = Response,
Error = Infallible,
Future = impl Future<Output = Result<Response, Infallible>>,
> {
let router = connection_router(&router);
hyper::service::service_fn(move |req: hyper::Request<Incoming>| {
hyper::service::service_fn(move |mut req: hyper::Request<Incoming>| {
if let Some(upgrades) = &upgrades {
upgrades.attach(&mut req);
}
start_request(Arc::clone(&router), req.map(TakoBody::incoming), &info)
})
}
Expand Down
183 changes: 183 additions & 0 deletions tako-rs-core/src/server_support/upgrades.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use std::sync::Arc;

use futures_util::future::AbortHandle;
use parking_lot::Mutex;
use tokio::sync::Notify;

#[cfg(all(test, feature = "ws"))]
mod tests;

/// Owns upgraded sessions and aborts them on drop.
#[derive(Default)]
pub struct UpgradeSupervisor {
registry: UpgradeRegistry,
}

/// Registers upgraded sessions with their server's shutdown lifecycle.
#[derive(Clone, Default)]
pub struct UpgradeRegistry(Arc<Inner>);

#[derive(Default)]
struct Inner {
state: Mutex<State>,
stopping: Notify,
drained: Notify,
}

#[derive(Default)]
struct State {
stopping: bool,
tasks: Vec<Option<AbortHandle>>,
#[cfg(feature = "ws")]
free: Vec<usize>,
active: usize,
}

impl UpgradeSupervisor {
pub fn new(registry: UpgradeRegistry) -> Self {
Self { registry }
}

pub fn registry(&self) -> UpgradeRegistry {
self.registry.clone()
}

/// Rejects new sessions and asks existing sessions to close gracefully.
pub fn trigger(&self) {
self.registry.0.state.lock().stopping = true;
self.registry.0.stopping.notify_waiters();
}

/// Cancels sessions on their next poll; [`Self::join`] waits for socket cleanup.
pub fn abort(&self) {
self.trigger();
let tasks = self
.registry
.0
.state
.lock()
.tasks
.iter()
.flatten()
.cloned()
.collect::<Vec<_>>();

for task in tasks {
task.abort();
}
}

pub async fn join(&self) {
self.registry.join().await;
}
}

impl Drop for UpgradeSupervisor {
fn drop(&mut self) {
self.abort();
}
}

impl UpgradeRegistry {
/// Waits until all registered session futures have been dropped.
pub async fn join(&self) {
loop {
let notified = self.0.drained.notified();
tokio::pin!(notified);
notified.as_mut().enable();

if self.0.state.lock().active == 0 {
return;
}

notified.await;
}
}

/// Attaches the registry to requests that support HTTP upgrades.
pub fn attach<B>(
&self,
#[cfg_attr(not(feature = "ws"), allow(unused_variables))] request: &mut http::Request<B>,
) {
#[cfg(feature = "ws")]
if request
.extensions()
.get::<hyper::upgrade::OnUpgrade>()
.is_some()
{
request.extensions_mut().insert(self.clone());
}
}

#[cfg(feature = "ws")]
pub(crate) fn register(&self) -> Option<UpgradeSession> {
let mut state = self.0.state.lock();
if state.stopping {
return None;
}

let (handle, registration) = AbortHandle::new_pair();
let slot = if let Some(slot) = state.free.pop() {
state.tasks[slot] = Some(handle);
slot
} else {
state.tasks.push(Some(handle));
state.tasks.len() - 1
};
state.active += 1;

Some(UpgradeSession {
registry: self.clone(),
slot,
registration: Some(registration),
})
}

#[cfg(feature = "ws")]
pub(crate) async fn stopping(&self) {
loop {
let notified = self.0.stopping.notified();
tokio::pin!(notified);
notified.as_mut().enable();

if self.0.state.lock().stopping {
return;
}

notified.await;
}
}
}

#[cfg(feature = "ws")]
pub(crate) struct UpgradeSession {
registry: UpgradeRegistry,
slot: usize,
registration: Option<futures_util::future::AbortRegistration>,
}

#[cfg(feature = "ws")]
impl UpgradeSession {
pub(crate) fn registry(&self) -> UpgradeRegistry {
self.registry.clone()
}

pub(crate) async fn run(mut self, future: impl std::future::Future<Output = ()>) {
let registration = self.registration.take().expect("session runs once");
let _ = futures_util::future::Abortable::new(future, registration).await;
}
}

#[cfg(feature = "ws")]
impl Drop for UpgradeSession {
fn drop(&mut self) {
let mut state = self.registry.0.state.lock();
state.tasks[self.slot] = None;
state.free.push(self.slot);
state.active -= 1;

if state.active == 0 {
self.registry.0.drained.notify_waiters();
}
}
}
Loading
Loading