use std::future::Future;
use super::App;
use crate::bridge::InvocationScope;
use crate::execution::{connection_invocation, deadline_after};
use crate::{CloseFrame, HeldConnection};
pub(crate) struct OpenConnection {
channel: u64,
scope: InvocationScope,
_held: HeldConnection,
}
impl OpenConnection {
pub(super) fn new(channel: u64, scope: InvocationScope, held: HeldConnection) -> Self {
Self {
channel,
scope,
_held: held,
}
}
pub(super) fn into_scope(self) -> InvocationScope {
self.scope
}
}
impl App {
pub(crate) async fn hold_connection(
&mut self,
open: OpenConnection,
stopping: impl Future<Output = ()>,
) {
let host = self.host.clone();
let deadline = deadline_after(
self.limits
.connection_lifetime
.zip(self.limits.timeout)
.and_then(|(lifetime, timeout)| lifetime.checked_add(timeout)),
);
let expression = connection_invocation(open.channel);
{
let evaluation = self.evaluate(expression, deadline);
tokio::pin!(evaluation, stopping);
let mut stopped = false;
loop {
tokio::select! {
_ = &mut evaluation => break,
() = &mut stopping, if !stopped => {
stopped = true;
host.close_channels(&CloseFrame::new(
CloseFrame::GOING_AWAY,
"the server is shutting down",
));
}
}
}
}
self.close(open.scope);
}
}