Skip to content

Conversation

@unbalancedparentheses
Copy link
Contributor

@unbalancedparentheses unbalancedparentheses commented Jan 8, 2026

Summary

Consolidate two separate GenServer implementations into a single implementation with a Backend enum parameter.

Breaking change: start() now requires a Backend argument.

New API

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

let handle = server.start(Backend::Async);

Backend Comparison

Backend Execution Model Best For
Async Tokio task Non-blocking I/O, async operations
Blocking Tokio blocking pool Short blocking operations
Thread Dedicated OS thread Long-running blocking work

PR Chain

This is PR 1 of 6:

  1. [1/6] feat: unify GenServer with Backend enum for runtime selection #74 (this PR) - Backend enum for GenServer
  2. [2/6] feat: add Pid and process linking primitives #68 - Pid and process linking primitives
  3. [3/6] feat: add global process table for link/monitor management #69 - Global process table
  4. [4/6] feat: add process registry for name-based lookup #70 - Process registry
  5. [5/6] feat: add link/monitor/registry integration to tasks GenServer #71 - GenServer integration with Pid/links
  6. Supervisor (pending)

All subsequent PRs depend on this one.

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
Property-based tests (proptest):
- Counter preserves initial state
- N increments result in initial + N
- Get is idempotent (multiple calls return same value)
- All backends produce working GenServers
- Multiple GenServers maintain independent state
- Cast followed by Get reflects the cast
Fuzzing (cargo-fuzz):
- Add fuzz target for GenServer operations
- Test random sequences of call/cast operations
- Verify state consistency across all backends
- Run with: cd concurrency/fuzz && cargo fuzz run fuzz_genserver_operations
… communication

- Backend equivalence tests: verify all backends produce identical results
- Cross-backend communication: test GenServers on different backends calling each other
- Stress tests: concurrent operations, mixed call/cast
- Init/teardown: verify lifecycle hooks work on all backends
- State consistency: large operations, alternating operations
- Remove concurrency/src/threads/ directory (replaced by Backend enum)
- Move files from concurrency/src/tasks/ to concurrency/src/
- Update all imports from spawned_concurrency::tasks:: to spawned_concurrency::
- Update internal crate imports accordingly
- Update examples README to reflect current architecture

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.
@unbalancedparentheses
Copy link
Contributor Author

Reorganizing PR sequence for coherent implementation order. This content will be included in the new PR series.

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