Browse Source
745: Refactor `maker_inc_connections::Actor` r=luckysori a=luckysori I'm clearly doing something wrong because the contract setup actor test doesn't pass. For some reason I'm not seeing the solution clearly even though we talked about it, `@thomaseizinger.` Co-authored-by: Lucas Soriano del Pino <l.soriano.del.pino@gmail.com>chore/leaner-release-process
committed by
GitHub
4 changed files with 67 additions and 108 deletions
@ -1,58 +0,0 @@ |
|||||
use std::fmt; |
|
||||
|
|
||||
use xtra::prelude::MessageChannel; |
|
||||
use xtra::{Handler, KeepRunning}; |
|
||||
|
|
||||
/// A forwarding actor that only forwards [`Result::Ok`] values and shuts itself down upon the first
|
|
||||
/// error.
|
|
||||
pub struct Actor<M> { |
|
||||
forward: Box<dyn MessageChannel<M>>, |
|
||||
} |
|
||||
|
|
||||
impl<M> Actor<M> { |
|
||||
pub fn new(forward: Box<dyn MessageChannel<M>>) -> Self { |
|
||||
Self { forward } |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
pub struct Message<TOk, TErr>(pub Result<TOk, TErr>); |
|
||||
|
|
||||
impl<TOk, TErr> xtra::Message for Message<TOk, TErr> |
|
||||
where |
|
||||
TOk: Send + 'static, |
|
||||
TErr: Send + 'static, |
|
||||
{ |
|
||||
type Result = KeepRunning; |
|
||||
} |
|
||||
|
|
||||
#[async_trait::async_trait] |
|
||||
impl<TOk, TErr> Handler<Message<TOk, TErr>> for Actor<TOk> |
|
||||
where |
|
||||
TOk: xtra::Message<Result = ()> + Send + 'static, |
|
||||
TErr: fmt::Display + Send + 'static, |
|
||||
{ |
|
||||
async fn handle( |
|
||||
&mut self, |
|
||||
Message(result): Message<TOk, TErr>, |
|
||||
_: &mut xtra::Context<Self>, |
|
||||
) -> KeepRunning { |
|
||||
let ok = match result { |
|
||||
Ok(ok) => ok, |
|
||||
Err(e) => { |
|
||||
tracing::error!("Stopping forwarding due to error: {}", e); |
|
||||
|
|
||||
return KeepRunning::StopSelf; |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
if let Err(xtra::Disconnected) = self.forward.send(ok).await { |
|
||||
tracing::info!("Target actor disappeared, stopping"); |
|
||||
|
|
||||
return KeepRunning::StopSelf; |
|
||||
} |
|
||||
|
|
||||
KeepRunning::Yes |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
impl<T: 'static + Send> xtra::Actor for Actor<T> {} |
|
Loading…
Reference in new issue