@@ -14,10 +14,11 @@ This is Phoenix.SessionProcess, an Elixir library that creates a process for eac
1414- ` mix test test/path/to/specific_test.exs ` - Run a specific test file
1515- ` mix compile ` - Compile the project
1616- ` mix docs ` - Generate documentation
17+ - ` mix format ` - Format code
1718- ` mix hex.publish ` - Publish to Hex.pm (requires authentication)
1819
1920### Testing
20- The test suite uses ExUnit. Tests are located in the ` test/ ` directory. The test helper starts the supervisor automatically .
21+ The test suite uses ExUnit. Tests are located in the ` test/ ` directory. The test helper (test/test_helper.exs:3) automatically starts the supervisor.
2122
2223### Development Environment
2324The project uses ` devenv ` for development environment setup with Nix. Key configuration:
@@ -42,44 +43,63 @@ Expected performance:
42431 . ** Phoenix.SessionProcess** (lib/phoenix/session_process.ex:1)
4344 - Main module providing the public API
4445 - Delegates to ProcessSupervisor for actual process management
45- - Provides macros for creating session processes
46+ - Provides two macros: ` :process ` (basic) and ` :process_link ` (with LiveView monitoring)
4647
47- 2 . ** Phoenix.SessionProcess.Supervisor** (lib/phoenix/session_process/superviser.ex)
48- - Top-level supervisor that manages the Registry and ProcessSupervisor
48+ 2 . ** Phoenix.SessionProcess.Supervisor** (lib/phoenix/session_process/superviser.ex:1 )
49+ - Top-level supervisor that manages the Registry, ProcessSupervisor, and Cleanup
4950 - Must be added to the application's supervision tree
5051
51- 3 . ** Phoenix.SessionProcess.ProcessSupervisor** (lib/phoenix/session_process/process_superviser.ex)
52+ 3 . ** Phoenix.SessionProcess.ProcessSupervisor** (lib/phoenix/session_process/process_superviser.ex:1 )
5253 - DynamicSupervisor that manages individual session processes
5354 - Handles starting, terminating, and communicating with session processes
55+ - Performs session validation and limit checks
5456
55574 . ** Phoenix.SessionProcess.SessionId** (lib/phoenix/session_process/session_id.ex)
5658 - Plug that generates unique session IDs
5759 - Must be placed after ` :fetch_session ` plug
5860
61+ 5 . ** Phoenix.SessionProcess.Cleanup** (lib/phoenix/session_process/cleanup.ex:1)
62+ - Automatic TTL-based session cleanup
63+ - Schedules session expiration on creation
64+
65+ 6 . ** Phoenix.SessionProcess.Redux** (lib/phoenix/session_process/redux.ex:1)
66+ - Redux-style state management with actions and reducers
67+ - Provides time-travel debugging, middleware support, and action history
68+
69+ 7 . ** Phoenix.SessionProcess.State** (lib/phoenix/session_process/state.ex:1)
70+ - Agent-based state storage with Redux-style dispatch support
71+ - Used for simpler state management scenarios
72+
5973### Process Management Flow
6074
61751 . Session ID generation via the SessionId plug
62762 . Process creation through ` Phoenix.SessionProcess.start/1-3 `
63- 3 . Processes are registered in ` Phoenix.SessionProcess.Registry `
64- 4 . Communication via ` call/2-3 ` and ` cast/2 `
65- 5 . Automatic cleanup when processes terminate
77+ 3 . Validation checks (session ID format, session limits)
78+ 4 . Processes are registered in ` Phoenix.SessionProcess.Registry ` with two entries:
79+ - ` {session_id, pid} ` for session lookup
80+ - ` {pid, module} ` for module tracking
81+ 5 . TTL-based cleanup is scheduled for each session
82+ 6 . Communication via ` call/2-3 ` and ` cast/2 `
83+ 7 . Automatic cleanup when processes terminate or TTL expires
6684
6785### Key Design Patterns
6886
69- - Uses Registry for process lookup by session ID
87+ - Uses Registry for bidirectional lookups (session_id ↔ pid, pid ↔ module)
7088- DynamicSupervisor for on-demand process creation
71- - Provides two macros: ` :process ` (basic) and ` :process_link ` (with LiveView monitoring)
72- - Session processes can monitor LiveView processes and notify them on termination
89+ - Macros inject GenServer boilerplate and provide ` get_session_id/0 ` helper
90+ - ` :process_link ` macro adds LiveView monitoring: sessions monitor LiveView processes and send ` :session_expired ` message on termination
91+ - Telemetry events for all lifecycle operations (start, stop, call, cast, cleanup, errors)
92+ - Comprehensive error handling with Phoenix.SessionProcess.Error module
7393
7494## Configuration
7595
7696The library uses application configuration:
7797``` elixir
7898config :phoenix_session_process ,
7999 session_process: MySessionProcess , # Default session module
80- max_sessions: 10_000 , # Maximum concurrent sessions
81- session_ttl: 3_600_000 , # Session TTL in milliseconds (1 hour)
82- rate_limit: 100 # Sessions per minute limit
100+ max_sessions: 10_000 , # Maximum concurrent sessions
101+ session_ttl: 3_600_000 , # Session TTL in milliseconds (1 hour)
102+ rate_limit: 100 # Sessions per minute limit
83103```
84104
85105Configuration options:
@@ -91,20 +111,33 @@ Configuration options:
91111## Usage in Phoenix Applications
92112
931131 . Add supervisor to application supervision tree
94- 2 . Add SessionId plug after fetch_session
95- 3 . Define custom session process modules using the provided macros
114+ 2 . Add SessionId plug after fetch_session in router
115+ 3 . Define custom session process modules using ` :process ` or ` :process_link ` macros
961164 . Start processes with session IDs
971175 . Communicate using call/cast operations
98118
119+ ## State Management Options
120+
121+ The library provides three state management approaches:
122+
123+ 1 . ** Basic GenServer** - Full control with standard GenServer callbacks
124+ 2 . ** Phoenix.SessionProcess.State** - Agent-based with simple get/put and Redux dispatch
125+ 3 . ** Phoenix.SessionProcess.Redux** - Full Redux pattern with actions, reducers, middleware, time-travel debugging
126+
99127## Telemetry and Error Handling
100128
101129### Telemetry Events
102130The library emits comprehensive telemetry events for monitoring:
103131- ` [:phoenix, :session_process, :start] ` - Session starts
104132- ` [:phoenix, :session_process, :stop] ` - Session stops
133+ - ` [:phoenix, :session_process, :start_error] ` - Session start errors
105134- ` [:phoenix, :session_process, :call] ` - Call operations
106135- ` [:phoenix, :session_process, :cast] ` - Cast operations
136+ - ` [:phoenix, :session_process, :communication_error] ` - Communication errors
107137- ` [:phoenix, :session_process, :cleanup] ` - Session cleanup
138+ - ` [:phoenix, :session_process, :cleanup_error] ` - Cleanup errors
139+
140+ Events include metadata (session_id, module, pid) and measurements (duration in native time units).
108141
109142### Error Types
110143Common error responses:
@@ -113,4 +146,4 @@ Common error responses:
113146- ` {:error, {:session_not_found, session_id}} ` - Session doesn't exist
114147- ` {:error, {:timeout, timeout}} ` - Operation timed out
115148
116- Use ` Phoenix.SessionProcess.Error.message/1 ` for human-readable error messages.
149+ Use ` Phoenix.SessionProcess.Error.message/1 ` for human-readable error messages.
0 commit comments