Skip to content

In-Memory Events ​

The in-memory transport delivers events inside the current process. There is no broker, no serialization and no network. The publisher puts the event in a channel and the event engine dispatches it to the handlers registered for its key.

The shared event model and the event definition are covered in Handling Events. Controllers and their registration are covered in Controllers. This page covers what is specific to memory.

Enabling the transport ​

Add the events-memory feature to the Sword dependency:

toml
[dependencies]
sword = { version = "0.7", features = ["events", "events-memory"] }

Configuration ​

Configure the transport in the [events.memory] section. Every key is optional. An absent section uses the defaults:

KeyTypeDefaultDescription
buffer_sizeusize1024Channel capacity, in events (must be greater than zero)
num_of_event_retryu83Extra attempts after the first handler failure
delay_between_event_retry_msu641000Wait between attempts, in milliseconds
handler_timeout_msu64inherits [events]Overrides the handler timeout for this transport
toml
[events.memory]
buffer_size = 1024
num_of_event_retry = 3
delay_between_event_retry_ms = 1000

Publishing events ​

The engine registers MemoryEventPublisher as a provider. Inject it into the controller and publish with publish:

rust
use std::sync::Arc;

use sword::events::*;
use sword::prelude::*;
use sword::web::*;

use crate::orders::{CreateOrderDto, OrderCreated};

#[controller(kind = Controller::Web, path = "/orders")]
pub struct OrdersController {
    publisher: Arc<MemoryEventPublisher>,
}

impl OrdersController {
    #[post("/")]
    async fn create(&self, req: Request) -> WebResult {
        let body = req.body::<CreateOrderDto>()?;

        let event = OrderCreated {
            order_id: "order-1".to_string(),
            total: body.total,
        };

        self.publisher.publish(event).await.map_err(|error| {
            tracing::error!(%error, "failed to publish order.created");
            JsonResponse::InternalServerError().message("failed to publish event")
        })?;

        Ok(JsonResponse::Created().message("order created"))
    }
}
rust
use serde::{Deserialize, Serialize};
use sword::events::*;

#[event(key = "order.created")]
#[derive(Debug, Serialize, Deserialize)]
pub struct OrderCreated {
    pub order_id: String,
    pub total: f64,
}

A successful publish only confirms that the event is in the channel, not that it was handled.

The channel has a fixed capacity and is shared by all events. When it is full, publish waits for space instead of dropping the event.

The only transport-specific error is EventPublishError::Closed, which appears when the engine stops receiving.

For the publish signature and the remaining errors, see Handling Events.

Subscribing to events ​

Select the transport with the transport attribute of the controller macro. If the controller handles events from several transports, each handler can declare its own in the subscribe macro.

rust
use std::sync::Arc;

use sword::events::*;
use sword::prelude::*;

use crate::orders::OrderCreated;
use crate::store::EventStore;

#[controller(kind = Controller::Events, transport = EventTransport::Memory)]
pub struct OrderEventsHandler {
    store: Arc<EventStore>,
}

impl OrderEventsHandler {
    #[subscribe(key = "order.created")]
    async fn on_created(&self, event: &OrderCreated) -> EventHandlerResult<()> {
        self.store.record(&event.order_id);

        Ok(())
    }
}

INFO

Unlike NATS, this transport has no wildcards. Handlers must subscribe to the literal key of the event they want to handle. See NATS wildcards.

Failure behavior ​

SituationResult
buffer_size = 0Startup error, the application does not start
Handler returns an errorRetried if configured