use std::future::Future;
use std::time::Duration;
use super::handle::ServerHandle;
#[cfg(feature = "tls")]
#[inline]
pub(crate) fn tls_alpn_for_tcp() -> Vec<Vec<u8>> {
#[cfg(feature = "http2")]
{
vec![b"h2".to_vec(), b"http/1.1".to_vec()]
}
#[cfg(not(feature = "http2"))]
{
vec![b"http/1.1".to_vec()]
}
}
pub(crate) fn make_handle(
drain_timeout: Duration,
) -> (ServerHandle, impl Future<Output = ()> + Send + 'static) {
let shutdown = tokio_util::sync::CancellationToken::new();
let done = tokio_util::sync::CancellationToken::new();
let shutdown_for_task = shutdown.clone();
let fut = async move {
shutdown_for_task.cancelled().await;
};
(
ServerHandle {
shutdown,
done,
drain_timeout,
},
fut,
)
}
#[cfg(not(feature = "compio"))]
pub(crate) fn spawn_done<F>(done: tokio_util::sync::CancellationToken, fut: F)
where
F: Future<Output = ()> + Send + 'static,
{
tokio::spawn(async move {
fut.await;
done.cancel();
});
}
#[cfg(feature = "compio")]
pub(crate) fn spawn_done_compio<F>(done: tokio_util::sync::CancellationToken, fut: F)
where
F: Future<Output = ()> + 'static,
{
compio::runtime::spawn(async move {
fut.await;
done.cancel();
})
.detach();
}