use std::{io, sync::Arc};
use log::debug;
use tokio_util::sync::CancellationToken;
use crate::{
app::Envelope,
codec::FrameCodec,
connection::{ConnectionActor, ConnectionChannels, PEER_CLOSED_RESPONSE_GRACE},
hooks::{ConnectionContext, ProtocolHooks},
push::{FrameLike, PushHandle, PushQueuesBuilder},
session::{ConnectionId, SessionRegistry},
};
pub(super) struct ConnectionResponseActor<F: FrameCodec> {
pub(super) actor: ConnectionActor<Envelope, (), F::Frame>,
_push_handle: PushHandle<F::Frame>,
_registry: RegistryGuard<F::Frame>,
}
struct RegistryGuard<F: FrameLike> {
registry: Arc<SessionRegistry<F>>,
connection_id: ConnectionId,
handle: PushHandle<F>,
}
impl<F: FrameLike> RegistryGuard<F> {
const fn new(
registry: Arc<SessionRegistry<F>>,
connection_id: ConnectionId,
handle: PushHandle<F>,
) -> Self {
Self {
registry,
connection_id,
handle,
}
}
}
impl<F: FrameLike> Drop for RegistryGuard<F> {
fn drop(&mut self) {
let removed = self.registry.remove_owned(self.connection_id, &self.handle);
debug!(
"session registry entry released: connection_id={:?}, removed={removed}",
self.connection_id
);
}
}
pub(super) struct CancellationGuard(CancellationToken);
impl CancellationGuard {
pub(super) const fn new(token: CancellationToken) -> Self { Self(token) }
}
impl Drop for CancellationGuard {
fn drop(&mut self) { self.0.cancel(); }
}
pub(super) struct ConnectionActorInit<'a, F>
where
F: FrameCodec,
{
pub(super) protocol_hooks: &'a mut ProtocolHooks<F::Frame, ()>,
pub(super) shutdown: CancellationToken,
pub(super) registry: &'a Arc<SessionRegistry<F::Frame>>,
pub(super) push_queues: &'a PushQueuesBuilder<F::Frame>,
pub(super) context: ConnectionContext,
}
pub(super) fn build_connection_actor<F>(
init: ConnectionActorInit<'_, F>,
) -> io::Result<(
ConnectionResponseActor<F>,
CancellationGuard,
ConnectionContext,
)>
where
F: FrameCodec,
{
let ConnectionActorInit {
protocol_hooks,
shutdown,
registry,
push_queues,
context,
} = init;
let (queues, handle) = push_queues.clone().build().map_err(|error| {
io::Error::other(format!("failed to build connection push queues: {error}"))
})?;
let connection_id = registry.allocate_and_publish(&handle);
debug!("session registry entry published: connection_id={connection_id:?}");
let registry_guard = RegistryGuard::new(Arc::clone(registry), connection_id, handle.clone());
let mut connection_context = context.with_published_id(connection_id);
protocol_hooks.on_connection_setup(handle.clone(), &mut connection_context);
let mut actor = ConnectionActor::new_prepared_untracked(
ConnectionChannels::new(queues, handle.clone()),
shutdown.clone(),
connection_context.connection_id(),
connection_context.peer_addr(),
);
actor.set_peer_closed_grace(PEER_CLOSED_RESPONSE_GRACE);
actor.set_abandon_response_on_shutdown(false);
let response_actor = ConnectionResponseActor {
actor,
_push_handle: handle,
_registry: registry_guard,
};
Ok((
response_actor,
CancellationGuard::new(shutdown),
connection_context,
))
}
pub(super) fn observe_peer<P>(
actor: &mut ConnectionActor<Envelope, (), P>,
peer_eof: &CancellationToken,
) -> (
CancellationToken,
Arc<std::sync::OnceLock<crate::connection::UnobservedCause>>,
)
where
P: FrameLike,
{
let unobserved = peer_eof.child_token();
let cause = Arc::new(std::sync::OnceLock::new());
actor.set_peer_observation(unobserved.clone(), Arc::clone(&cause));
(unobserved, cause)
}