Skip to content

Conversation

@unbalancedparentheses
Copy link
Contributor

@unbalancedparentheses unbalancedparentheses commented Jan 8, 2026

Summary

This PR adds Supervisor/DynamicSupervisor and unifies the GenServer API with a Backend enum.

GenServer Unification (Latest)

  • Consolidate two separate GenServer implementations (async/tokio and threads) into a single implementation with a Backend enum parameter
  • Breaking change: start() now requires a Backend argument instead of separate start_blocking() and start_on_thread() methods
  • Delete the entire threads module and thread-based examples to reduce code duplication

New API

pub enum Backend {
    Async,    // tokio async tasks (default)
    Blocking, // tokio's blocking thread pool
    Thread,   // dedicated OS thread
}

// Usage:
let handle = server.start(Backend::Async);
let handle = server.start(Backend::Blocking);
let handle = server.start(Backend::Thread);

Supervisor

  • Implement as a GenServer that monitors children
  • Support OneForOne, OneForAll, RestForOne restart strategies
  • Support Permanent, Transient, Temporary restart types
  • Add max_restarts/max_seconds for restart intensity
  • Add ChildSpec with type-erased ChildHandle trait
  • Add StartChild, TerminateChild, RestartChild, DeleteChild operations
  • Add WhichChildren, CountChildren queries
  • Handle DOWN messages to detect child crashes
  • Properly terminate children before restart (OneForAll/RestForOne)

DynamicSupervisor

  • Optimized for many dynamically started children
  • Children indexed by Pid (not string ID)
  • Always uses OneForOne strategy
  • Support max_children limit
  • Full restart intensity enforcement

Tests

  • Add CrashableWorker test GenServer
  • Add 8 Supervisor integration tests
  • Add 4 DynamicSupervisor integration tests
  • All 77 tests pass

Deleted

  • concurrency/src/threads/ module entirely
  • examples/bank_threads/, examples/ping_pong_threads/, examples/updater_threads/

Dependencies

⚠️ Depends on PR #68, #69, #70, #71, #72 - merge those first

Merge Order

PR #68 → PR #69 → PR #70 → PR #71 → PR #72 → PR #73 (this)

🤖 Generated with Claude Code

unbalancedparentheses added 6 commits January 8, 2026 14:09
- Add Pid struct with unique process identifiers (AtomicU64)
- Add HasPid trait for types that have a process ID
- Add ExitReason enum (Normal, Shutdown, Error, Killed)
- Add MonitorRef for tracking monitors
- Add SystemMessage enum (Down, Exit, Timeout)

These are the foundational types for OTP-style process management.
- Add ProcessTable with global process tracking
- Implement bidirectional linking (link/unlink)
- Implement unidirectional monitoring (monitor/demonitor)
- Add trap_exit support for catching linked process exits
- Add SystemMessageSender trait for delivering DOWN/EXIT messages
- Handle exit propagation to linked processes

The process table is the central registry for all running processes
and manages the relationships between them.
- Add global Registry for name -> Pid mapping
- Implement register/unregister functions
- Add whereis for name lookup
- Add name_of for reverse lookup (Pid -> name)
- Prevent duplicate names and multiple names per process
- Add comprehensive tests with mutex for isolation

Enables Erlang-style named processes for easier discovery.
- Add HasPid implementation to GenServerHandle
- Add link/unlink methods for bidirectional process linking
- Add monitor/demonitor for unidirectional monitoring
- Add trap_exit/is_trapping_exit for exit handling
- Add register/unregister for registry integration
- Add handle_info callback for system messages (DOWN, EXIT, Timeout)
- Add teardown callback for cleanup on stop
- Add stop() convenience method
- Add start_linked() and start_monitored() helpers
- Add InitResult enum for init success/failure handling
- Register/unregister with process_table on start/stop
- Use futures::select_biased for message prioritization
- Fix typos in documentation

GenServer now fully integrates with the OTP-style process model.
- Add HasPid implementation to GenServerHandle
- Add link/unlink, monitor/demonitor methods
- Add trap_exit support
- Add register/unregister for registry integration
- Add handle_info callback for system messages
- Add teardown callback
- Add stop() method
- Add start_linked() and start_monitored() helpers
- Add InitResult and InfoResponse types
- Register with process_table on start
- Add Sync bound to message types for consistency
- Update timer_tests to use new InitResult

Threads GenServer now has full parity with tasks version.
Supervisor:
- Implement as a GenServer that monitors children
- Support OneForOne, OneForAll, RestForOne restart strategies
- Support Permanent, Transient, Temporary restart types
- Add max_restarts/max_seconds for restart intensity
- Add ChildSpec with type-erased ChildHandle trait
- Add StartChild, TerminateChild, RestartChild, DeleteChild operations
- Add WhichChildren, CountChildren queries
- Handle DOWN messages to detect child crashes
- Properly terminate children before restart (OneForAll/RestForOne)

DynamicSupervisor:
- Optimized for many dynamically started children
- Children indexed by Pid (not string ID)
- Always uses OneForOne strategy
- Support max_children limit
- Full restart intensity enforcement

Tests:
- Add CrashableWorker test GenServer
- Add 8 Supervisor integration tests
- Add 4 DynamicSupervisor integration tests
- Add supervisor example

Exports all types from lib.rs and tasks/mod.rs.
@unbalancedparentheses unbalancedparentheses changed the title feat: add Supervisor and DynamicSupervisor [6/6] feat: add Supervisor and DynamicSupervisor Jan 8, 2026
Consolidate the two separate GenServer implementations (async/tokio and
threads) into a single implementation with a Backend enum parameter.

Breaking change: start() now requires a Backend argument:
- Backend::Async - tokio async tasks (default)
- Backend::Blocking - tokio's blocking thread pool
- Backend::Thread - dedicated OS thread

This provides runtime flexibility without code duplication, allowing
users to mix different execution backends in the same application.

- Add Backend enum to gen_server.rs
- Remove start_blocking() and start_on_thread() from trait
- Update start_linked() and start_monitored() to accept Backend
- Delete threads module entirely
- Update all examples and tests
- Remove thread-based example crates

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@unbalancedparentheses unbalancedparentheses changed the title [6/6] feat: add Supervisor and DynamicSupervisor feat: add Supervisor/DynamicSupervisor and unify GenServer with Backend enum Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant