2025-11-26 01:51:13 +00:00
|
|
|
# swactor
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Minimal actor runtime for Rust. Single-threaded or multi-threaded, with
|
|
|
|
|
Python and WebAssembly bindings.
|
2026-02-06 14:54:56 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
## Quick Start
|
2026-02-06 11:25:37 +00:00
|
|
|
|
|
|
|
|
```rust
|
2026-02-06 17:12:04 +00:00
|
|
|
use swactor::actor::{ActorAddress, ActorInterface};
|
|
|
|
|
use swactor::runtime::{Ctx, Runtime, RuntimeConfig};
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-06 17:12:04 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct Greet { name: String, reply_to: ActorAddress }
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-06 17:12:04 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct Greeting(String);
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-06 17:12:04 +00:00
|
|
|
struct Greeter;
|
2026-02-06 11:25:37 +00:00
|
|
|
|
|
|
|
|
impl ActorInterface for Greeter {
|
2026-02-06 17:12:04 +00:00
|
|
|
type Incoming = Greet;
|
|
|
|
|
type Response = Greeting;
|
|
|
|
|
|
|
|
|
|
fn handle(&mut self, ctx: &Ctx, msg: Greet) {
|
|
|
|
|
let _ = ctx.send(msg.reply_to, Greeting(format!("Hello, {}!", msg.name)));
|
2026-02-06 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let rt = Runtime::new(RuntimeConfig::default());
|
2026-02-06 17:12:04 +00:00
|
|
|
let addr = rt.spawn(Greeter).unwrap();
|
|
|
|
|
let inbox = rt.new_inbox::<Greeting>().unwrap();
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-06 17:12:04 +00:00
|
|
|
rt.send_to(addr, Greet { name: "world".into(), reply_to: *inbox.addr() }).unwrap();
|
|
|
|
|
rt.tick();
|
|
|
|
|
rt.tick();
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-06 17:12:04 +00:00
|
|
|
println!("{}", inbox.try_recv().unwrap().0); // "Hello, world!"
|
2026-02-06 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
## Features
|
2026-02-06 11:25:37 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
### Actor Model
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Actors implement one trait (`ActorInterface`), receive one message type, and
|
|
|
|
|
hold mutable state. No lifecycle hooks, no supervision trees, no async.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Every actor gets a 32-byte globally unique `ActorAddress`. The same
|
|
|
|
|
`ctx.send(addr, msg)` call works whether the target is on the same worker,
|
|
|
|
|
a different worker thread, an external inbox, or a remote process.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Single-threaded mode (`rt.tick()`) gives deterministic frame-level control.
|
|
|
|
|
Multi-threaded mode (`rt.run()`) spawns OS threads with adaptive backoff.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
See [docs/actor-model.md](docs/actor-model.md) and
|
|
|
|
|
[docs/runtime.md](docs/runtime.md) for the full model.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
### Transport
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Pluggable cross-process messaging. User-provided codecs handle serialization
|
|
|
|
|
(gRPC/protobuf, bincode, hand-rolled — no serde bounds imposed) and
|
|
|
|
|
user-provided transports handle delivery (TCP, in-memory, gRPC channel).
|
2026-02-06 17:12:04 +00:00
|
|
|
|
|
|
|
|
```bash
|
2026-02-09 19:02:36 +00:00
|
|
|
cargo build --features transport
|
|
|
|
|
cargo run --example tcp_ping_pong --features transport -- receiver # terminal 1
|
|
|
|
|
cargo run --example tcp_ping_pong --features transport -- sender # terminal 2
|
2026-02-06 17:12:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
See [docs/transport.md](docs/transport.md) for the routing chain, codec
|
|
|
|
|
registry, and address resolution.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
### Runtime Dashboard
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Live web dashboard for monitoring actors, message throughput, and mailbox
|
|
|
|
|
depths. Supports trace recording and replay at configurable speed.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Includes hand-authored SVG diagrams (actor lifecycle, message lifecycle,
|
|
|
|
|
tick cycle, transport routing) and generated diagrams from DOT sources
|
|
|
|
|
(architecture, dataflow, type erasure).
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
See [crates/runtime-dashboard/](crates/runtime-dashboard/README.md).
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
### Language Bindings
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
**Python** — PyO3 via Maturin. Spawn actors from Python callables, pass
|
|
|
|
|
dicts as messages, single-threaded or multi-threaded.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
```bash
|
|
|
|
|
cd crates/swactor-python && maturin develop
|
2026-02-06 17:12:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Examples in `examples/python/` (single-thread, async, Jupyter notebook).
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
**WASM** — wasm-bindgen. Runs single-threaded with deterministic addressing
|
|
|
|
|
(`no_random` feature).
|
2026-02-06 17:12:04 +00:00
|
|
|
|
|
|
|
|
```bash
|
2026-02-09 19:02:36 +00:00
|
|
|
cd crates/swactor-wasm && wasm-pack build --target nodejs
|
2026-02-06 17:12:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
### Connectome Analysis
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
Structural analysis of the internal dependency graph.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
- **depgraph** (`tools/depgraph/`) — AST-based extraction of module
|
|
|
|
|
dependencies, outputs GraphViz DOT
|
|
|
|
|
- **spectral** (`tools/spectral/`) — Laplacian eigenvalue analysis,
|
|
|
|
|
Connectome Complexity Index (CCI), coupling heatmaps, interactive HTML
|
|
|
|
|
dashboard
|
2026-02-06 17:12:04 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
```bash
|
|
|
|
|
cargo run --manifest-path tools/depgraph/Cargo.toml -- --src-dir src/ --output deps
|
|
|
|
|
python tools/spectral/spectral_analysis.py deps.dot
|
2026-02-06 17:12:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
See [docs/connectome.md](docs/connectome.md) for metric interpretation.
|
2026-02-06 17:12:04 +00:00
|
|
|
|
|
|
|
|
## Building & Testing
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-02-09 19:02:36 +00:00
|
|
|
cargo test # all tests
|
|
|
|
|
cargo test --features transport # include transport tests
|
|
|
|
|
cargo run --example hello # single actor example
|
|
|
|
|
cargo run --example ring # 500-actor ring topology
|
2026-02-06 17:12:04 +00:00
|
|
|
cargo bench # benchmarks (criterion)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Feature Flags
|
|
|
|
|
|
|
|
|
|
| Flag | Default | What it does |
|
|
|
|
|
|------|---------|--------------|
|
|
|
|
|
| `getrandom` | yes | System RNG for actor addresses |
|
2026-02-09 19:02:36 +00:00
|
|
|
| `no_random` | no | Deterministic counter (WASM / reproducible tests) |
|
|
|
|
|
| `transport` | no | Pluggable remote messaging (codec + transport) |
|
|
|
|
|
| `tracing` | no | `tracing` instrumentation for runtime internals |
|
|
|
|
|
| `serde` | no | Serde derives for stats types |
|
|
|
|
|
| `python` | no | PyO3 bindings (cdylib wheel) |
|
2026-02-07 10:36:45 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
## Documentation
|
2026-02-07 10:36:45 +00:00
|
|
|
|
2026-02-09 19:02:36 +00:00
|
|
|
| Document | Covers |
|
|
|
|
|
|----------|--------|
|
|
|
|
|
| [Actor Model](docs/actor-model.md) | Traits, type erasure, addresses |
|
|
|
|
|
| [Runtime](docs/runtime.md) | Runtime, Ctx, Inbox, RuntimeHandle, stats |
|
|
|
|
|
| [Worker Thread](docs/worker-thread.md) | Tick phases, backoff, routing, full system topology |
|
|
|
|
|
| [Channels](docs/channels.md) | HybridChannel, AddressMap, Placement |
|
|
|
|
|
| [Transport](docs/transport.md) | Codec, Transport, remote messaging, address resolution |
|
|
|
|
|
| [Connectome](docs/connectome.md) | CCI metrics, spectral analysis interpretation |
|
|
|
|
|
| [Dashboard](crates/runtime-dashboard/README.md) | Live web UI, trace recording, diagram index |
|