|
| 1 | +use crate::api::error::LdkServerError; |
| 2 | +use async_trait::async_trait; |
| 3 | +use ldk_server_protos::events::EventEnvelope; |
| 4 | + |
| 5 | +/// A trait for publishing events or notifications from the LDK Server. |
| 6 | +/// |
| 7 | +/// Implementors of this trait define how events are sent to various messaging |
| 8 | +/// systems. It provides a consistent, asynchronous interface for event publishing, while allowing |
| 9 | +/// each implementation to manage its own initialization and configuration, typically sourced from |
| 10 | +/// the `ldk-server.config` file. A no-op implementation is included by default, |
| 11 | +/// with specific implementations enabled via feature flags. |
| 12 | +/// |
| 13 | +/// Events are represented as [`EventEnvelope`] messages, which are Protocol Buffers |
| 14 | +/// ([protobuf](https://protobuf.dev/)) objects defined in [`ldk_server_protos::events`]. |
| 15 | +/// These events are serialized to bytes by the publisher before transmission, and consumers can |
| 16 | +/// deserialize them using the protobuf definitions. |
| 17 | +/// |
| 18 | +/// The underlying messaging system is expected to support durably buffered events, |
| 19 | +/// enabling easy decoupling between the LDK Server and event consumers. |
| 20 | +#[async_trait] |
| 21 | +pub trait EventPublisher { |
| 22 | + /// Publishes an event to the underlying messaging system. |
| 23 | + /// |
| 24 | + /// # Arguments |
| 25 | + /// * `event` - The event message to publish, provided as an [`EventEnvelope`] |
| 26 | + /// defined in [`ldk_server_protos::events`]. Implementors must serialize |
| 27 | + /// the whole [`EventEnvelope`] to bytes before publishing. |
| 28 | + /// |
| 29 | + /// In order to ensure no events are lost, implementors of this trait must publish events |
| 30 | + /// durably to underlying messaging system. An event is considered published when |
| 31 | + /// [`EventPublisher::publish`] returns `Ok(())`, thus implementors MUST durably persist/publish events *before* |
| 32 | + /// returning `Ok(())`. |
| 33 | + /// |
| 34 | + /// # Errors |
| 35 | + /// May return an [`LdkServerErrorCode::InternalServerError`] if the event cannot be published, |
| 36 | + /// such as due to network failures, misconfiguration, or transport-specific issues. |
| 37 | + /// If event publishing fails, the LDK Server will retry publishing the event indefinitely, which |
| 38 | + /// may degrade performance until the underlying messaging system is operational again. |
| 39 | + /// |
| 40 | + /// [`LdkServerErrorCode::InternalServerError`]: crate::api::error::LdkServerErrorCode |
| 41 | + async fn publish(&self, event: EventEnvelope) -> Result<(), LdkServerError>; |
| 42 | +} |
0 commit comments