nestrs-microservices 0.1.1

Microservice transport and client primitives for nestrs.
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Production Kafka transport using [rskafka](https://docs.rs/rskafka) (pure Rust).
//!
//! Wire format matches Redis/NATS: JSON `WireRequest` payloads on the `requests` topic; replies go to
//! a per-client `replies.{instance_id}` topic with record key = `correlation_id`.

use std::collections::BTreeMap;
use std::sync::Arc;

use super::connection::client_builder_from_parts;
use async_trait::async_trait;
use chrono::Utc;
use rskafka::client::partition::{Compression, OffsetAt, UnknownTopicHandling};
use rskafka::record::Record;
use serde_json::Value;
use tokio::sync::Mutex;
use uuid::Uuid;

use super::connection::KafkaConnectionOptions;
use crate::wire::{dispatch_emit, dispatch_send, WireError, WireKind, WireRequest, WireResponse};
use crate::{MicroserviceHandler, MicroserviceServer, ShutdownFuture, Transport, TransportError};

/// Client / producer options.
///
/// **Topic retention**: the rskafka `create_topic` helper does not set `retention.ms`. Configure retention via
/// broker defaults, `kafka-topics --alter`, or your cluster operator (Strimzi, MSK, Confluent, etc.).
#[derive(Clone, Debug)]
pub struct KafkaTransportOptions {
    pub bootstrap_brokers: Vec<String>,
    pub topic_prefix: String,
    pub request_timeout: std::time::Duration,
    pub replication_factor: i16,
    pub create_topics: bool,
    /// TLS (recommended with SASL on untrusted networks), SASL, and Kafka `client.id`.
    pub connection: KafkaConnectionOptions,
}

impl Default for KafkaTransportOptions {
    fn default() -> Self {
        Self {
            bootstrap_brokers: vec!["127.0.0.1:9092".to_string()],
            topic_prefix: "nestrs".to_string(),
            request_timeout: std::time::Duration::from_secs(30),
            replication_factor: 1i16,
            create_topics: true,
            connection: KafkaConnectionOptions::default(),
        }
    }
}

impl KafkaTransportOptions {
    pub fn new(brokers: Vec<String>) -> Self {
        Self {
            bootstrap_brokers: brokers,
            ..Default::default()
        }
    }

    fn requests_topic(&self) -> String {
        format!("{}.requests", self.topic_prefix)
    }

    fn replies_topic(&self, instance_id: &str) -> String {
        format!("{}.replies.{}", self.topic_prefix, instance_id)
    }
}

/// Nest-style Kafka [`Transport`] (JSON payloads, same patterns as Redis pub/sub).
pub struct KafkaTransport {
    options: KafkaTransportOptions,
    instance_id: String,
    client: Mutex<Option<Arc<rskafka::client::Client>>>,
}

impl KafkaTransport {
    pub fn new(options: KafkaTransportOptions) -> Self {
        Self {
            instance_id: Uuid::new_v4().simple().to_string(),
            options,
            client: Mutex::new(None),
        }
    }

    async fn connect(&self) -> Result<Arc<rskafka::client::Client>, TransportError> {
        let mut g = self.client.lock().await;
        if let Some(c) = g.as_ref() {
            return Ok(c.clone());
        }
        let builder = client_builder_from_parts(
            self.options.bootstrap_brokers.clone(),
            &self.options.connection,
        )
        .map_err(|e| TransportError::new(format!("kafka client options: {e}")))?;
        let c = Arc::new(
            builder
                .build()
                .await
                .map_err(|e| TransportError::new(format!("kafka connect failed: {e}")))?,
        );
        if self.options.create_topics {
            if let Ok(ctrl) = c.controller_client() {
                let _ = ctrl
                    .create_topic(
                        self.options.requests_topic(),
                        1,
                        self.options.replication_factor,
                        5_000,
                    )
                    .await;
                let _ = ctrl
                    .create_topic(
                        self.options.replies_topic(&self.instance_id),
                        1,
                        self.options.replication_factor,
                        5_000,
                    )
                    .await;
            }
        }
        *g = Some(c.clone());
        Ok(c)
    }

    async fn partition(
        &self,
        topic: &str,
    ) -> Result<rskafka::client::partition::PartitionClient, TransportError> {
        let c = self.connect().await?;
        c.partition_client(topic.to_owned(), 0, UnknownTopicHandling::Retry)
            .await
            .map_err(|e| {
                TransportError::new(format!("kafka partition client `{topic}` failed: {e}"))
            })
    }
}

#[async_trait]
impl Transport for KafkaTransport {
    async fn send_json(&self, pattern: &str, payload: Value) -> Result<Value, TransportError> {
        let correlation_id = Uuid::new_v4().simple().to_string();
        let reply_topic = self.options.replies_topic(&self.instance_id);
        let wire = WireRequest {
            kind: WireKind::Send,
            pattern: pattern.to_string(),
            payload,
            reply: Some(reply_topic.clone()),
            correlation_id: Some(correlation_id.clone()),
        };
        let body = serde_json::to_vec(&wire)
            .map_err(|e| TransportError::new(format!("serialize request failed: {e}")))?;

        let req_pc = self.partition(&self.options.requests_topic()).await?;
        let rep_pc = self.partition(&reply_topic).await?;

        let start_off = rep_pc
            .get_offset(OffsetAt::Latest)
            .await
            .map_err(|e| TransportError::new(format!("kafka get_offset (replies) failed: {e}")))?;

        let record = Record {
            key: None,
            value: Some(body),
            headers: BTreeMap::new(),
            timestamp: Utc::now(),
        };
        req_pc
            .produce(vec![record], Compression::default())
            .await
            .map_err(|e| TransportError::new(format!("kafka produce failed: {e}")))?;
        #[cfg(feature = "microservice-metrics")]
        metrics::counter!("nestrs_microservice_kafka_produce_total", "topic" => "requests")
            .increment(1);

        let deadline = tokio::time::Instant::now() + self.options.request_timeout;
        let mut next_off = start_off;

        loop {
            if tokio::time::Instant::now() > deadline {
                return Err(TransportError::new("kafka request timed out"));
            }
            let (records, _) = rep_pc
                .fetch_records(next_off, 1..1_000_000, 500)
                .await
                .map_err(|e| TransportError::new(format!("kafka fetch (replies) failed: {e}")))?;

            if records.is_empty() {
                tokio::time::sleep(std::time::Duration::from_millis(15)).await;
                continue;
            }

            for ro in records {
                next_off = ro.offset + 1;
                let key_bytes = ro.record.key.as_deref().unwrap_or_default();
                let key_str = String::from_utf8_lossy(key_bytes);
                if key_str != correlation_id {
                    continue;
                }
                let val = ro
                    .record
                    .value
                    .as_deref()
                    .ok_or_else(|| TransportError::new("kafka reply missing value"))?;
                let wire: WireResponse = serde_json::from_slice(val).map_err(|e| {
                    TransportError::new(format!("deserialize response failed: {e}"))
                })?;
                if wire.ok {
                    return Ok(wire.payload.unwrap_or(Value::Null));
                }
                let err = wire.error.unwrap_or(WireError {
                    message: "microservice error".to_string(),
                    details: None,
                });
                let mut out = TransportError::new(err.message);
                if let Some(details) = err.details {
                    out = out.with_details(details);
                }
                return Err(out);
            }
        }
    }

    async fn emit_json(&self, pattern: &str, payload: Value) -> Result<(), TransportError> {
        let wire = WireRequest {
            kind: WireKind::Emit,
            pattern: pattern.to_string(),
            payload,
            reply: None,
            correlation_id: None,
        };
        let body = serde_json::to_vec(&wire)
            .map_err(|e| TransportError::new(format!("serialize event failed: {e}")))?;
        let req_pc = self.partition(&self.options.requests_topic()).await?;
        let record = Record {
            key: None,
            value: Some(body),
            headers: BTreeMap::new(),
            timestamp: Utc::now(),
        };
        req_pc
            .produce(vec![record], Compression::default())
            .await
            .map_err(|e| TransportError::new(format!("kafka produce failed: {e}")))?;
        #[cfg(feature = "microservice-metrics")]
        metrics::counter!("nestrs_microservice_kafka_produce_total", "topic" => "requests")
            .increment(1);
        Ok(())
    }
}

/// Server bootstrap options (topic layout matches [`KafkaTransportOptions`]).
#[derive(Clone, Debug)]
pub struct KafkaMicroserviceOptions {
    pub bootstrap_brokers: Vec<String>,
    pub topic_prefix: String,
    pub replication_factor: i16,
    pub create_topics: bool,
    pub connection: KafkaConnectionOptions,
}

impl Default for KafkaMicroserviceOptions {
    fn default() -> Self {
        Self {
            bootstrap_brokers: vec!["127.0.0.1:9092".to_string()],
            topic_prefix: "nestrs".to_string(),
            replication_factor: 1i16,
            create_topics: true,
            connection: KafkaConnectionOptions::default(),
        }
    }
}

impl KafkaMicroserviceOptions {
    pub fn new(brokers: Vec<String>) -> Self {
        Self {
            bootstrap_brokers: brokers,
            ..Default::default()
        }
    }

    fn requests_topic(&self) -> String {
        format!("{}.requests", self.topic_prefix)
    }
}

/// Consumes `*.requests` partition 0 and dispatches `WireRequest` payloads (same as Redis micro listener).
pub struct KafkaMicroserviceServer {
    options: KafkaMicroserviceOptions,
    client: Mutex<Option<Arc<rskafka::client::Client>>>,
    handlers: Vec<Arc<dyn MicroserviceHandler>>,
    next_offset: Mutex<i64>,
}

impl KafkaMicroserviceServer {
    pub fn new(
        options: KafkaMicroserviceOptions,
        handlers: Vec<Arc<dyn MicroserviceHandler>>,
    ) -> Self {
        Self {
            options,
            client: Mutex::new(None),
            handlers,
            next_offset: Mutex::new(0),
        }
    }

    async fn ensure_client(&self) -> Result<Arc<rskafka::client::Client>, TransportError> {
        let mut g = self.client.lock().await;
        if let Some(c) = g.as_ref() {
            return Ok(c.clone());
        }
        let builder = client_builder_from_parts(
            self.options.bootstrap_brokers.clone(),
            &self.options.connection,
        )
        .map_err(|e| TransportError::new(format!("kafka client options: {e}")))?;
        let c = Arc::new(
            builder
                .build()
                .await
                .map_err(|e| TransportError::new(format!("kafka connect failed: {e}")))?,
        );
        if self.options.create_topics {
            if let Ok(ctrl) = c.controller_client() {
                let _ = ctrl
                    .create_topic(
                        self.options.requests_topic(),
                        1,
                        self.options.replication_factor,
                        5_000,
                    )
                    .await;
            }
        }
        let req_pc = c
            .partition_client(
                self.options.requests_topic(),
                0,
                UnknownTopicHandling::Retry,
            )
            .await
            .map_err(|e| TransportError::new(format!("kafka partition client failed: {e}")))?;
        let earliest = req_pc.get_offset(OffsetAt::Earliest).await.unwrap_or(0);
        *self.next_offset.lock().await = earliest;
        *g = Some(c.clone());
        Ok(c)
    }

    pub async fn listen(self) -> Result<(), TransportError> {
        self.listen_with_shutdown(std::future::pending::<()>())
            .await
    }

    pub async fn listen_with_shutdown<F>(self, shutdown: F) -> Result<(), TransportError>
    where
        F: std::future::Future<Output = ()> + Send + 'static,
    {
        let client = self.ensure_client().await?;
        let requests_topic = self.options.requests_topic();
        let handlers = Arc::new(self.handlers);
        let next_offset = self.next_offset;
        let c2 = client;

        tokio::pin!(shutdown);
        loop {
            tokio::select! {
                _ = &mut shutdown => break,
                _ = tokio::time::sleep(std::time::Duration::from_millis(25)) => {
                    let req_pc = match c2
                        .partition_client(requests_topic.clone(), 0, UnknownTopicHandling::Retry)
                        .await
                    {
                        Ok(p) => p,
                        Err(_) => continue,
                    };
                    let fetched = {
                        let off = next_offset.lock().await;
                        req_pc.fetch_records(*off, 1..4_000_000, 900).await
                    };
                    let (records, _) = match fetched {
                        Ok(x) => x,
                        Err(_) => continue,
                    };
                    if records.is_empty() {
                        continue;
                    }
                    let last_off = records.iter().map(|r| r.offset).max().unwrap_or(0);
                    {
                        let mut off = next_offset.lock().await;
                        *off = last_off + 1;
                    }
                    for ro in records {
                        let payload_bytes = match ro.record.value.as_deref() {
                            Some(b) => b,
                            None => continue,
                        };
                        let req: WireRequest = match serde_json::from_slice(payload_bytes) {
                            Ok(v) => v,
                            Err(_) => continue,
                        };
                        let handlers = handlers.clone();
                        let client = c2.clone();
                        match req.kind {
                            WireKind::Send => {
                                let Some(reply_topic) = req.reply.clone() else { continue };
                                let corr = req.correlation_id.clone().unwrap_or_default();
                                tokio::spawn(async move {
                                    let res = dispatch_send(&handlers, &req.pattern, req.payload.clone()).await;
                                    let wire = match res {
                                        Ok(v) => WireResponse {
                                            ok: true,
                                            payload: Some(v),
                                            error: None,
                                        },
                                        Err(e) => WireResponse {
                                            ok: false,
                                            payload: None,
                                            error: Some(WireError {
                                                message: e.message,
                                                details: e.details,
                                            }),
                                        },
                                    };
                                    if let Ok(bytes) = serde_json::to_vec(&wire) {
                                        if let Ok(rep_pc) = client
                                            .partition_client(reply_topic.clone(), 0, UnknownTopicHandling::Retry)
                                            .await
                                        {
                                            let rec = Record {
                                                key: Some(corr.into_bytes()),
                                                value: Some(bytes),
                                                headers: BTreeMap::new(),
                                                timestamp: Utc::now(),
                                            };
                                            let _ = rep_pc.produce(vec![rec], Compression::default()).await;
                                            #[cfg(feature = "microservice-metrics")]
                                            metrics::counter!("nestrs_microservice_kafka_produce_total", "topic" => "replies")
                                                .increment(1);
                                        }
                                    }
                                });
                            }
                            WireKind::Emit => {
                                let handlers = handlers.clone();
                                tokio::spawn(async move {
                                    dispatch_emit(&handlers, &req.pattern, req.payload.clone()).await;
                                });
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

#[async_trait]
impl MicroserviceServer for KafkaMicroserviceServer {
    async fn listen_with_shutdown(
        self: Box<Self>,
        shutdown: ShutdownFuture,
    ) -> Result<(), TransportError> {
        (*self).listen_with_shutdown(shutdown).await
    }
}

/// Liveness probe: broker accepts a Kafka connection (no topic I/O).
pub async fn kafka_cluster_reachable(brokers: Vec<String>) -> Result<(), TransportError> {
    kafka_cluster_reachable_with(brokers, &KafkaConnectionOptions::default()).await
}

/// Same as [`kafka_cluster_reachable`] but with TLS / SASL / `client.id`.
pub async fn kafka_cluster_reachable_with(
    brokers: Vec<String>,
    connection: &KafkaConnectionOptions,
) -> Result<(), TransportError> {
    let builder = client_builder_from_parts(brokers, connection)
        .map_err(|e| TransportError::new(format!("kafka client options: {e}")))?;
    builder
        .build()
        .await
        .map(|_| ())
        .map_err(|e| TransportError::new(format!("kafka broker unreachable: {e}")))
}