Skip to content

Commit 1d6e97b

Browse files
gsmlgGSMLG-BOT
andauthored
docs: Enhance CLAUDE.md with comprehensive architecture documentation (#4)
Co-authored-by: Jonathan Gao <[email protected]>
1 parent 26369ff commit 1d6e97b

File tree

23 files changed

+940
-406
lines changed

23 files changed

+940
-406
lines changed

.claude/settings.local.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ '*' ]
6+
7+
jobs:
8+
test:
9+
name: Build and test
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Elixir
17+
uses: erlef/setup-beam@v1
18+
with:
19+
elixir-version: '1.18'
20+
otp-version: '28'
21+
22+
- name: Restore dependencies cache
23+
uses: actions/cache@v4
24+
with:
25+
path: |
26+
deps
27+
_build
28+
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
29+
restore-keys: ${{ runner.os }}-mix-
30+
31+
- name: Install dependencies
32+
run: mix deps.get
33+
34+
- name: Compile
35+
run: mix compile --warnings-as-errors
36+
37+
- name: Run Credo
38+
run: mix credo --strict
39+
40+
- name: Build Dialyzer PLT (cached)
41+
id: plt
42+
uses: actions/cache@v4
43+
with:
44+
path: priv/plts
45+
key: dialyzer-${{ hashFiles('mix.lock') }}
46+
47+
- name: Run Dialyzer
48+
run: mix dialyzer --halt-exit-status
49+

.github/workflows/elixir.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v5
16+
17+
- name: Set up Elixir
18+
uses: erlef/setup-beam@v1
19+
with:
20+
elixir-version: '1.18'
21+
otp-version: '28'
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v4
25+
with:
26+
path: |
27+
deps
28+
_build
29+
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
30+
restore-keys: |
31+
${{ runner.os }}-mix-
32+
33+
- name: Install dependencies
34+
run: |
35+
mix local.hex --force
36+
mix local.rebar --force
37+
mix deps.get
38+
39+
- name: Run tests
40+
run: mix test

CLAUDE.md

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2324
The project uses `devenv` for development environment setup with Nix. Key configuration:
@@ -42,44 +43,63 @@ Expected performance:
4243
1. **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

5557
4. **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

6175
1. Session ID generation via the SessionId plug
6276
2. 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

7696
The library uses application configuration:
7797
```elixir
7898
config :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

85105
Configuration options:
@@ -91,20 +111,33 @@ Configuration options:
91111
## Usage in Phoenix Applications
92112

93113
1. 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
96116
4. Start processes with session IDs
97117
5. 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
102130
The 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
110143
Common 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.

GEMINI.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

devenv.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{ pkgs, lib, config, inputs, ... }:
21

32
let
43
pkgs-stable = import inputs.nixpkgs-stable { system = pkgs.stdenv.system; };

0 commit comments

Comments
 (0)