|
| 1 | +# Overview |
| 2 | +Implement Erlang-style named process registry and scheduled messaging to make joerl more feature-complete for Erlang developers. This includes: |
| 3 | +* `whereis/1` - Look up processes by registered name |
| 4 | +* `register/2` - Register a process with a name |
| 5 | +* `unregister/1` - Unregister a named process |
| 6 | +* `send/2` - Send to named process or Pid |
| 7 | +* `send_after/3` - Schedule a message to be sent after a delay |
| 8 | +# Current State |
| 9 | +joerl currently only supports addressing actors by Pid. The ActorSystem maintains a DashMap registry mapping Pid to ActorEntry (joerl/src/system.rs:279). Messages are sent via `ActorSystem::send()` and `ActorRef::send()` methods which only accept Pid as the recipient. |
| 10 | +There is no timer/scheduler subsystem, and all message sending is immediate. |
| 11 | +# Proposed Changes |
| 12 | +## 1. Create Named Registry Module |
| 13 | +Create joerl/src/registry.rs for name registration functionality. |
| 14 | +### Registry Structure |
| 15 | +* Use `DashMap<String, Pid>` for thread-safe name-to-Pid mapping |
| 16 | +* Use `DashMap<Pid, String>` for reverse lookup (Pid-to-name) |
| 17 | +* Support both local and distributed names (node-scoped) |
| 18 | +* Automatic cleanup when actors terminate |
| 19 | +### Key Types |
| 20 | +* `RegisteredName` - Newtype wrapper for registered names with validation |
| 21 | +* `RegistrationError` - Specific errors for name conflicts, invalid names |
| 22 | +### Core Functions |
| 23 | +* `register(name: String, pid: Pid) -> Result<()>` - Register name (error if already registered) |
| 24 | +* `unregister(name: &str) -> Result<Pid>` - Remove registration, return Pid |
| 25 | +* `whereis(name: &str) -> Option<Pid>` - Look up Pid by name |
| 26 | +* `registered() -> Vec<String>` - List all registered names |
| 27 | +* `cleanup_pid(pid: Pid)` - Internal cleanup when actor dies |
| 28 | +## 2. Integrate Registry into ActorSystem |
| 29 | +Add registry field to ActorSystem (joerl/src/system.rs:278). |
| 30 | +### Changes to ActorSystem |
| 31 | +* Add `registry: Arc<Registry>` field |
| 32 | +* Initialize registry in `new()` and `new_distributed()` |
| 33 | +* Expose public API methods that delegate to registry: |
| 34 | + * `register(&self, name: impl Into<String>, pid: Pid) -> Result<()>` |
| 35 | + * `unregister(&self, name: &str) -> Result<Pid>` |
| 36 | + * `whereis(&self, name: &str) -> Option<Pid>` |
| 37 | + * `registered(&self) -> Vec<String>` |
| 38 | +* Call `registry.cleanup_pid()` in `cleanup_actor()` (system.rs:717) |
| 39 | +### Named Send Support |
| 40 | +* Create `Destination` enum: `Pid(Pid) | Name(String)` |
| 41 | +* Add `send_to(&self, dest: Destination, msg: Message) -> Result<()>` |
| 42 | +* Modify existing `send()` to work through this abstraction |
| 43 | +* Update ActorRef to support named sends |
| 44 | +## 3. Create Scheduler Module |
| 45 | +Create joerl/src/scheduler.rs for delayed message scheduling. |
| 46 | +### Scheduler Structure |
| 47 | +* Use tokio's time utilities (`tokio::time::sleep`, `Instant`) |
| 48 | +* Store scheduled messages as heap-ordered by delivery time |
| 49 | +* Each scheduled message spawns a tokio task that sleeps then sends |
| 50 | +### Key Types |
| 51 | +* `ScheduledMessage` - Contains destination, message, delivery time |
| 52 | +* `SchedulerHandle` - Cancellable reference to scheduled message |
| 53 | +* `TimerRef` - Erlang-style timer reference for cancellation |
| 54 | +### Core Functions |
| 55 | +* `send_after(dest: Destination, msg: Message, duration: Duration) -> TimerRef` |
| 56 | +* `cancel_timer(timer_ref: TimerRef) -> Result<bool>` - Returns true if cancelled |
| 57 | +* `send_interval(dest: Destination, msg: Message, interval: Duration) -> TimerRef` (bonus) |
| 58 | +### Implementation Strategy |
| 59 | +* Each `send_after` spawns a detached tokio task |
| 60 | +* Task sleeps for duration, then calls `ActorSystem::send_to()` |
| 61 | +* Store cancellation tokens in `DashMap<TimerRef, CancellationToken>` |
| 62 | +* Use `tokio_util::sync::CancellationToken` for cancellation |
| 63 | +## 4. Update Message Types |
| 64 | +Add timer-related message types to joerl/src/message.rs if needed for timer expiry notifications. |
| 65 | +## 5. Extend ActorContext |
| 66 | +Add convenience methods to ActorContext (joerl/src/actor.rs:100): |
| 67 | +* `send_after(&self, dest: Destination, msg: Message, duration: Duration) -> Result<TimerRef>` |
| 68 | +* `whereis(&self, name: &str) -> Option<Pid>` |
| 69 | +* Allow actors to easily schedule messages from within their context |
| 70 | +## 6. Update Error Types |
| 71 | +Add new error variants to ActorError (joerl/src/error.rs): |
| 72 | +* `NameAlreadyRegistered(String)` |
| 73 | +* `NameNotRegistered(String)` |
| 74 | +* `InvalidName(String)` |
| 75 | +* `TimerNotFound(u64)` |
| 76 | +## 7. Telemetry Integration |
| 77 | +Add metrics for registry and scheduler (if telemetry feature enabled): |
| 78 | +* `joerl_registry_size` - Current number of registered names |
| 79 | +* `joerl_registry_lookups_total` - Registry lookup counter |
| 80 | +* `joerl_registry_conflicts_total` - Registration conflict counter |
| 81 | +* `joerl_scheduled_messages_total` - Messages scheduled |
| 82 | +* `joerl_scheduled_messages_cancelled_total` - Timers cancelled |
| 83 | +## 8. Documentation and Examples |
| 84 | +### Update README.md |
| 85 | +Add section on named processes and scheduled messaging with examples. |
| 86 | +### Create Examples |
| 87 | +* `examples/named_processes.rs` - Registry demonstration |
| 88 | +* `examples/scheduled_messaging.rs` - Timer demonstration |
| 89 | +* Update existing examples to show optional use of names |
| 90 | +### Update Erlang Terminology Mapping |
| 91 | +Add to README.md (line 301): |
| 92 | +* `register(Name, Pid)` -> `system.register(name, pid)` |
| 93 | +* `unregister(Name)` -> `system.unregister(name)` |
| 94 | +* `whereis(Name)` -> `system.whereis(name)` |
| 95 | +* `registered()` -> `system.registered()` |
| 96 | +* `erlang:send_after(Time, Pid, Msg)` -> `system.send_after(dest, msg, duration)` |
| 97 | +* `erlang:cancel_timer(TRef)` -> `system.cancel_timer(tref)` |
| 98 | +### Update WARP.md |
| 99 | +Document the new registry and scheduler modules in the Architecture section. |
| 100 | +## 9. Testing |
| 101 | +### Registry Tests |
| 102 | +* Test basic register/unregister/whereis |
| 103 | +* Test name conflicts (should error) |
| 104 | +* Test automatic cleanup on actor death |
| 105 | +* Test concurrent registration attempts |
| 106 | +* Test distributed name registration (node-scoped) |
| 107 | +* Property tests for registry consistency |
| 108 | +### Scheduler Tests |
| 109 | +* Test basic send_after delivery |
| 110 | +* Test timer cancellation before delivery |
| 111 | +* Test message ordering with multiple scheduled messages |
| 112 | +* Test scheduled message to named destination |
| 113 | +* Test scheduled messages survive actor restarts |
| 114 | +* Test timer behaviour when destination dies |
| 115 | +### Integration Tests |
| 116 | +* Named processes with supervision trees |
| 117 | +* Scheduled messages in distributed systems |
| 118 | +* Registry with monitors and links |
| 119 | +## 10. Export Public API |
| 120 | +Update joerl/src/lib.rs: |
| 121 | +* Add `pub mod registry;` |
| 122 | +* Add `pub mod scheduler;` |
| 123 | +* Re-export key types: `RegisteredName`, `Destination`, `TimerRef` |
| 124 | +* Update documentation with new capabilities |
0 commit comments