Automatic session cleanup process with TTL (Time-To-Live) support.
This GenServer runs in the background and periodically cleans up expired session processes based on their last activity time. This prevents memory leaks from abandoned sessions and ensures efficient resource utilization.
How It Works
Cleanup Strategy
- Runs cleanup tasks every 60 seconds (
@cleanup_interval) - Checks all active session processes for expiration
- Terminates sessions that have exceeded their TTL
- Emits telemetry events for monitoring cleanup operations
TTL Calculation
Each session process tracks its last activity timestamp. The cleanup process:
- Gets the configured TTL from
Phoenix.SessionProcess.Config.session_ttl() - Calculates the expiration threshold (current_time - TTL)
- Compares each session's last activity against the threshold
- Terminates sessions that have been inactive longer than TTL
Configuration
The cleanup process respects the TTL configuration:
config :phoenix_session_process,
session_ttl: :timer.hours(2) # Sessions expire after 2 hoursPerformance Considerations
Memory Efficiency
- Prevents memory accumulation from abandoned sessions
- Releases resources back to the system
- Maintains optimal memory usage patterns
Performance Impact
- Cleanup runs in the background without blocking requests
- Uses efficient registry operations for session discovery
- Minimal impact on active session performance
Scalability
- Handles thousands of concurrent sessions efficiently
- Cleanup time scales linearly with active session count
- Can be tuned for different deployment scales
Telemetry
The cleanup process emits several telemetry events:
[:phoenix, :session_process, :cleanup]- Cleanup cycle completed[:phoenix, :session_process, :cleanup_error]- Cleanup operation failed[:phoenix, :session_process, :session_expired]- Individual session expired
Monitoring
You can monitor cleanup performance through telemetry events. See the Telemetry section above for available events.
Example handler setup is documented in the telemetry module.
Error Handling
- Session process termination failures are logged but don't stop cleanup
- Registry lookup errors are handled gracefully
- Continuous operation ensures consistent cleanup performance
Integration
This process is automatically started by the top-level supervisor and requires no manual intervention or configuration beyond setting the desired TTL.
Summary
Functions
Cancels scheduled cleanup for a session.
Returns a specification to start this module under a supervisor.
Refreshes the TTL for a session by canceling the old timer and scheduling a new one. This is called when a session is actively used to extend its lifetime.
Schedules cleanup for a specific session after TTL. Returns timer reference for potential cancellation.
Functions
@spec cancel_session_cleanup(binary()) :: :ok
Cancels scheduled cleanup for a session.
Returns a specification to start this module under a supervisor.
See Supervisor.
Refreshes the TTL for a session by canceling the old timer and scheduling a new one. This is called when a session is actively used to extend its lifetime.
Schedules cleanup for a specific session after TTL. Returns timer reference for potential cancellation.