reinhardt-core 0.1.1

Core components for Reinhardt framework
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
#![cfg(native)]

//! Distributed Signals - Cross-service signal dispatch via message brokers
//!
//! This module provides distributed signal support, allowing signals to be
//! dispatched across multiple service instances via message brokers like
//! Redis Pub/Sub, RabbitMQ, or Kafka.
//!
//! # Examples
//!
//! ```rust,no_run
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
//!
//! // Create a distributed signal with an in-memory broker
//! let broker = InMemoryBroker::new();
//! let signal = DistributedSignal::<serde_json::Value, _>::new("user_created", broker, "service-1");
//!
//! // Subscribe to signals from other services
//! signal.subscribe(|event| {
//!     println!("Received distributed signal: {:?}", event);
//!     Ok(())
//! }).await?;
//!
//! // Publish signals to other services
//! let user_event = serde_json::json!({"user_id": 123});
//! signal.publish(user_event).await?;
//! # Ok(())
//! # }

use super::error::SignalError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Distributed signal event wrapper
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::distributed::DistributedEvent;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, Clone)]
/// struct UserEvent { user_id: i64 }
///
/// let event = DistributedEvent::new(
///     "user_created",
///     UserEvent { user_id: 123 },
///     "service-1"
/// );
/// assert_eq!(event.signal_name, "user_created");
/// assert_eq!(event.source_service, "service-1");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedEvent<T> {
	/// Signal name
	pub signal_name: String,
	/// Event payload
	pub payload: T,
	/// Source service identifier
	pub source_service: String,
	/// Event timestamp (Unix timestamp in milliseconds)
	pub timestamp: u64,
	/// Unique event ID
	pub event_id: String,
}

impl<T> DistributedEvent<T> {
	/// Create a new distributed event
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::distributed::DistributedEvent;
	///
	/// let event = DistributedEvent::new("test_signal", "payload", "service-1");
	/// assert_eq!(event.signal_name, "test_signal");
	/// ```
	pub fn new(
		signal_name: impl Into<String>,
		payload: T,
		source_service: impl Into<String>,
	) -> Self {
		use std::time::{SystemTime, UNIX_EPOCH};

		let timestamp = SystemTime::now()
			.duration_since(UNIX_EPOCH)
			.unwrap_or_default()
			.as_millis() as u64;

		Self {
			signal_name: signal_name.into(),
			payload,
			source_service: source_service.into(),
			timestamp,
			event_id: uuid::Uuid::now_v7().to_string(),
		}
	}
}

/// Message broker trait for distributed signals
///
/// Implement this trait to support different message brokers
#[async_trait]
pub trait MessageBroker: Send + Sync {
	/// Publish a message to a channel
	async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), SignalError>;

	/// Subscribe to a channel
	async fn subscribe(
		&self,
		channel: &str,
		handler: Arc<dyn Fn(Vec<u8>) -> Result<(), SignalError> + Send + Sync>,
	) -> Result<(), SignalError>;

	/// Unsubscribe from a channel
	async fn unsubscribe(&self, channel: &str) -> Result<(), SignalError>;
}

/// In-memory message broker for testing and local development
///
/// Type alias for channel subscriber function
type SubscriberFn = Arc<dyn Fn(Vec<u8>) -> Result<(), SignalError> + Send + Sync>;

/// Type alias for channels map
type ChannelsMap = std::collections::HashMap<String, Vec<SubscriberFn>>;

/// # Examples
///
/// ```
/// use reinhardt_core::signals::distributed::InMemoryBroker;
///
/// let broker = InMemoryBroker::new();
/// ```
pub struct InMemoryBroker {
	channels: Arc<RwLock<ChannelsMap>>,
}

impl InMemoryBroker {
	/// Create a new in-memory broker
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::distributed::InMemoryBroker;
	///
	/// let broker = InMemoryBroker::new();
	/// ```
	pub fn new() -> Self {
		Self {
			channels: Arc::new(RwLock::new(std::collections::HashMap::new())),
		}
	}
}

impl Default for InMemoryBroker {
	fn default() -> Self {
		Self::new()
	}
}

#[async_trait]
impl MessageBroker for InMemoryBroker {
	async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), SignalError> {
		let channels = self.channels.read().await;
		if let Some(handlers) = channels.get(channel) {
			for handler in handlers {
				handler(message.to_vec())?;
			}
		}
		Ok(())
	}

	async fn subscribe(
		&self,
		channel: &str,
		handler: Arc<dyn Fn(Vec<u8>) -> Result<(), SignalError> + Send + Sync>,
	) -> Result<(), SignalError> {
		let mut channels = self.channels.write().await;
		channels
			.entry(channel.to_string())
			.or_insert_with(Vec::new)
			.push(handler);
		Ok(())
	}

	async fn unsubscribe(&self, channel: &str) -> Result<(), SignalError> {
		let mut channels = self.channels.write().await;
		channels.remove(channel);
		Ok(())
	}
}

/// Distributed signal with message broker integration
///
/// # Examples
///
/// ```
/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
///
/// # async fn example() {
/// let broker = InMemoryBroker::new();
/// let signal = DistributedSignal::<String, _>::new("my_signal", broker, "service-1");
/// # }
/// ```
pub struct DistributedSignal<T, B>
where
	T: Serialize + DeserializeOwned + Send + Sync + 'static,
	B: MessageBroker + 'static,
{
	signal_name: String,
	broker: Arc<B>,
	service_id: String,
	_phantom: PhantomData<T>,
}

impl<T, B> DistributedSignal<T, B>
where
	T: Serialize + DeserializeOwned + Send + Sync + 'static,
	B: MessageBroker + 'static,
{
	/// Create a new distributed signal
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::<String, _>::new("test", broker, "svc-1");
	/// ```
	pub fn new(signal_name: impl Into<String>, broker: B, service_id: impl Into<String>) -> Self {
		Self {
			signal_name: signal_name.into(),
			broker: Arc::new(broker),
			service_id: service_id.into(),
			_phantom: PhantomData,
		}
	}

	/// Get the signal name
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::<String, _>::new("my_signal", broker, "svc-1");
	/// assert_eq!(signal.name(), "my_signal");
	/// ```
	pub fn name(&self) -> &str {
		&self.signal_name
	}

	/// Get the service ID
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::<String, _>::new("test", broker, "service-1");
	/// assert_eq!(signal.service_id(), "service-1");
	/// ```
	pub fn service_id(&self) -> &str {
		&self.service_id
	}

	/// Publish an event to the distributed signal
	///
	/// # Examples
	///
	/// ```rust,no_run
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// # #[tokio::main]
	/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::new("user_created", broker, "api-service");
	///
	/// signal.publish("User data".to_string()).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn publish(&self, payload: T) -> Result<(), SignalError> {
		let event = DistributedEvent::new(&self.signal_name, payload, &self.service_id);
		let message = serde_json::to_vec(&event)
			.map_err(|e| SignalError::new(format!("Serialization error: {}", e)))?;

		self.broker.publish(&self.signal_name, &message).await
	}

	/// Subscribe to distributed signal events
	///
	/// # Examples
	///
	/// ```rust,no_run
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// # #[tokio::main]
	/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::<String, _>::new("user_created", broker, "worker-service");
	///
	/// signal.subscribe(|event| {
	///     println!("Received: {:?}", event);
	///     Ok(())
	/// }).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn subscribe<F>(&self, handler: F) -> Result<(), SignalError>
	where
		F: Fn(DistributedEvent<T>) -> Result<(), SignalError> + Send + Sync + 'static,
	{
		let handler = Arc::new(handler);
		let wrapped_handler = Arc::new(move |message: Vec<u8>| {
			let event: DistributedEvent<T> = serde_json::from_slice(&message)
				.map_err(|e| SignalError::new(format!("Deserialization error: {}", e)))?;
			handler(event)
		});

		self.broker
			.subscribe(&self.signal_name, wrapped_handler)
			.await
	}

	/// Unsubscribe from distributed signal events
	///
	/// # Examples
	///
	/// ```rust,no_run
	/// use reinhardt_core::signals::distributed::{DistributedSignal, InMemoryBroker};
	///
	/// # #[tokio::main]
	/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
	/// let broker = InMemoryBroker::new();
	/// let signal = DistributedSignal::<String, _>::new("user_created", broker, "worker-service");
	///
	/// signal.unsubscribe().await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn unsubscribe(&self) -> Result<(), SignalError> {
		self.broker.unsubscribe(&self.signal_name).await
	}
}

impl<T, B> Clone for DistributedSignal<T, B>
where
	T: Serialize + DeserializeOwned + Send + Sync + 'static,
	B: MessageBroker + 'static,
{
	fn clone(&self) -> Self {
		Self {
			signal_name: self.signal_name.clone(),
			broker: Arc::clone(&self.broker),
			service_id: self.service_id.clone(),
			_phantom: PhantomData,
		}
	}
}

impl<T, B> fmt::Debug for DistributedSignal<T, B>
where
	T: Serialize + DeserializeOwned + Send + Sync + 'static,
	B: MessageBroker + 'static,
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("DistributedSignal")
			.field("signal_name", &self.signal_name)
			.field("service_id", &self.service_id)
			.finish()
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use parking_lot::Mutex;
	use std::sync::Arc;
	use std::sync::atomic::{AtomicUsize, Ordering};

	#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
	struct TestPayload {
		message: String,
		count: i32,
	}

	#[test]
	fn test_distributed_event_creation() {
		let event = DistributedEvent::new("test_signal", "test_payload", "service-1");
		assert_eq!(event.signal_name, "test_signal");
		assert_eq!(event.payload, "test_payload");
		assert_eq!(event.source_service, "service-1");
		assert!(event.timestamp > 0);
		assert!(!event.event_id.is_empty());
	}

	#[tokio::test]
	async fn test_in_memory_broker() {
		let broker = InMemoryBroker::new();
		let received = Arc::new(Mutex::new(Vec::new()));

		let r = received.clone();
		let handler = Arc::new(move |msg: Vec<u8>| {
			r.lock().push(msg);
			Ok(())
		});

		broker.subscribe("test_channel", handler).await.unwrap();
		broker.publish("test_channel", b"hello").await.unwrap();

		let messages = received.lock();
		assert_eq!(messages.len(), 1);
		assert_eq!(messages[0], b"hello");
	}

	#[tokio::test]
	async fn test_distributed_signal_publish_subscribe() {
		let broker = InMemoryBroker::new();
		let signal = DistributedSignal::new("user_event", broker, "service-1");

		let received = Arc::new(Mutex::new(Vec::new()));
		let r = received.clone();

		signal
			.subscribe(move |event| {
				r.lock().push(event.payload);
				Ok(())
			})
			.await
			.unwrap();

		let payload = TestPayload {
			message: "Hello".to_string(),
			count: 42,
		};

		signal.publish(payload.clone()).await.unwrap();

		let events = received.lock();
		assert_eq!(events.len(), 1);
		assert_eq!(events[0], payload);
	}

	#[tokio::test]
	async fn test_distributed_signal_multiple_subscribers() {
		let broker = InMemoryBroker::new();
		let signal = DistributedSignal::new("broadcast", broker, "svc-1");

		let counter = Arc::new(AtomicUsize::new(0));

		let c1 = counter.clone();
		signal
			.subscribe(move |_event: DistributedEvent<String>| {
				c1.fetch_add(1, Ordering::SeqCst);
				Ok(())
			})
			.await
			.unwrap();

		let c2 = counter.clone();
		signal
			.subscribe(move |_event| {
				c2.fetch_add(1, Ordering::SeqCst);
				Ok(())
			})
			.await
			.unwrap();

		signal.publish("test".to_string()).await.unwrap();

		assert_eq!(counter.load(Ordering::SeqCst), 2);
	}

	#[tokio::test]
	async fn test_distributed_signal_name_and_service_id() {
		let broker = InMemoryBroker::new();
		let signal = DistributedSignal::<String, _>::new("my_signal", broker, "my_service");

		assert_eq!(signal.name(), "my_signal");
		assert_eq!(signal.service_id(), "my_service");
	}

	#[tokio::test]
	async fn test_distributed_signal_unsubscribe() {
		let broker = InMemoryBroker::new();
		let signal = DistributedSignal::new("temp_signal", broker, "svc-1");

		let counter = Arc::new(AtomicUsize::new(0));
		let c = counter.clone();

		signal
			.subscribe(move |_event: DistributedEvent<String>| {
				c.fetch_add(1, Ordering::SeqCst);
				Ok(())
			})
			.await
			.unwrap();

		signal.publish("first".to_string()).await.unwrap();
		assert_eq!(counter.load(Ordering::SeqCst), 1);

		signal.unsubscribe().await.unwrap();
		signal.publish("second".to_string()).await.unwrap();

		// Still 1 because we unsubscribed
		assert_eq!(counter.load(Ordering::SeqCst), 1);
	}

	#[tokio::test]
	async fn test_distributed_event_metadata() {
		let broker = InMemoryBroker::new();
		let signal = DistributedSignal::new("metadata_test", broker, "test-service");

		let received_event = Arc::new(Mutex::new(None));
		let r = received_event.clone();

		signal
			.subscribe(move |event: DistributedEvent<String>| {
				*r.lock() = Some(event);
				Ok(())
			})
			.await
			.unwrap();

		signal.publish("payload".to_string()).await.unwrap();

		let event = received_event.lock().clone().unwrap();
		assert_eq!(event.signal_name, "metadata_test");
		assert_eq!(event.source_service, "test-service");
		assert_eq!(event.payload, "payload");
		assert!(event.timestamp > 0);
		assert!(!event.event_id.is_empty());
	}
}