This crate contains helpers related to common asynchronous programming patterns used in nearcore:
Sender<T>
and AsyncSender<T>
are abstractions of our Actix interfaces. When
a component needs to send a message to another component, the component should
keep a Sender<T>
as a field and require it during construction, like:
```rust
struct MyComponent {
downstream_component: Sender
impl MyComponent {
pub fn new(downstream_component: Sender
The sender can then be used to send messages:
rust
impl MyComponent {
fn do_something(&mut self, args: ...) {
self.downstream_component.send(DownstreamMessage::DataReady(...));
}
}
To create a Sender<T>
, we need any implementation of CanSend<T>
. One way is
to use an Actix address:
```rust
impl Handler
impl DownstreamActor {
pub fn spawn(...) -> Addr
fn setupsystem() { let addr = DownstreamActor::spawn(...); let mycomponent = MyComponent::new(addr.into_sender()); } ```
In tests, the TestLoopBuilder
provides the sender()
function which also
implements CanSend
, see the examples directory under this crate.
AsyncSender<T>
is similar, except that calling send_async
returns a future
that carries the response to the message.