Skip to content

Conversation

@unbalancedparentheses
Copy link

Summary

Remove code duplication in supervisor module by extracting shared functionality.

Changes

RestartIntensityTracker

Extract restart intensity checking logic into a shared struct, eliminating duplication between Supervisor and DynamicSupervisor.

The tracker encapsulates:

  • Recording restart timestamps
  • Pruning old restart times
  • Checking if more restarts are allowed within the intensity window

ChildSpec Helpers

  • Add ChildSpec::new_with_type() helper to remove duplication between worker() and supervisor() constructors
  • Add RestartType::should_restart() method to centralize restart decision logic

File Changes

  • concurrency/src/supervisor.rs - 85 insertions, 56 deletions (net +29 lines)

Depends on: PR #111 (Simplify)

Test plan

  • Existing supervisor tests pass
  • Restart intensity behavior unchanged
  • ChildSpec construction unchanged

🤖 Generated with Claude Code

unbalancedparentheses and others added 8 commits January 9, 2026 20:39
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
- Change start() signature to accept Backend parameter
- Update all examples and tests
- Remove thread-based example crates
Add 10 new tests covering:
- Backend enum traits (Default, Copy, Clone, Debug, PartialEq, Eq)
- All three backends handle call/cast correctly
- Backend::Thread isolates blocking work from async runtime
- Multiple backends can run concurrently with independent state
- Backend::default() works in start()
Document each backend option with:
- Comparison table showing execution model, best use cases, and limitations
- Code examples for each backend
- Detailed "When to Use" guide with advantages and avoid-when advice
- Per-variant documentation with specific use cases
Move files from tasks/ to root and remove threads/ directory:
- concurrency/src/tasks/* → concurrency/src/*
- Delete concurrency/src/threads/ (replaced by Backend enum)
- Update all imports from spawned_concurrency::tasks:: to spawned_concurrency::

The Backend enum (Async, Blocking, Thread) now provides all the
functionality previously split between tasks and threads modules,
offering a cleaner and more unified API.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- 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.
Integrate the Pid, link, monitor, and registry functionality into the
tasks-based GenServer:

- Add Pid field to GenServerHandle for process identification
- Implement HasPid trait for GenServerHandle
- Add system message handling via handle_info callback
- Add link/unlink methods for bidirectional process linking
- Add monitor/demonitor methods for process monitoring
- Add trap_exit/is_trapping_exit for exit signal handling
- Add register/unregister/registered_name for process registry
- Add start_linked and start_monitored convenience methods
- Update lib.rs to export link, pid, process_table, registry modules
- Add comprehensive Pid and registry tests
@unbalancedparentheses unbalancedparentheses changed the title refactor: deduplicate ChildSpec and RestartIntensityTracker [8/9] [8/9] refactor: deduplicate ChildSpec and RestartIntensityTracker Jan 10, 2026
unbalancedparentheses and others added 7 commits January 9, 2026 21:54
- test_linked_process_killed_on_abnormal_exit: verifies linked process is killed when other exits abnormally
- test_linked_process_survives_normal_exit: verifies linked process survives normal exit
- test_trap_exit_receives_message_instead_of_kill: verifies trap_exit causes EXIT message instead of kill
Implements Erlang/OTP-style supervision for fault-tolerant systems:

Supervisor:
- Manages a static set of child processes defined at start
- Monitors children and restarts them according to restart strategy
- Supports restart strategies: OneForOne, OneForAll, RestForOne
- Child specs with: restart type, shutdown behavior, child type

DynamicSupervisor:
- Manages dynamically spawned children
- Supports start_child/terminate_child for runtime management
- Configurable max_children limit

Features:
- Automatic restart on child crash (permanent, temporary, transient)
- Max restart intensity to prevent restart loops
- Graceful shutdown with configurable timeout
- Type-safe child handle abstraction
- Full integration with GenServer Backend system
- Comprehensive test suite (unit + integration tests)
- Remove CallResponse::Unused and CastResponse::Unused variants
  Default implementations now panic with clear message
- Remove ChildPidWrapper, add impl HasPid for Pid instead
- Delete messages.rs module, use () instead of Unused type
- Reduce lib.rs exports, move supervisor internals to supervisor:: namespace
- Move gen_server tests to gen_server_tests.rs (1431 lines)
- Move supervisor tests to supervisor_tests.rs (768 lines)

Implementation file reductions:
- gen_server.rs: 2245 → 808 lines (-64%)
- supervisor.rs: 2111 → 1389 lines (-34%)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Extract the restart intensity checking logic into a shared
RestartIntensityTracker struct, eliminating duplication between
Supervisor and DynamicSupervisor.

The tracker encapsulates:
- Recording restart timestamps
- Pruning old restart times
- Checking if more restarts are allowed

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Extract ChildSpec::new_with_type() helper to remove duplication
  between worker() and supervisor() constructors
- Add RestartType::should_restart() method to centralize restart
  decision logic used in both Supervisor and DynamicSupervisor

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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