use std::time::Duration;
use futures::{Stream, StreamExt};
use tracing::{debug, info};
use tokio::pin;
use crate::{AbortOnDrop, ShutdownSignal, connection::ConnectionUpdate, host_runtime::Connection};
pub const GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(3);
pub trait LibraryEntrypoint<IO>: std::fmt::Debug + Send + Sync + 'static {
fn entrypoint(
self,
shutdown: ShutdownSignal,
conn_events: impl Stream<Item = ConnectionUpdate<IO>> + Send + Sync + 'static,
) -> impl Future<Output = ()> + Send;
}
#[derive(Debug, Default)]
pub struct Main;
impl<IO> LibraryEntrypoint<IO> for Main
where
IO: Connection,
{
async fn entrypoint(
self,
shutdown: ShutdownSignal,
conn_events: impl Stream<Item = ConnectionUpdate<IO>> + Send + Sync + 'static,
) {
info!(
version = env!("CARGO_PKG_VERSION"),
"starting rc-x509-client instance"
);
let _conn_events = AbortOnDrop::from(tokio::spawn(handle_connection_events(conn_events)));
shutdown.wait_for_shutdown().await;
info!("stopping rc-x509-client instance");
}
}
async fn handle_connection_events<IO>(
incoming: impl Stream<Item = ConnectionUpdate<IO>> + Send + Sync + 'static,
) where
IO: std::fmt::Debug,
{
debug!("starting connection event handler");
pin!(incoming);
while let Some(event) = incoming.next().await {
debug!(?event, "received connection lifecycle event");
}
debug!("stopping connection event handler");
}