1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![allow(clippy::blocks_in_conditions)]
mod config;
mod convert;
pub mod error;

#[allow(clippy::all)]
pub mod proto {
    tonic::include_proto!("services.outbox.v1");
}

use futures::StreamExt;
use proto::{outbox_service_server::OutboxService, *};
use tonic::{transport::Server, Request, Response, Status};
use tracing::instrument;

use super::{EventSequence, Outbox};
pub use config::*;
use error::*;

pub struct OutboxServer {
    outbox: Outbox,
}

#[tonic::async_trait]
impl OutboxService for OutboxServer {
    type SubscribeStream = std::pin::Pin<
        Box<dyn futures::Stream<Item = Result<CalaLedgerEvent, Status>> + Send + Sync + 'static>,
    >;

    #[instrument(name = "cala_ledger.subscribe", skip_all, fields(error, error.level, error.message), err)]
    async fn subscribe(
        &self,
        request: Request<SubscribeRequest>,
    ) -> Result<Response<Self::SubscribeStream>, Status> {
        cala_tracing::grpc::extract_tracing(&request);

        let SubscribeRequest { after_sequence } = request.into_inner();

        let outbox_listener = self
            .outbox
            .register_listener(after_sequence.map(EventSequence::from))
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?;
        Ok(Response::new(Box::pin(
            outbox_listener
                .map(|event| Ok(proto::CalaLedgerEvent::from(event)))
                .fuse(),
        )))
    }
}

pub(crate) async fn start(
    server_config: OutboxServerConfig,
    outbox: Outbox,
) -> Result<(), OutboxServerError> {
    let outbox_service = OutboxServer { outbox };
    Server::builder()
        .add_service(outbox_service_server::OutboxServiceServer::new(
            outbox_service,
        ))
        .serve(([0, 0, 0, 0], server_config.listen_port).into())
        .await?;
    Ok(())
}