Skip to main content

puerto_sqlx/eventos/
deliveries.rs

1//! The `db` transport: no broker, one `entregas_eventos` row per event and subscribed consumer.
2
3use std::collections::BTreeMap;
4use std::time::Duration;
5
6use async_trait::async_trait;
7use puerto_nucleo::SobreEvento;
8use puerto_nucleo::eventos::entrega::Entrega;
9use puerto_nucleo::eventos::transporte::{
10    ErrorTransporte, PublicadorEventos, Reservado, SuscripcionEventos,
11};
12use sqlx::PgPool;
13
14use super::{ENVELOPE_COLUMNS, envelope_from_pg, unavailable};
15
16/// How long a claimed delivery stays invisible to other claims. A worker that dies while
17/// handling one leaves it due again after this — at-least-once, never lost.
18pub const RESERVA_POR_DEFECTO: Duration = Duration::from_secs(60);
19
20/// The relay's publisher on the `db` transport: fans each event out to the consumers subscribed
21/// to its type and marks it published, in one transaction.
22pub struct EntregasPostgres {
23    pool: PgPool,
24    /// event type → the consumers subscribed to it, in the order they subscribed.
25    routes: BTreeMap<String, Vec<String>>,
26}
27
28impl EntregasPostgres {
29    pub fn new(pool: PgPool) -> Self {
30        Self {
31            pool,
32            routes: BTreeMap::new(),
33        }
34    }
35
36    /// Subscribes `consumer` to `event_types` — one `[[consumer]]` of puerto.toml.
37    pub fn enrutar(mut self, consumer: &str, event_types: &[&str]) -> Self {
38        for event_type in event_types {
39            let consumers = self.routes.entry((*event_type).to_string()).or_default();
40            if !consumers.iter().any(|c| c == consumer) {
41                consumers.push(consumer.to_string());
42            }
43        }
44        self
45    }
46
47    /// Who receives an event of this type. None is fine: the event is marked published anyway.
48    pub fn consumidores_de(&self, event_type: &str) -> &[String] {
49        self.routes.get(event_type).map_or(&[], Vec::as_slice)
50    }
51}
52
53#[async_trait]
54impl PublicadorEventos for EntregasPostgres {
55    async fn publicar(&self, event: &SobreEvento) -> Result<(), ErrorTransporte> {
56        let context = "entregas: publicar";
57        let mut tx = self.pool.begin().await.map_err(unavailable(context))?;
58        sqlx::query(
59            "INSERT INTO entregas_eventos (id_evento, consumidor) \
60             SELECT $1, consumidor FROM unnest($2::text[]) AS consumidor \
61             ON CONFLICT DO NOTHING",
62        )
63        .bind(event.id)
64        .bind(self.consumidores_de(&event.tipo_evento))
65        .execute(&mut *tx)
66        .await
67        .map_err(unavailable(context))?;
68        sqlx::query(
69            "UPDATE outbox SET publicado_en = now() WHERE id = $1 AND publicado_en IS NULL",
70        )
71        .bind(event.id)
72        .execute(&mut *tx)
73        .await
74        .map_err(unavailable(context))?;
75        tx.commit().await.map_err(unavailable(context))
76    }
77}
78
79/// One consumer's input on the `db` transport.
80pub struct SuscripcionPostgres {
81    pool: PgPool,
82    consumer: String,
83    reserva: Duration,
84}
85
86impl SuscripcionPostgres {
87    pub fn new(pool: PgPool, consumer: impl Into<String>) -> Self {
88        Self {
89            pool,
90            consumer: consumer.into(),
91            reserva: RESERVA_POR_DEFECTO,
92        }
93    }
94
95    /// How long a claim is kept from other workers; longer than the slowest handler.
96    pub fn con_reserva(mut self, reserva: Duration) -> Self {
97        self.reserva = reserva;
98        self
99    }
100}
101
102#[async_trait]
103impl SuscripcionEventos for SuscripcionPostgres {
104    fn consumidor(&self) -> &str {
105        &self.consumer
106    }
107
108    /// Claims the due deliveries, oldest event first. A claim counts as a delivery (`attempts`
109    /// goes up now, not when it is settled), so a worker that crashes mid-handle still uses one
110    /// of the event's deliveries, and the claim is hidden for the lease.
111    async fn siguiente_lote(&mut self, max: u32) -> Result<Vec<Reservado>, ErrorTransporte> {
112        let context = "entregas: siguiente_lote";
113        let rows = sqlx::query(&format!(
114            "WITH due AS ( \
115                 SELECT d.id_evento FROM entregas_eventos d \
116                 JOIN outbox o ON o.id = d.id_evento \
117                 WHERE d.consumidor = $1 AND d.estado = 'pendiente' AND d.siguiente_intento_en <= now() \
118                 ORDER BY o.secuencia LIMIT $2 \
119                 FOR UPDATE OF d SKIP LOCKED \
120             ), claimed AS ( \
121                 UPDATE entregas_eventos d \
122                 SET intentos = d.intentos + 1, \
123                     siguiente_intento_en = now() + make_interval(secs => $3) \
124                 FROM due WHERE d.id_evento = due.id_evento AND d.consumidor = $1 \
125                 RETURNING d.id_evento, d.intentos \
126             ) \
127             SELECT {ENVELOPE_COLUMNS}, claimed.intentos FROM claimed \
128             JOIN outbox o ON o.id = claimed.id_evento \
129             ORDER BY o.secuencia"
130        ))
131        .bind(&self.consumer)
132        .bind(i64::from(max))
133        .bind(self.reserva.as_secs_f64())
134        .fetch_all(&self.pool)
135        .await
136        .map_err(unavailable(context))?;
137        rows.iter()
138            .map(|row| {
139                let envelope = envelope_from_pg(row, context)?;
140                let attempts: i32 = sqlx::Row::try_get(row, "intentos")
141                    .map_err(|_| ErrorTransporte::DatosInvalidos)?;
142                let attempt =
143                    u32::try_from(attempts).map_err(|_| ErrorTransporte::DatosInvalidos)?;
144                Ok(Reservado::new(envelope, attempt))
145            })
146            .collect()
147    }
148
149    async fn resolver(
150        &mut self,
151        claimed: &Reservado,
152        delivery: &Entrega,
153    ) -> Result<(), ErrorTransporte> {
154        let (id, consumer) = (claimed.sobre.id, &self.consumer);
155        let result = match delivery {
156            Entrega::Ack => {
157                sqlx::query(
158                    "UPDATE entregas_eventos SET estado = 'hecho', ultimo_error = NULL \
159                     WHERE id_evento = $1 AND consumidor = $2",
160                )
161                .bind(id)
162                .bind(consumer)
163                .execute(&self.pool)
164                .await
165            }
166            Entrega::Reintentar { espera } => {
167                sqlx::query(
168                    "UPDATE entregas_eventos \
169                     SET siguiente_intento_en = now() + make_interval(secs => $3) \
170                     WHERE id_evento = $1 AND consumidor = $2",
171                )
172                .bind(id)
173                .bind(consumer)
174                .bind(espera.as_secs_f64())
175                .execute(&self.pool)
176                .await
177            }
178            Entrega::Descartar { motivo, detalle } => {
179                sqlx::query(
180                    "UPDATE entregas_eventos SET estado = 'descartado', ultimo_error = $3 \
181                     WHERE id_evento = $1 AND consumidor = $2",
182                )
183                .bind(id)
184                .bind(consumer)
185                .bind(format!("{motivo}: {detalle}"))
186                .execute(&self.pool)
187                .await
188            }
189        };
190        result.map_err(unavailable("entregas: resolver"))?;
191        Ok(())
192    }
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198    use sqlx::postgres::PgPoolOptions;
199
200    fn lazy_pool() -> PgPool {
201        PgPoolOptions::new()
202            .connect_lazy("postgres://puerto@127.0.0.1:1/puerto")
203            .unwrap()
204    }
205
206    #[tokio::test]
207    async fn should_route_an_event_to_every_consumer_subscribed_to_its_type() {
208        let deliveries = EntregasPostgres::new(lazy_pool())
209            .enrutar(
210                "notify_new_product",
211                &["product_created", "product_published"],
212            )
213            .enrutar("audit", &["product_created"]);
214
215        assert_eq!(
216            deliveries.consumidores_de("product_created"),
217            ["notify_new_product", "audit"]
218        );
219        assert_eq!(
220            deliveries.consumidores_de("product_published"),
221            ["notify_new_product"]
222        );
223    }
224
225    #[tokio::test]
226    async fn should_route_to_nobody_when_no_consumer_subscribed() {
227        let deliveries = EntregasPostgres::new(lazy_pool()).enrutar("audit", &["product_created"]);
228
229        assert!(deliveries.consumidores_de("product_deleted").is_empty());
230    }
231
232    #[tokio::test]
233    async fn should_route_once_when_a_consumer_subscribes_twice() {
234        let deliveries = EntregasPostgres::new(lazy_pool())
235            .enrutar("audit", &["product_created", "product_created"])
236            .enrutar("audit", &["product_created"]);
237
238        assert_eq!(deliveries.consumidores_de("product_created"), ["audit"]);
239    }
240
241    #[tokio::test]
242    async fn should_name_its_consumer_and_default_lease_when_subscribed() {
243        let subscription = SuscripcionPostgres::new(lazy_pool(), "audit");
244
245        assert_eq!(subscription.consumidor(), "audit");
246        assert_eq!(subscription.reserva, RESERVA_POR_DEFECTO);
247        assert_eq!(
248            subscription.con_reserva(Duration::from_secs(5)).reserva,
249            Duration::from_secs(5)
250        );
251    }
252}