use std::num::NonZeroUsize;
use crate::{Lane, MailboxSender, MailboxSnapshot, RetainedBytes, sync::Arc};
use super::{ReactorGroupSendError, ReactorGroupSendFailure, ReactorId, admission::Shared};
pub struct ReactorGroupHandle<T> {
shared: Arc<Shared<T>>,
}
pub(super) struct AdmissionCloser<T> {
shared: Arc<Shared<T>>,
}
pub(super) fn bounded_ingress<T>(
senders: Vec<MailboxSender<T>>,
) -> (ReactorGroupHandle<T>, AdmissionCloser<T>) {
let shared = Arc::new(Shared::new(senders));
(
ReactorGroupHandle {
shared: Arc::clone(&shared),
},
AdmissionCloser { shared },
)
}
impl<T> ReactorGroupHandle<T> {
pub fn try_send(&self, reactor: ReactorId, item: T) -> Result<(), ReactorGroupSendError<T>> {
self.try_send_to(reactor, Lane::Work, item)
}
pub fn try_send_control(
&self,
reactor: ReactorId,
item: T,
) -> Result<(), ReactorGroupSendError<T>> {
self.try_send_to(reactor, Lane::Control, item)
}
pub fn try_send_to(
&self,
reactor: ReactorId,
lane: Lane,
item: T,
) -> Result<(), ReactorGroupSendError<T>> {
if !self.shared.is_open() {
return Err(ReactorGroupSendError::new(
item,
lane,
ReactorGroupSendFailure::Closed,
));
}
let Some(sender) = reactor
.position()
.and_then(|position| self.shared.shards.get(position))
else {
return Err(ReactorGroupSendError::new(
item,
lane,
ReactorGroupSendFailure::UnknownReactor {
reactor,
reactors: self.shared.reactors,
},
));
};
let _admission = sender.lock();
if !self.shared.is_open() {
return Err(ReactorGroupSendError::new(
item,
lane,
ReactorGroupSendFailure::Closed,
));
}
sender.ingress.try_send_to(lane, item).map_err(|error| {
let (item, rejected_lane, failure) = error.into_parts();
ReactorGroupSendError::new(
item,
rejected_lane,
ReactorGroupSendFailure::Mailbox(failure),
)
})
}
pub fn try_send_materialized<U>(
&self,
reactor: ReactorId,
lane: Lane,
owner: U,
retained_bytes: impl FnOnce(&U) -> RetainedBytes,
materialize: impl FnOnce(U) -> T,
) -> Result<(), ReactorGroupSendError<U>> {
if !self.shared.is_open() {
return Err(ReactorGroupSendError::new(
owner,
lane,
ReactorGroupSendFailure::Closed,
));
}
let Some(sender) = reactor
.position()
.and_then(|position| self.shared.shards.get(position))
else {
return Err(ReactorGroupSendError::new(
owner,
lane,
ReactorGroupSendFailure::UnknownReactor {
reactor,
reactors: self.shared.reactors,
},
));
};
let _admission = sender.lock();
if !self.shared.is_open() {
return Err(ReactorGroupSendError::new(
owner,
lane,
ReactorGroupSendFailure::Closed,
));
}
sender
.ingress
.try_send_materialized(lane, owner, retained_bytes, materialize)
.map_err(|error| {
let (item, rejected_lane, failure) = error.into_parts();
ReactorGroupSendError::new(
item,
rejected_lane,
ReactorGroupSendFailure::Mailbox(failure),
)
})
}
pub fn reactors(&self) -> NonZeroUsize {
self.shared.reactors
}
pub fn is_open(&self) -> bool {
self.shared.is_open()
}
pub fn mailbox_snapshot(&self, reactor: ReactorId) -> Option<MailboxSnapshot> {
reactor
.position()
.and_then(|position| self.shared.shards.get(position))
.map(|shard| shard.ingress.snapshot())
}
pub(super) fn into_senders(self) -> Vec<MailboxSender<T>> {
let shared = Arc::try_unwrap(self.shared)
.unwrap_or_else(|_| panic!("reactor group ingress ownership invariant violated"));
shared
.shards
.into_vec()
.into_iter()
.map(|shard| shard.ingress)
.collect()
}
}
impl<T> Clone for ReactorGroupHandle<T> {
fn clone(&self) -> Self {
Self {
shared: Arc::clone(&self.shared),
}
}
}
impl<T> AdmissionCloser<T> {
pub(super) fn close(&self) {
self.shared.close();
}
}
impl<T> Clone for AdmissionCloser<T> {
fn clone(&self) -> Self {
Self {
shared: Arc::clone(&self.shared),
}
}
}
impl<T> core::fmt::Debug for ReactorGroupHandle<T> {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("ReactorGroupHandle")
.field("reactors", &self.shared.reactors)
.field("open", &self.is_open())
.finish_non_exhaustive()
}
}