maiko 0.1.0

Lightweight event-driven actor runtime with topic-based pub/sub for Tokio
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Maiko

<div align="center">

**Event-driven actors for Tokio**

[![Crates.io](https://img.shields.io/crates/v/maiko.svg)](https://crates.io/crates/maiko)
[![Documentation](https://docs.rs/maiko/badge.svg)](https://docs.rs/maiko)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

</div>

---

## What is Maiko?

**Maiko** is a lightweight actor runtime for building event-driven concurrent systems in Rust. Unlike traditional actor frameworks, Maiko actors communicate through **topic-based pub/sub** rather than direct addressing, making them loosely coupled and ideal for stream processing workloads.

### The Problem Maiko Solves

Building complex Tokio applications often leads to **channel spaghetti**:

```rust
// Without Maiko: Manual channel orchestration
let (tx1, rx1) = mpsc::channel(32);
let (tx2, rx2) = mpsc::channel(32);
let (tx3, rx3) = mpsc::channel(32);

let tx1_clone = tx1.clone();
let tx2_clone = tx2.clone();

tokio::spawn(async move {
    // Task A needs to send to B and C...
    tx1_clone.send(data).await?;
    tx2_clone.send(data).await?;
});

tokio::spawn(async move {
    // Task B needs rx1 and tx3...
    while let Some(msg) = rx1.recv().await {
        tx3.send(process(msg)).await?;
    }
});

// ... and it gets worse with more tasks
```

**With Maiko, channels disappear from your code:**

```rust
// Actors just subscribe to topics - Maiko handles all routing
sup.add_actor("task_a", |ctx| TaskA { ctx }, &[Topic::Input])?;
sup.add_actor("task_b", |ctx| TaskB { ctx }, &[Topic::Processed])?;
sup.add_actor("task_c", |ctx| TaskC { ctx }, &[Topic::Input, Topic::Processed])?;

// No manual channel creation, cloning, or wiring needed!
```

Maiko manages the entire channel topology internally, letting you focus on business logic instead of coordination.

### Why "Maiko"?

**Maiko** (舞妓) are traditional Japanese performers known for their coordinated dances and artistic discipline. Like maiko who respond to music and each other in harmony, Maiko actors coordinate through events in the Tokio runtime.

---

## When to Use Maiko

Maiko excels at processing **unidirectional event streams** where actors don't need to know about each other:

- **System event processing** - inotify, epoll, signals, device monitoring
- **Data stream handling** - stock ticks, sensor data, telemetry pipelines
- **Network event processing** - packet handling, protocol parsing
- **Reactive architectures** - event sourcing, CQRS patterns
- **Game engines** - entity systems, event-driven gameplay

### Maiko vs Alternatives

**Feature Comparison:**

| Feature | Maiko | Actix | Ractor | Tokio Channels |
|---------|-------|-------|--------|----------------|
| Pub/Sub Topics |||||
| Actor Addressing |||| N/A |
| Supervision Trees |||| N/A |
| Loose Coupling |||||
| Event Metadata |||||
| Correlation Tracking |||||
| Type-Safe Routing |||||
| Learning Curve | Low | Medium | Low | Low |

**Use Case Guide:**

| Use Case | Best Choice |
|----------|-------------|
| Event streams, pub/sub patterns | **Maiko**|
| Request/reply, actor addressing | Actix, Ractor |
| Supervision trees, fault tolerance | Actix, Ractor |
| Simple async tasks | Tokio tasks |
| Distributed messaging | Kafka, NATS |

---

## Quick Start

Add Maiko to your `Cargo.toml`:

```toml
[dependencies]
maiko = "0.1.0"
tokio = { version = "1", features = ["full"] }
```

### Hello World Example

```rust
use maiko::*;

// Define your events
#[derive(Event, Clone, Debug)]
enum MyEvent {
    Hello(String),
}

// Create an actor
struct Greeter {
    ctx: Context<MyEvent>,
}

impl Actor for Greeter {
    type Event = MyEvent;

    async fn handle(&mut self, event: &Self::Event, meta: &Meta) -> Result<()> {
        match event {
            MyEvent::Hello(name) => {
                println!("Hello, {}! (from {})", name, meta.actor_name());
                Ok(())
            }
        }
    }

    async fn on_start(&mut self) -> Result<()> {
        self.ctx.send(MyEvent::Hello("World".into())).await
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let mut sup = Supervisor::<MyEvent>::default();

    // Add actor with factory function
    sup.add_actor("greeter", |ctx| Greeter { ctx }, &[Broadcast])?;

    // Start the supervisor
    sup.start().await?;

    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    sup.stop().await?;

    Ok(())
}
```

---

## Core Concepts

### 1. Events

Events are messages that flow through the system. They must implement the `Event` trait:

```rust
#[derive(Event, Clone, Debug)]
enum NetworkEvent {
    PacketReceived(Vec<u8>),
    ConnectionClosed(u32),
    Error(String),
}
```

### 2. Topics

Topics route events to interested actors. Define custom topics for fine-grained control:

```rust
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
enum NetworkTopic {
    Ingress,
    Egress,
    Control,
}

impl Topic<NetworkEvent> for NetworkTopic {
    fn from_event(event: &NetworkEvent) -> Self {
        match event {
            NetworkEvent::PacketReceived(_) => NetworkTopic::Ingress,
            NetworkEvent::ConnectionClosed(_) => NetworkTopic::Control,
            NetworkEvent::Error(_) => NetworkTopic::Control,
        }
    }
}
```

Or use `Broadcast` to broadcast to all actors:

```rust
sup.add_actor("processor", factory, &[Broadcast])?;
```

### 3. Actors

Actors are independent units that process events asynchronously:

```rust
struct PacketProcessor {
    ctx: Context<NetworkEvent>,
    stats: PacketStats,
}

impl Actor for PacketProcessor {
    type Event = NetworkEvent;

    async fn on_start(&mut self) -> Result<()> {
        println!("Processor starting...");
        Ok(())
    }

    async fn handle(&mut self, event: &Self::Event, meta: &Meta) -> Result<()> {
        match event {
            NetworkEvent::PacketReceived(data) => {
                self.stats.increment();
                self.process_packet(data).await?;
            }
            _ => {}
        }
        Ok(())
    }

    async fn tick(&mut self) -> Result<()> {
        // Called periodically when no events are queued
        // Useful for polling external sources, timeouts, housekeeping, etc.
        if self.stats.should_report() {
            println!("Processed {} packets", self.stats.count);
        }
        Ok(())
    }

    async fn on_stop(&mut self) -> Result<()> {
        println!("Final count: {}", self.stats.count);
        Ok(())
    }
}
```

### 4. Context

The `Context` provides actors with capabilities:

```rust
// Send events to topics
ctx.send(NetworkEvent::PacketReceived(data)).await?;

// Send with correlation (for tracking related events)
ctx.send_child_event(NetworkEvent::Response(data)).await?;

// Check if system is still running
if !ctx.is_alive() {
    return Ok(());
}

// Get actor's name
let name = ctx.name();
```

### 5. Actor Patterns: Handle vs Tick

Maiko actors typically follow one of two patterns:

**Handle-Heavy Actors** (Event Processors):
```rust
// Telemetry actor - processes many incoming events
impl Actor for TelemetryCollector {
    type Event = MetricEvent;

    async fn handle(&mut self, event: &Self::Event, _meta: &Meta) -> Result<()> {
        // Main logic here - process incoming metrics
        self.export_to_otel(event).await?;
        self.aggregate(event);
        Ok(())
    }

    async fn tick(&mut self) -> Result<()> {
        // Minimal - just periodic cleanup
        self.flush_buffer().await
    }
}
```

**Tick-Heavy Actors** (Event Producers):
```rust
// Stock data reader - polls external source, emits many events
impl Actor for StockReader {
    type Event = StockEvent;

    async fn handle(&mut self, event: &Self::Event, _meta: &Meta) -> Result<()> {
        // Rarely receives events - maybe just control messages
        if let StockEvent::Stop = event {
            self.websocket.close().await?;
        }
        Ok(())
    }

    async fn tick(&mut self) -> Result<()> {
        // Main logic here - poll WebSocket and emit events
        if let Some(tick) = self.websocket.next().await {
            let event = StockEvent::Tick(tick.symbol, tick.price);
            self.ctx.send(event).await?;
        }
        Ok(())
    }
}

// System event monitor - polls inotify/epoll
impl Actor for SystemMonitor {
    type Event = SystemEvent;

    async fn tick(&mut self) -> Result<()> {
        // Poll for file system events
        for event in self.inotify.read_events()? {
            self.ctx.send(SystemEvent::FileChanged(event.path)).await?;
        }
        Ok(())
    }
}
```

The `tick()` method runs when the event queue is empty, making it perfect for:
- **Polling external sources** (WebSockets, file descriptors, system APIs)
- **Periodic tasks** (metrics reporting, health checks)
- **Timeout logic** (detecting stale connections)
- **Housekeeping** (buffer flushing, cache cleanup)

### 6. Supervisor

The `Supervisor` manages actor lifecycles:

```rust
let mut sup = Supervisor::<NetworkEvent>::new();

// Add actors with subscriptions
sup.add_actor("ingress", |ctx| IngressActor::new(ctx), &[NetworkTopic::Ingress])?;
sup.add_actor("egress", |ctx| EgressActor::new(ctx), &[NetworkTopic::Egress])?;
sup.add_actor("monitor", |ctx| MonitorActor::new(ctx), &[Broadcast])?;

// Start all actors
sup.start().await?;

// ... application runs ...

// Graceful shutdown
sup.stop().await?;
```

---

## Design Philosophy

### Loose Coupling Through Topics

Maiko actors **don't know about each other**. They only know about:
- Events they can send
- Topics they subscribe to

This is fundamentally different from Akka/Actix where actors have addresses:

```rust
// ❌ Traditional actors (tight coupling)
actor_ref.tell(message);  // Must know the actor's address

// ✅ Maiko (loose coupling)
ctx.send(event).await?;   // Only knows about event types
```

### Unidirectional Flow

Events typically flow in one direction:

```
System Event → Parser → Validator → Processor → Logger
```

This makes Maiko ideal for **pipeline architectures** and **stream processing**.

### Type Safety

Event types are checked at compile time. Invalid event routing is impossible:

```rust
// Compile error - wrong event type!
ctx.send(DifferentEvent::Foo).await?;
```

---

## Advanced Features

### Correlation IDs

Track related events across actors:

```rust
async fn handle(&mut self, event: &Self::Event, meta: &Meta) -> Result<()> {
    if let Some(correlation_id) = meta.correlation_id() {
        println!("Event chain: {}", correlation_id);
    }

    // Child events inherit correlation
    self.ctx.send_child_event(ResponseEvent::Ok).await?;

    Ok(())
}
```

### Error Handling

Control error propagation:

```rust
impl Actor for MyActor {
    // ...

    fn on_error(&self, error: Error) -> Result<()> {
        match error {
            Error::Recoverable(_) => {
                eprintln!("Warning: {}", error);
                Ok(())  // Swallow error, continue
            }
            Error::Fatal(_) => {
                eprintln!("Fatal: {}", error);
                Err(error)  // Propagate error, stop actor
            }
        }
    }
}
```

### Custom Configuration

Fine-tune actor behavior:

```rust
let config = Config::default()
    .with_channel_size(100)           // Event queue size per actor
    .with_max_events_per_tick(50);    // Events processed per tick cycle

let mut sup = Supervisor::new(config);
```

---

## Examples

See the [`examples/`](maiko/examples/) directory for complete programs:

- **[pingpong.rs]maiko/examples/pingpong.rs** - Simple event exchange between actors
- **[guesser.rs]maiko/examples/guesser.rs** - Multi-actor game with topics and timing

Run examples with:

```bash
cargo run --example pingpong
cargo run --example guesser
```

---

## Roadmap

### v0.1.0 (Current - Single Process)
- Topic-based event routing
- Async actor lifecycle hooks
- Graceful shutdown via cancellation tokens
- Correlation ID tracking
- Flexible error handling

### v0.2.0 (Supervision & Control)
- Actor restart policies and strategies
- Supervisor metrics and monitoring
- Dynamic actor spawning at runtime
- Backpressure configuration
- Enhanced error recovery

### v0.3.0 (Cross-Process Communication)
- IPC bridge actors (Unix sockets, TCP)
- Event serialization framework (bincode, JSON, protobuf)
- Remote topic subscriptions
- Multi-supervisor coordination
- Process-level fault isolation

### v0.4.0+ (Ready-to-Use Actor Library)
- **Inter-supervisor communication** - Unix socket, gRPC bridge actors
- **Networking actors** - HTTP client/server, WebSocket, TCP/UDP handlers
- **Telemetry actors** - OpenTelemetry integration, metrics exporters
- **Storage actors** - Database connectors, file watchers, cache adapters
- Authentication and encryption for network bridges

---

## Architecture

```
┌─────────────────────────────────────────────────────┐
│                    Supervisor                       │
│  ┌────────────────────────────────────────────────┐ │
│  │              Central Broker                    │ │
│  │  (Topic-based routing & filtering)             │ │
│  └────────────────────────────────────────────────┘ │
│         │              │              │             │
│    ┌────▼───┐     ┌────▼───┐     ┌────▼───┐         │
│    │Actor A │     │Actor B │     │Actor C │         │
│    │        │     │        │     │        │         │
│    │Topics: │     │Topics: │     │Topics: │         │
│    │  [T1]  │     │[T1,T2] │     │  [T2]  │         │
│    └────┬───┘     └───┬────┘     └───┬────┘         │
│         │             │              │              │
│         └─────────────┴──────────────┘              │
│              Events flow up to broker               │
└─────────────────────────────────────────────────────┘
```

**Key Components:**

- **Supervisor** - Manages actor lifecycles and shutdown
- **Broker** - Routes events to subscribed actors based on topics
- **Actors** - Independent event processors with isolated state
- **Context** - Actor's interface to send events and check system state

---

## Contributing

Contributions are welcome! Please feel free to:

- Report bugs via [GitHub Issues]https://github.com/ddrcode/maiko/issues
- Suggest features and improvements
- Improve documentation
- Submit pull requests

### Code Philosophy

Maiko is **100% human-written code**, crafted with passion for Rust and genuine love for coding. While AI tools have been valuable for architectural discussions, code reviews, and documentation, every line of implementation code comes from human creativity and expertise.

We believe in:
- **Thoughtful design** over automated generation
- **Deep understanding** of the code we write
- **Human craftsmanship** in software engineering

Contributors are expected to write their own code. AI may assist with reviews, discussions, and documentation, but implementations should reflect your own understanding and skills.

---

## License

Licensed under the [MIT License](LICENSE).

---

## Acknowledgments

Inspired by:
- **Kafka** - Topic-based event streaming
- **Akka Streams** - Reactive stream processing
- **Tokio** - Async runtime foundation

Built with ❤️ and by humans, for humans 🦀