use std::{
collections::{BTreeMap, VecDeque},
fmt,
future::Future,
pin::Pin,
sync::{Arc, PoisonError, Weak},
task::{Context, Poll, Waker},
};
use crate::sync::{
Mutex, RwLock,
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
};
use futures::{
Stream,
channel::{mpsc, oneshot},
task::AtomicWaker,
};
use rig_core::{
effect::{EffectId, EffectKind, HandlerDescriptor, HandlerKey, Outcome},
error::{ErrorKind, ErrorReport},
streaming::Relayed,
tool::{PublishedContext, ToolContext},
};
pub(super) struct Shared {
next_id: AtomicU64,
descriptors: RwLock<BTreeMap<HandlerKey, HandlerDescriptor>>,
queue: Mutex<CommandQueue>,
dispatchers: AtomicUsize,
serial_per_handler: bool,
causality: Mutex<Causality>,
closed: AtomicBool,
commands_closed: AtomicBool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BusId(u64);
impl BusId {
pub const fn as_u64(self) -> u64 {
self.0
}
}
impl fmt::Display for BusId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "bus#{:x}", self.0)
}
}
pub(super) enum Enqueue {
Sent,
Parked(Command),
Refused(Command),
Cancelled(Command),
Closed,
}
struct CommandQueue {
commands: VecDeque<Command>,
capacity: usize,
driver: Option<Waker>,
senders: Vec<Weak<AtomicWaker>>,
}
impl Shared {
pub(super) fn new(config: rig_core::serve::ServingPolicy) -> Self {
Self {
serial_per_handler: config.serial_per_handler,
causality: Mutex::new(Causality::default()),
next_id: AtomicU64::new(1),
descriptors: RwLock::new(BTreeMap::new()),
queue: Mutex::new(CommandQueue {
commands: VecDeque::new(),
capacity: config.command_capacity.max(1),
driver: None,
senders: Vec::new(),
}),
dispatchers: AtomicUsize::new(0),
closed: AtomicBool::new(false),
commands_closed: AtomicBool::new(false),
}
}
pub(super) fn try_close_commands(&self) -> bool {
let queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
if queue.commands.is_empty() && self.dispatchers() == 0 {
self.commands_closed.store(true, Ordering::SeqCst);
true
} else {
false
}
}
#[cfg(rig_loom)]
pub(super) fn commands_closed(&self) -> bool {
self.commands_closed.load(Ordering::SeqCst)
}
pub(super) fn id(self: &Arc<Self>) -> BusId {
BusId(Arc::as_ptr(self) as usize as u64)
}
pub(super) fn descriptors(&self) -> Vec<HandlerDescriptor> {
self.descriptors
.read()
.unwrap_or_else(PoisonError::into_inner)
.values()
.cloned()
.collect()
}
pub(super) fn driver_died(&self) {
self.descriptors
.write()
.unwrap_or_else(PoisonError::into_inner)
.clear();
}
pub(super) fn mark_closed(&self) {
let (commands, senders) = {
let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
self.closed.store(true, Ordering::SeqCst);
(
std::mem::take(&mut queue.commands),
std::mem::take(&mut queue.senders),
)
};
for command in commands {
command.reply.fail(bus_closed());
}
wake_parked(senders);
}
pub(super) fn enqueue(
&self,
command: Command,
parked: &Arc<AtomicWaker>,
cx: &Context<'_>,
) -> Enqueue {
if command.lineage.is_cancelled() {
return Enqueue::Cancelled(command);
}
if self.is_reentrant(&command) {
return Enqueue::Refused(command);
}
parked.register(cx.waker());
let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
if self.closed.load(Ordering::SeqCst) || self.commands_closed.load(Ordering::SeqCst) {
drop(queue);
drop(command);
return Enqueue::Closed;
}
if command.lineage.is_cancelled() {
return Enqueue::Cancelled(command);
}
if queue.commands.len() >= queue.capacity {
if !queue
.senders
.iter()
.any(|slot| Weak::ptr_eq(slot, &Arc::downgrade(parked)))
{
queue.senders.push(Arc::downgrade(parked));
}
return Enqueue::Parked(command);
}
queue.commands.push_back(command);
let driver = queue.driver.take();
drop(queue);
if let Some(driver) = driver {
driver.wake();
}
Enqueue::Sent
}
pub(super) fn drain(&self, cx: &Context<'_>) -> VecDeque<Command> {
let next_driver = cx.waker().clone();
let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
let commands = std::mem::take(&mut queue.commands);
let previous_driver = queue.driver.replace(next_driver);
let senders = std::mem::take(&mut queue.senders);
drop(queue);
drop(previous_driver);
wake_parked(senders);
commands
}
#[cfg(all(test, not(rig_loom)))]
pub(super) fn parked_senders(&self) -> usize {
self.queue
.lock()
.unwrap_or_else(PoisonError::into_inner)
.senders
.len()
}
pub(super) fn buffered(&self) -> usize {
self.queue
.lock()
.unwrap_or_else(PoisonError::into_inner)
.commands
.len()
}
pub(super) fn dispatcher_opened(&self) {
self.dispatchers.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn dispatcher_closed(&self) {
if self.dispatchers.fetch_sub(1, Ordering::SeqCst) == 1 {
self.wake_driver();
}
}
pub(super) fn dispatchers(&self) -> usize {
self.dispatchers.load(Ordering::SeqCst)
}
pub(super) fn is_closed(&self) -> bool {
self.closed.load(Ordering::SeqCst)
}
pub(super) fn publish_descriptor(
&self,
key: HandlerKey,
descriptor: HandlerDescriptor,
) -> Result<(), ErrorReport> {
let family = descriptor.family.family();
let mut descriptors = self
.descriptors
.write()
.unwrap_or_else(PoisonError::into_inner);
if let Some(current) = descriptors.get(&key) {
let current_family = current.family.family();
if current_family != family {
return Err(ErrorReport::new(
ErrorKind::HandlerUnavailable,
format!(
"key `{key}` serves the {current_family:?} family; a {family:?} handler cannot replace it"
),
)
.with_retryable(false));
}
}
descriptors.insert(
key.clone(),
HandlerDescriptor {
key,
family: descriptor.family,
layers: descriptor.layers,
},
);
Ok(())
}
pub(super) fn retract_descriptor(&self, key: &HandlerKey) -> bool {
self.descriptors
.write()
.unwrap_or_else(PoisonError::into_inner)
.remove(key)
.is_some()
}
fn wake_driver(&self) {
let driver = self
.queue
.lock()
.unwrap_or_else(PoisonError::into_inner)
.driver
.take();
if let Some(driver) = driver {
driver.wake();
}
}
pub(super) fn begin_lineage(
&self,
lineage: Arc<Lineage>,
key: HandlerKey,
) -> Result<Arc<CancelFlag>, ChainCancelled> {
let mut causality = self
.causality
.lock()
.unwrap_or_else(PoisonError::into_inner);
if lineage.is_cancelled() {
return Err(ChainCancelled);
}
let flag = Arc::clone(&lineage.cancelled);
causality
.in_flight
.insert(lineage.id, InFlightEntry { key, lineage });
Ok(flag)
}
#[cfg(all(test, rig_loom))]
pub(super) fn retained_lineage(&self, id: EffectId) -> Arc<Lineage> {
self.causality
.lock()
.unwrap_or_else(PoisonError::into_inner)
.in_flight
.get(&id)
.expect("test dispatch is active")
.lineage
.clone()
}
#[cfg(test)]
pub(super) fn begin_in_flight(
&self,
id: EffectId,
key: HandlerKey,
parent: Option<EffectId>,
) -> Result<Arc<CancelFlag>, ChainCancelled> {
let parent = parent.map(|id| {
self.causality
.lock()
.unwrap_or_else(PoisonError::into_inner)
.in_flight
.get(&id)
.expect("test parent is active")
.lineage
.clone()
});
self.begin_lineage(Lineage::new(id, parent), key)
}
pub(super) fn end_in_flight(&self, id: EffectId) -> bool {
self.causality
.lock()
.unwrap_or_else(PoisonError::into_inner)
.in_flight
.remove(&id)
.is_some_and(|entry| entry.lineage.is_cancelled())
}
pub(super) fn fail_cancelled_buffered(&self) {
let orphans = {
let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
let (orphans, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut queue.commands)
.into_iter()
.partition(|command| command.lineage.is_cancelled());
queue.commands = kept.into();
orphans
};
for orphan in orphans {
orphan.reply.fail(rig_core::serve::cancelled());
}
}
pub(super) fn cancel_descendants(&self, id: EffectId) {
let flags = {
let causality = self
.causality
.lock()
.unwrap_or_else(PoisonError::into_inner);
let flags: Vec<_> = causality
.in_flight
.values()
.filter(|entry| entry.lineage.contains(id))
.map(|entry| entry.lineage.cancelled.clone())
.collect();
for flag in &flags {
flag.set.store(true, std::sync::atomic::Ordering::SeqCst);
}
flags
};
for flag in flags {
flag.waker.wake();
}
let (senders, driver) = {
let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner);
(std::mem::take(&mut queue.senders), queue.driver.take())
};
wake_parked(senders);
if let Some(driver) = driver {
driver.wake();
}
}
fn is_reentrant(&self, command: &Command) -> bool {
if !self.serial_per_handler {
return false;
}
let causality = self
.causality
.lock()
.unwrap_or_else(PoisonError::into_inner);
let mut next = command.lineage.parent.as_deref();
while let Some(node) = next {
if causality
.in_flight
.get(&node.id)
.is_some_and(|entry| entry.key == command.key)
{
return true;
}
next = node.parent.as_deref();
}
false
}
pub(super) fn descriptor(&self, key: &HandlerKey) -> Option<HandlerDescriptor> {
self.descriptors
.read()
.unwrap_or_else(PoisonError::into_inner)
.get(key)
.cloned()
}
pub(super) fn keys(&self) -> Vec<HandlerKey> {
self.descriptors
.read()
.unwrap_or_else(PoisonError::into_inner)
.keys()
.cloned()
.collect()
}
}
fn wake_parked(senders: Vec<Weak<AtomicWaker>>) {
for slot in senders {
if let Some(parked) = slot.upgrade() {
parked.wake();
}
}
}
#[derive(Default)]
pub(super) struct Causality {
in_flight: BTreeMap<EffectId, InFlightEntry>,
}
pub(super) struct ChainCancelled;
pub(super) struct Lineage {
id: EffectId,
parent: Option<Arc<Lineage>>,
cancelled: Arc<CancelFlag>,
}
impl Lineage {
pub(super) fn new(id: EffectId, parent: Option<Arc<Self>>) -> Arc<Self> {
Arc::new(Self {
id,
parent,
cancelled: Arc::new(CancelFlag::default()),
})
}
pub(super) fn is_cancelled(&self) -> bool {
let mut next = Some(self);
while let Some(node) = next {
if node.cancelled.is_set() {
return true;
}
next = node.parent.as_deref();
}
false
}
fn contains(&self, id: EffectId) -> bool {
let mut next = Some(self);
while let Some(node) = next {
if node.id == id {
return true;
}
next = node.parent.as_deref();
}
false
}
}
impl Drop for Lineage {
fn drop(&mut self) {
let mut next = self.parent.take();
while let Some(parent) = next {
match Arc::try_unwrap(parent) {
Ok(mut node) => next = node.parent.take(),
Err(_) => break,
}
}
}
}
struct InFlightEntry {
key: HandlerKey,
lineage: Arc<Lineage>,
}
#[derive(Default)]
pub(super) struct CancelFlag {
set: std::sync::atomic::AtomicBool,
waker: AtomicWaker,
}
impl CancelFlag {
pub(super) fn is_set(&self) -> bool {
self.set.load(std::sync::atomic::Ordering::SeqCst)
}
pub(super) fn wait(self: &Arc<Self>) -> CancelWait {
CancelWait(Arc::clone(self))
}
}
pub(super) struct CancelWait(Arc<CancelFlag>);
impl Future for CancelWait {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if self.0.is_set() {
return Poll::Ready(());
}
self.0.waker.register(cx.waker());
if self.0.is_set() {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
pub(super) struct Command {
pub(super) lineage: Arc<Lineage>,
pub(super) id: EffectId,
pub(super) key: HandlerKey,
pub(super) kind: EffectKind,
pub(super) parent: Option<EffectId>,
pub(super) scope: Option<Arc<str>>,
pub(super) context: Option<ToolContext>,
pub(super) adapter_context: Option<rig_core::observe::AdapterContext>,
pub(super) published: Option<Arc<PublishedContext>>,
pub(super) reply: Reply,
pub(super) span: tracing::Span,
pub(super) cancel: oneshot::Receiver<()>,
}
pub(super) enum Reply {
Unary(oneshot::Sender<Result<Outcome, ErrorReport>>),
Stream(mpsc::Sender<Result<Relayed, ErrorReport>>),
}
impl Reply {
pub(super) fn fail(self, report: ErrorReport) {
match self {
Self::Unary(sender) => {
let _ = sender.send(Err(report));
}
Self::Stream(sender) => {
let _ = sender.clone().try_send(Err(report));
}
}
}
}
pub struct Dispatcher {
lineage: Option<Arc<Lineage>>,
pub(super) shared: Arc<Shared>,
pub(super) stream_capacity: usize,
pub(super) parent: Option<EffectId>,
pub(super) scope: Option<Arc<str>>,
}
impl Dispatcher {
pub(super) fn open(shared: Arc<Shared>, stream_capacity: usize) -> Self {
shared.dispatcher_opened();
Self {
shared,
stream_capacity,
parent: None,
lineage: None,
scope: None,
}
}
pub(super) fn parented(
shared: Arc<Shared>,
stream_capacity: usize,
lineage: Arc<Lineage>,
scope: Option<Arc<str>>,
) -> Self {
Self {
shared,
stream_capacity,
parent: Some(lineage.id),
lineage: Some(lineage),
scope,
}
}
pub const fn parent(&self) -> Option<EffectId> {
self.parent
}
pub fn scoped(&self, scope: impl Into<Arc<str>>) -> Self {
let mut dispatcher = self.clone();
dispatcher.scope = Some(scope.into());
dispatcher
}
pub fn scope(&self) -> Option<&Arc<str>> {
self.scope.as_ref()
}
}
impl Clone for Dispatcher {
fn clone(&self) -> Self {
let mut dispatcher = match &self.lineage {
None => Self::open(Arc::clone(&self.shared), self.stream_capacity),
Some(lineage) => Self::parented(
Arc::clone(&self.shared),
self.stream_capacity,
lineage.clone(),
None,
),
};
dispatcher.scope = self.scope.clone();
dispatcher
}
}
impl Drop for Dispatcher {
fn drop(&mut self) {
if self.parent.is_none() {
self.shared.dispatcher_closed();
}
}
}
impl fmt::Debug for Dispatcher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Dispatcher")
.field("closed", &self.shared.is_closed())
.field("handlers", &self.shared.keys())
.finish_non_exhaustive()
}
}
impl Dispatcher {
fn mint(&self) -> EffectId {
EffectId::from_raw(self.shared.next_id.fetch_add(1, Ordering::SeqCst))
}
pub fn mint_id(&self) -> EffectId {
self.mint()
}
pub fn dispatch(&self, key: &HandlerKey, kind: EffectKind) -> Pending {
self.dispatch_with(key, kind, DispatchOptions::default())
}
pub fn dispatch_with(
&self,
key: &HandlerKey,
kind: EffectKind,
options: DispatchOptions,
) -> Pending {
let DispatchOptions {
id,
tool_context,
adapter_context,
} = options;
self.dispatch_in(
id.unwrap_or_else(|| self.mint()),
key,
kind,
tool_context,
adapter_context,
)
}
fn dispatch_in(
&self,
id: EffectId,
key: &HandlerKey,
kind: EffectKind,
context: Option<ToolContext>,
adapter_context: Option<rig_core::observe::AdapterContext>,
) -> Pending {
let (reply, receiver) = oneshot::channel();
let (cancel_guard, cancel) = oneshot::channel();
let published = (context.is_some() || matches!(kind, EffectKind::ToolCall { .. }))
.then(PublishedContext::new);
Pending {
id,
parent: self.parent,
state: PendingState::Sending {
command: Some(Command {
lineage: Lineage::new(id, self.lineage.clone()),
id,
key: key.clone(),
kind,
parent: self.parent,
scope: self.scope.clone(),
context,
adapter_context,
published: published.clone(),
reply: Reply::Unary(reply),
span: tracing::Span::current(),
cancel,
}),
},
receiver,
shared: self.shared.clone(),
parked: Arc::new(AtomicWaker::new()),
_cancel_guard: cancel_guard,
published,
}
}
pub(crate) fn refused(&self, report: ErrorReport) -> Pending {
let (_reply, receiver) = oneshot::channel();
let (cancel_guard, _cancel) = oneshot::channel();
Pending {
id: self.mint_id(),
parent: self.parent,
state: PendingState::Failed(Some(report)),
receiver,
shared: self.shared.clone(),
parked: Arc::new(AtomicWaker::new()),
_cancel_guard: cancel_guard,
published: None,
}
}
pub fn dispatch_stream(&self, key: &HandlerKey, kind: EffectKind) -> EffectStream {
self.dispatch_stream_with(key, kind, DispatchOptions::default())
}
pub fn dispatch_stream_with(
&self,
key: &HandlerKey,
kind: EffectKind,
options: DispatchOptions,
) -> EffectStream {
let DispatchOptions {
id,
adapter_context,
..
} = options;
self.dispatch_stream_in(
id.unwrap_or_else(|| self.mint()),
key,
kind,
adapter_context,
)
}
fn dispatch_stream_in(
&self,
id: EffectId,
key: &HandlerKey,
kind: EffectKind,
adapter_context: Option<rig_core::observe::AdapterContext>,
) -> EffectStream {
if !kind.streams() {
return EffectStream {
_cancel_guard: None,
id,
parent: self.parent,
state: StreamState::Failed(Some(ErrorReport::new(
ErrorKind::Request,
format!(
"invalid dispatch: `{}` is a unary effect and cannot be dispatched as a stream",
kind.name()
),
))),
shared: self.shared.clone(),
parked: Arc::new(AtomicWaker::new()),
};
}
let (events, receiver) = mpsc::channel(self.stream_capacity);
let (cancel_guard, cancel) = oneshot::channel();
EffectStream {
id,
parent: self.parent,
state: StreamState::Sending {
command: Some(Command {
lineage: Lineage::new(id, self.lineage.clone()),
id,
key: key.clone(),
kind,
parent: self.parent,
scope: self.scope.clone(),
context: None,
adapter_context,
published: None,
reply: Reply::Stream(events),
span: tracing::Span::current(),
cancel,
}),
receiver: Some(receiver),
},
shared: self.shared.clone(),
parked: Arc::new(AtomicWaker::new()),
_cancel_guard: Some(cancel_guard),
}
}
pub fn descriptor(&self, key: &HandlerKey) -> Option<HandlerDescriptor> {
self.shared.descriptor(key)
}
pub fn keys(&self) -> Vec<HandlerKey> {
self.shared.keys()
}
pub fn descriptors(&self) -> Vec<HandlerDescriptor> {
self.shared.descriptors()
}
pub fn id(&self) -> BusId {
self.shared.id()
}
pub fn is_closed(&self) -> bool {
self.shared.is_closed()
}
pub fn buffered(&self) -> usize {
self.shared.buffered()
}
}
pub(super) fn bus_closed() -> ErrorReport {
ErrorReport::new(ErrorKind::BusClosed, "the bus driver is gone").with_retryable(false)
}
fn reentrant(key: &HandlerKey) -> ErrorReport {
ErrorReport::new(
ErrorKind::Request,
format!(
"re-entrant dispatch: the handler serving `{key}` dispatched to its own key under serial serving and would wait on itself"
),
)
.with_retryable(false)
}
pub(super) fn stream_truncated() -> ErrorReport {
rig_core::serve::stream_truncated()
}
pub(super) fn handler_unavailable(key: &HandlerKey) -> ErrorReport {
ErrorReport::new(
ErrorKind::HandlerUnavailable,
format!("no handler serves key `{key}`"),
)
.with_retryable(false)
}
fn reply_dropped(shared: &Shared) -> ErrorReport {
if shared.is_closed() {
bus_closed()
} else {
ErrorReport::new(
ErrorKind::Internal,
"the handler dropped its outcome sink without answering",
)
}
}
enum PendingState {
Sending {
command: Option<Command>,
},
Waiting,
Failed(Option<ErrorReport>),
}
#[derive(Debug, Default)]
pub struct DispatchOptions {
pub id: Option<EffectId>,
pub tool_context: Option<ToolContext>,
pub adapter_context: Option<rig_core::observe::AdapterContext>,
}
impl DispatchOptions {
#[must_use = "the setting applies to the returned value"]
pub fn with_id(mut self, id: EffectId) -> Self {
self.id = Some(id);
self
}
#[must_use = "the setting applies to the returned value"]
pub fn with_tool_context(mut self, context: ToolContext) -> Self {
self.tool_context = Some(context);
self
}
#[must_use = "the setting applies to the returned value"]
pub fn with_adapter_context(mut self, context: rig_core::observe::AdapterContext) -> Self {
self.adapter_context = Some(context);
self
}
}
#[must_use = "a dispatch does nothing until polled"]
pub struct Pending {
id: EffectId,
parent: Option<EffectId>,
state: PendingState,
receiver: oneshot::Receiver<Result<Outcome, ErrorReport>>,
shared: Arc<Shared>,
parked: Arc<AtomicWaker>,
_cancel_guard: oneshot::Sender<()>,
published: Option<Arc<PublishedContext>>,
}
impl Pending {
pub fn published_context(&self) -> Option<Arc<PublishedContext>> {
self.published.clone()
}
pub const fn parent(&self) -> Option<EffectId> {
self.parent
}
pub const fn id(&self) -> EffectId {
self.id
}
}
impl fmt::Debug for Pending {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pending").field("id", &self.id).finish()
}
}
impl Future for Pending {
type Output = Result<Outcome, ErrorReport>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
loop {
match &mut this.state {
PendingState::Sending { command } => {
if this.shared.is_closed() {
return Poll::Ready(Err(bus_closed()));
}
let Some(taken) = command.take() else {
return Poll::Ready(Err(ErrorReport::new(
ErrorKind::Internal,
"a dispatch was sent twice",
)));
};
match this.shared.enqueue(taken, &this.parked, cx) {
Enqueue::Sent => this.state = PendingState::Waiting,
Enqueue::Parked(kept) => {
*command = Some(kept);
return Poll::Pending;
}
Enqueue::Cancelled(cancelled) => {
drop(cancelled);
return Poll::Ready(Err(rig_core::serve::cancelled()));
}
Enqueue::Refused(refused) => {
return Poll::Ready(Err(reentrant(&refused.key)));
}
Enqueue::Closed => return Poll::Ready(Err(bus_closed())),
}
}
PendingState::Failed(report) => {
return Poll::Ready(Err(report.take().unwrap_or_else(|| {
ErrorReport::new(ErrorKind::Internal, "a refused dispatch was polled twice")
})));
}
PendingState::Waiting => {
return match Pin::new(&mut this.receiver).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(outcome)) => Poll::Ready(outcome),
Poll::Ready(Err(oneshot::Canceled)) => {
Poll::Ready(Err(reply_dropped(&this.shared)))
}
};
}
}
}
}
}
enum StreamState {
Sending {
command: Option<Command>,
receiver: Option<mpsc::Receiver<Result<Relayed, ErrorReport>>>,
},
Receiving {
receiver: mpsc::Receiver<Result<Relayed, ErrorReport>>,
saw_terminal: bool,
},
Failed(Option<ErrorReport>),
Done,
}
#[must_use = "a dispatch does nothing until polled"]
pub struct EffectStream {
id: EffectId,
parent: Option<EffectId>,
state: StreamState,
shared: Arc<Shared>,
parked: Arc<AtomicWaker>,
_cancel_guard: Option<oneshot::Sender<()>>,
}
impl EffectStream {
pub const fn parent(&self) -> Option<EffectId> {
self.parent
}
pub const fn id(&self) -> EffectId {
self.id
}
}
impl fmt::Debug for EffectStream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EffectStream")
.field("id", &self.id)
.finish()
}
}
impl Stream for EffectStream {
type Item = Result<Relayed, ErrorReport>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
loop {
match &mut this.state {
StreamState::Failed(report) => {
let report = report.take();
this.state = StreamState::Done;
return Poll::Ready(report.map(Err));
}
StreamState::Done => return Poll::Ready(None),
StreamState::Sending { command, receiver } => {
if this.shared.is_closed() {
this.state = StreamState::Done;
return Poll::Ready(Some(Err(bus_closed())));
}
let Some(taken) = command.take() else {
this.state = StreamState::Done;
return Poll::Ready(Some(Err(ErrorReport::new(
ErrorKind::Internal,
"a stream dispatch was sent twice",
))));
};
match this.shared.enqueue(taken, &this.parked, cx) {
Enqueue::Sent => {}
Enqueue::Parked(kept) => {
*command = Some(kept);
return Poll::Pending;
}
Enqueue::Cancelled(cancelled) => {
drop(cancelled);
this.state = StreamState::Done;
return Poll::Ready(Some(Err(rig_core::serve::cancelled())));
}
Enqueue::Refused(refused) => {
this.state = StreamState::Done;
return Poll::Ready(Some(Err(reentrant(&refused.key))));
}
Enqueue::Closed => {
this.state = StreamState::Done;
return Poll::Ready(Some(Err(bus_closed())));
}
}
let Some(receiver) = receiver.take() else {
this.state = StreamState::Done;
return Poll::Ready(Some(Err(ErrorReport::new(
ErrorKind::Internal,
"a stream dispatch was sent twice",
))));
};
this.state = StreamState::Receiving {
receiver,
saw_terminal: false,
};
}
StreamState::Receiving {
receiver,
saw_terminal,
} => {
return match Pin::new(receiver).poll_next(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Some(item)) => {
if matches!(item, Ok(Relayed::Done(_)) | Err(_)) {
*saw_terminal = true;
}
Poll::Ready(Some(item))
}
Poll::Ready(None) => {
let terminated = *saw_terminal;
this.state = StreamState::Done;
if terminated {
Poll::Ready(None)
} else if this.shared.is_closed() {
Poll::Ready(Some(Err(bus_closed())))
} else {
Poll::Ready(Some(Err(stream_truncated())))
}
}
};
}
}
}
}
}
const _: () = {
const fn assert_dispatcher<T: Clone + Send + Sync + 'static>() {}
const fn assert_unpin<T: Unpin + 'static>() {}
const fn assert_send<T: Send + 'static>() {}
assert_dispatcher::<Dispatcher>();
assert_unpin::<Pending>();
assert_unpin::<EffectStream>();
assert_send::<Pending>();
assert_send::<EffectStream>();
};
#[cfg(all(test, not(rig_loom)))]
mod tests;