I've successfully implemented auto-expiration for WebSocket sessions in the Veritix Python ticketing platform. Here's what was accomplished:
- Enhanced TicketScanManager with session timeout functionality
- Activity tracking for each WebSocket connection
- Background cleanup task that runs every minute
- Configurable timeout via constructor parameter
- Automatic cleanup of inactive sessions
- Environment variable support:
SESSION_TIMEOUT_MINUTES - Default timeout: 30 minutes (configurable)
- Startup/shutdown hooks for cleanup task management
- Proper lifecycle management of background tasks
- Added
SESSION_TIMEOUT_MINUTES=30configuration option - Clear documentation for users
- 14 test cases covering all functionality
- Tests for initialization, connection tracking, cleanup logic
- Environment variable configuration testing
- Exception handling verification
- Broadcast activity updates
- Detailed feature documentation
- Configuration guide
- Architecture explanation
- Best practices and troubleshooting
- Performance considerations
- Sessions automatically expire after configured inactivity period
- Background task checks every minute
- Graceful cleanup with proper logging
- Connection timestamps updated on:
- New connections
- Broadcast messages
- WebSocket communication
- Environment variable:
SESSION_TIMEOUT_MINUTES - Default: 30 minutes
- Range: Any positive integer (minutes)
- Exception-safe cleanup task
- Graceful handling of connection failures
- Proper resource cleanup
-
Set timeout in
.env:SESSION_TIMEOUT_MINUTES=30
-
Start the service:
docker compose up -d # or python run.py -
Sessions will automatically expire after the configured time of inactivity
The implementation includes comprehensive tests that verify:
- ✅ Manager initialization
- ✅ Connection activity tracking
- ✅ Session cleanup logic
- ✅ Background task lifecycle
- ✅ Configuration via environment variables
- ✅ Exception handling
- ✅ Broadcast activity updates
Modified:
src/manager.py- Added session timeout logicsrc/websocket.py- Added configuration and lifecycle management.env.example- Added session timeout configuration
Added:
tests/test_session_timeout.py- Comprehensive test suitedocs/session_timeout.md- Detailed documentationverify_session_timeout.py- Simple verification script
The feature is production-ready and follows the project's existing patterns and standards.