basilisk-rust-client 0.1.1

Rust client for the basilisk reverse proxy server
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use crate::error::{ClientError, ClientResult};
use crate::protocol::{
    ServiceBusEventEnvelope, ServiceBusForwardRequest, ServiceBusForwardResponse,
    ServiceBusProtocolMessage, protocol_types,
};
use chrono::Utc;
use futures_util::{FutureExt, future::BoxFuture};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::sync::{Mutex, RwLock, oneshot};
use tokio::time::{Duration, timeout};
use tracing::{debug, error, info, warn};

const COMMAND_RESPONSE_TIMEOUT: Duration = Duration::from_secs(10);
const METRICS_INTERVAL: Duration = Duration::from_secs(20);
const CONNECT_RETRY_BASE_DELAY: Duration = Duration::from_millis(500);
const CONNECT_RETRY_MAX_DELAY: Duration = Duration::from_secs(30);
const CONNECT_RETRY_MAX_JITTER_MS: u64 = 500;
const CONNECT_RETRY_MAX_ATTEMPTS: usize = 8;
const METRICS_TOPIC: &str = "basilisk.metrics.distribution";
const METRICS_MESSAGE_TYPE: &str = "basilisk.internal";

/// Async event-handler function signature used by `BusClient::on_event`.
pub type EventHandler =
    Arc<dyn Fn(ServiceBusEventEnvelope) -> BoxFuture<'static, ()> + Send + Sync>;
/// Async request-handler function signature used by `BusClient::on_request`.
pub type RequestHandler = Arc<
    dyn Fn(ServiceBusRequest, RequestResponder) -> BoxFuture<'static, ClientResult<()>>
        + Send
        + Sync,
>;

/// Low-level TCP service-bus client.
#[derive(Clone)]
pub struct BusClient {
    inner: Arc<Inner>,
}

struct Inner {
    service_id: String,
    instance_id: String,
    writer: Mutex<OwnedWriteHalf>,
    pending: Mutex<VecDeque<oneshot::Sender<ServiceBusProtocolMessage>>>,
    event_handlers: RwLock<HashMap<String, Vec<EventHandler>>>,
    request_handlers: RwLock<HashMap<String, RequestHandler>>,
}

#[derive(Debug, Clone)]
/// Wrapper around an incoming request-style event.
pub struct ServiceBusRequest {
    /// Incoming event envelope.
    pub event: ServiceBusEventEnvelope,
}

impl ServiceBusRequest {
    /// Returns the reply topic from the payload field ` reply_to `, if present.
    pub fn reply_to(&self) -> Option<&str> {
        self.event.payload.get("reply_to")?.as_str()
    }
}

/// Helper used by request handlers to publish replies.
#[derive(Clone)]
pub struct RequestResponder {
    client: BusClient,
    reply_to_topic: String,
    causation_id: String,
    correlation_id: i64,
    default_message_type: String,
}

impl RequestResponder {
    /// Sends a typed reply event to the request's reply topic.
    pub async fn respond(
        &self,
        message_type: impl Into<String>,
        payload: HashMap<String, serde_json::Value>,
    ) -> ClientResult<i32> {
        let event = ServiceBusEventEnvelope {
            event_id: String::new(),
            emitted_at_utc: Utc::now(),
            service_id: String::new(),
            instance_id: String::new(),
            topic: self.reply_to_topic.clone(),
            message_type: message_type.into(),
            correlation_id: self.correlation_id,
            causation_id: Some(self.causation_id.clone()),
            payload,
        };
        self.client.publish_event(event).await
    }

    /// Sends a reply using the request's original message type.
    pub async fn respond_ok(
        &self,
        payload: HashMap<String, serde_json::Value>,
    ) -> ClientResult<i32> {
        self.respond(self.default_message_type.clone(), payload)
            .await
    }
}

#[derive(Debug, Clone)]
/// Input payload for `BusClient::forward`.
pub struct ForwardRequest {
    /// Target service id that should process the request.
    pub target_service_id: String,
    /// Message type to execute at the target.
    pub message_type: String,
    /// Request payload.
    pub payload: HashMap<String, serde_json::Value>,
    /// Optional timeout in milliseconds.
    pub timeout_ms: Option<u64>,
}

impl BusClient {
    /// Opens a TCP connection and authenticates with a `connect` protocol frame.
    pub async fn connect(
        host: &str,
        port: u16,
        service_id: impl Into<String>,
        instance_id: impl Into<String>,
        token: impl Into<String>,
    ) -> ClientResult<Self> {
        let service_id = service_id.into();
        let instance_id = instance_id.into();
        let token = token.into();
        let connection_key = format!("{}:{}", service_id, instance_id);

        let mut attempt = 0usize;
        loop {
            let stream = match TcpStream::connect((host, port)).await {
                Ok(stream) => stream,
                Err(err) => {
                    warn!(%connection_key, %err, "TCP connection failed");
                    if attempt >= CONNECT_RETRY_MAX_ATTEMPTS - 1 {
                        error!(%connection_key, "Giving up after connection retries");
                        return Err(err.into());
                    }
                    let delay = connect_retry_delay(attempt);
                    info!(%connection_key, ?delay, "Connection retry scheduled");
                    tokio::time::sleep(delay).await;
                    attempt += 1;
                    continue;
                }
            };

            let _ = stream.set_nodelay(true);
            let (reader, writer) = stream.into_split();

            let inner = Arc::new(Inner {
                service_id: service_id.clone(),
                instance_id: instance_id.clone(),
                writer: Mutex::new(writer),
                pending: Mutex::new(VecDeque::new()),
                event_handlers: RwLock::new(HashMap::new()),
                request_handlers: RwLock::new(HashMap::new()),
            });

            tokio::spawn(read_loop(Arc::clone(&inner), reader));

            info!(%connection_key, "TCP socket established; sending connect handshake");

            let client = Self {
                inner: Arc::clone(&inner),
            };

            let connect_result = client
                .send_command(ServiceBusProtocolMessage {
                    r#type: protocol_types::CONNECT.to_string(),
                    service_id: Some(service_id.clone()),
                    instance_id: Some(instance_id.clone()),
                    token: Some(token.clone()),
                    ..Default::default()
                })
                .await;

            if let Err(err) = connect_result {
                warn!(%connection_key, %err, "Connect handshake failed");
                if attempt >= CONNECT_RETRY_MAX_ATTEMPTS - 1 {
                    return Err(err);
                }
                let delay = connect_retry_delay(attempt);
                info!(%connection_key, ?delay, "Connection retry scheduled");
                tokio::time::sleep(delay).await;
                attempt += 1;
                continue;
            }

            info!(%connection_key, "Authenticated and connected");

            start_metrics_publisher(client.clone());
            return Ok(client);
        }
    }

    /// Subscribes to one or more topics and waits for an `ack`.
    pub async fn subscribe(&self, topics: Vec<String>) -> ClientResult<()> {
        self.send_command(ServiceBusProtocolMessage {
            r#type: protocol_types::SUBSCRIBE.to_string(),
            topics: Some(topics),
            ..Default::default()
        })
        .await
        .map(|_| ())
    }

    /// Unsubscribes from topics without waiting for a response frame.
    pub async fn unsubscribe(&self, topics: Vec<String>) -> ClientResult<()> {
        self.send_fire_and_forget(ServiceBusProtocolMessage {
            r#type: protocol_types::UNSUBSCRIBE.to_string(),
            topics: Some(topics),
            ..Default::default()
        })
        .await
    }

    /// Publishes a topic + message-type payload and returns subscriber count.
    pub async fn publish(
        &self,
        topic: impl Into<String>,
        message_type: impl Into<String>,
        payload: HashMap<String, serde_json::Value>,
    ) -> ClientResult<i32> {
        let event = ServiceBusEventEnvelope {
            event_id: String::new(),
            emitted_at_utc: Utc::now(),
            service_id: String::new(),
            instance_id: String::new(),
            topic: topic.into(),
            message_type: message_type.into(),
            correlation_id: 0,
            causation_id: None,
            payload,
        };

        self.publish_event(event).await
    }

    /// Publishes a fully formed event envelope and returns the subscriber count.
    pub async fn publish_event(&self, event: ServiceBusEventEnvelope) -> ClientResult<i32> {
        let msg = self
            .send_command(ServiceBusProtocolMessage {
                r#type: protocol_types::PUBLISH.to_string(),
                event: Some(event),
                ..Default::default()
            })
            .await?;

        Ok(msg.subscriber_count.unwrap_or_default())
    }

    /// Sends a forward request and validates a `forward_response` frame.
    pub async fn forward(
        &self,
        request: ForwardRequest,
    ) -> ClientResult<ServiceBusForwardResponse> {
        let response = self
            .send_command_expect(ServiceBusProtocolMessage {
                r#type: protocol_types::FORWARD.to_string(),
                forward_request: Some(ServiceBusForwardRequest {
                    target_service_id: request.target_service_id,
                    message_type: request.message_type,
                    payload: request.payload,
                    timeout_ms: request.timeout_ms,
                }),
                ..Default::default()
            })
            .await?;

        if response.r#type != protocol_types::FORWARD_RESPONSE {
            return Err(ClientError::UnexpectedMessage(response.r#type));
        }

        response
            .forward_response
            .ok_or(ClientError::MissingField("forwardResponse"))
    }

    /// Registers an async event handler for a topic and subscribes automatically.
    pub async fn on_event<F, Fut>(&self, topic: impl Into<String>, handler: F) -> ClientResult<()>
    where
        F: Fn(ServiceBusEventEnvelope) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let topic = topic.into();
        self.subscribe(vec![topic.clone()]).await?;

        let boxed: EventHandler = Arc::new(move |event| handler(event).boxed());
        let mut guard = self.inner.event_handlers.write().await;
        guard.entry(topic).or_default().push(boxed);
        Ok(())
    }

    /// Registers an async request responder by message type.
    pub async fn on_request<F, Fut>(
        &self,
        topic: impl Into<String>,
        responder: F,
    ) -> ClientResult<()>
    where
        F: Fn(ServiceBusRequest, RequestResponder) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ClientResult<()>> + Send + 'static,
    {
        let topic = topic.into();
        let service_topic = format!("service-{}", self.inner.service_id);
        self.subscribe(vec![service_topic]).await?;

        let handler: RequestHandler = Arc::new(move |req, resp| responder(req, resp).boxed());
        let mut guard = self.inner.request_handlers.write().await;
        guard.insert(topic, handler);
        Ok(())
    }

    async fn send_command(
        &self,
        msg: ServiceBusProtocolMessage,
    ) -> ClientResult<ServiceBusProtocolMessage> {
        let response = self.send_command_expect(msg).await?;
        if response.r#type != protocol_types::ACK {
            return Err(ClientError::UnexpectedMessage(response.r#type));
        }
        Ok(response)
    }

    async fn send_command_expect(
        &self,
        msg: ServiceBusProtocolMessage,
    ) -> ClientResult<ServiceBusProtocolMessage> {
        let (tx, rx) = oneshot::channel();
        {
            let mut pending = self.inner.pending.lock().await;
            pending.push_back(tx);
        }

        let mut wire = serde_json::to_string(&msg)?;
        wire.push('\n');
        let mut writer = self.inner.writer.lock().await;
        if let Err(err) = writer.write_all(wire.as_bytes()).await {
            let mut pending = self.inner.pending.lock().await;
            let _ = pending.pop_back();
            return Err(err.into());
        }
        writer.flush().await?;

        let response = timeout(COMMAND_RESPONSE_TIMEOUT, rx)
            .await
            .map_err(|_| ClientError::Protocol {
                code: "COMMAND_TIMEOUT".to_string(),
                message: "Timed out waiting for protocol response".to_string(),
            })?
            .map_err(|_| ClientError::ChannelClosed)?;
        if response.r#type == protocol_types::ERROR {
            return Err(ClientError::Protocol {
                code: response
                    .error_code
                    .unwrap_or_else(|| "UNKNOWN_ERROR".to_string()),
                message: response
                    .message
                    .unwrap_or_else(|| "Service bus protocol error".to_string()),
            });
        }

        Ok(response)
    }

    async fn send_fire_and_forget(&self, msg: ServiceBusProtocolMessage) -> ClientResult<()> {
        let mut wire = serde_json::to_string(&msg)?;
        wire.push('\n');
        let mut writer = self.inner.writer.lock().await;
        writer.write_all(wire.as_bytes()).await?;
        writer.flush().await?;
        Ok(())
    }
}

fn start_metrics_publisher(client: BusClient) {
    let weak_inner = Arc::downgrade(&client.inner);
    tokio::spawn(async move {
        let mut ticker = tokio::time::interval(METRICS_INTERVAL);
        loop {
            ticker.tick().await;
            let Some(inner) = weak_inner.upgrade() else {
                debug!("Metrics task stopping because client was dropped");
                break;
            };

            let metrics_client = BusClient { inner };
            let connection_key = format!(
                "{}:{}",
                metrics_client.inner.service_id, metrics_client.inner.instance_id
            );
            match metrics_client
                .publish(METRICS_TOPIC, METRICS_MESSAGE_TYPE, build_metrics_payload())
                .await
            {
                Ok(subscribers) => {
                    debug!(%connection_key, subscribers, "Metric published");
                }
                Err(err) => {
                    warn!(%connection_key, %err, "Metric publish failed");
                }
            }
        }
    });
}

fn build_metrics_payload() -> HashMap<String, serde_json::Value> {
    let mut payload = HashMap::new();
    payload.insert(
        "name".to_string(),
        serde_json::Value::String("memory_usage".to_string()),
    );
    payload.insert(
        "value".to_string(),
        serde_json::Value::from(current_memory_usage_bytes()),
    );
    payload.insert(
        "unit".to_string(),
        serde_json::Value::String("bytes".to_string()),
    );
    payload
}

fn current_memory_usage_bytes() -> u64 {
    #[cfg(target_os = "linux")]
    {
        if let Ok(status) = std::fs::read_to_string("/proc/self/statm")
            && let Some(pages_str) = status.split_whitespace().next()
            && let Ok(pages) = pages_str.parse::<u64>()
        {
            return pages.saturating_mul(4096);
        }
    }

    0
}

fn connect_retry_delay(attempt: usize) -> Duration {
    let exp_factor = 1u128 << attempt.min(16);
    let base_ms = CONNECT_RETRY_BASE_DELAY
        .as_millis()
        .saturating_mul(exp_factor);
    let capped_ms = base_ms.min(CONNECT_RETRY_MAX_DELAY.as_millis()) as u64;
    let jitter = (SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.subsec_nanos() as u64)
        .unwrap_or(0))
        % (CONNECT_RETRY_MAX_JITTER_MS + 1);
    Duration::from_millis(capped_ms.saturating_add(jitter))
}

async fn read_loop(inner: Arc<Inner>, reader: OwnedReadHalf) {
    let mut reader = BufReader::new(reader);
    let mut line = String::new();
    let connection_key = format!("{}:{}", inner.service_id, inner.instance_id);

    debug!(%connection_key, "Read loop started");

    loop {
        line.clear();
        let bytes = match reader.read_line(&mut line).await {
            Ok(size) => size,
            Err(err) => {
                warn!(%connection_key, %err, "Read error");
                break;
            }
        };
        if bytes == 0 {
            info!(%connection_key, "Socket closed by peer");
            break;
        }

        let message: ServiceBusProtocolMessage = match serde_json::from_str(&line) {
            Ok(msg) => msg,
            Err(err) => {
                warn!(%connection_key, %err, "Failed to parse message");
                continue;
            }
        };

        match message.r#type.as_str() {
            protocol_types::EVENT => {
                if let Some(event) = message.event
                    && event.instance_id == inner.instance_id
                {
                    dispatch_event(Arc::clone(&inner), event).await;
                }
            }
            protocol_types::ACK | protocol_types::ERROR | protocol_types::FORWARD_RESPONSE => {
                let sender = {
                    let mut pending = inner.pending.lock().await;
                    pending.pop_front()
                };
                if let Some(sender) = sender {
                    let _ = sender.send(message);
                }
            }
            _ => {}
        }
    }

    debug!(%connection_key, "Read loop stopped");
}

async fn dispatch_event(inner: Arc<Inner>, event: ServiceBusEventEnvelope) {
    let handlers = {
        let guard = inner.event_handlers.read().await;
        let mut collected: Vec<EventHandler> = guard.get(&event.topic).cloned().unwrap_or_default();
        if let Some(wildcard) = guard.get("*") {
            collected.extend(wildcard.iter().cloned());
        }
        collected
    };

    for handler in handlers {
        let event_clone = event.clone();
        tokio::spawn(async move {
            handler(event_clone).await;
        });
    }

    if event.topic == format!("service-{}", inner.service_id) {
        let request_handler = {
            let guard = inner.request_handlers.read().await;
            guard.get(&event.message_type).cloned()
        };

        if let Some(handler) = request_handler
            && let Some(reply_to) = event.payload.get("reply_to").and_then(|v| v.as_str())
        {
            let req = ServiceBusRequest {
                event: event.clone(),
            };
            let responder = RequestResponder {
                client: BusClient {
                    inner: Arc::clone(&inner),
                },
                reply_to_topic: reply_to.to_string(),
                causation_id: event.event_id.clone(),
                correlation_id: event.correlation_id,
                default_message_type: event.message_type.clone(),
            };

            tokio::spawn(async move {
                let _ = handler(req, responder).await;
            });
        }
    }
}