use std::any::Any;
use dactor::{Disposition, Headers, OutboundContext, OutboundInterceptor, Outcome, RuntimeHeaders};
use crate::header::{ContextHeader, ContextSnapshotHeader};
use crate::ErrorPolicy;
#[derive(Default)]
pub struct ContextOutboundInterceptor {
error_policy: ErrorPolicy,
}
impl ContextOutboundInterceptor {
pub fn new(error_policy: ErrorPolicy) -> Self {
Self { error_policy }
}
}
impl OutboundInterceptor for ContextOutboundInterceptor {
fn name(&self) -> &'static str {
"dcontext-outbound"
}
fn on_send(
&self,
ctx: &OutboundContext<'_>,
_runtime_headers: &RuntimeHeaders,
headers: &mut Headers,
_message: &dyn Any,
) -> Disposition {
if ctx.remote {
match dcontext::capture().serialize() {
Ok(bytes) => {
headers.insert(ContextHeader { bytes });
}
Err(e) => {
tracing::warn!(
target: "dcontext_dactor",
error = %e,
target_actor = %ctx.target_name,
"failed to serialize dcontext for outbound message"
);
if self.error_policy == ErrorPolicy::Reject {
return Disposition::Reject(format!("dcontext serialization failed: {e}"));
}
}
}
} else {
let snap = dcontext::capture();
headers.insert(ContextSnapshotHeader { snapshot: snap });
}
Disposition::Continue
}
fn on_reply(
&self,
_ctx: &OutboundContext<'_>,
_runtime_headers: &RuntimeHeaders,
_headers: &Headers,
_outcome: &Outcome<'_>,
) {
}
}