Repository navigation
Conversation
…#418) Adds SessionDriversTrait, which builds every built-in storage driver (file, in-memory, cache, SQLite, MySQL, MSSQL) for driver-parametrized tests. Database drivers are skipped once per run when unreachable, and pooled connections are closed after each test. SessionUserPersistenceTest checks that setUser() writes the reserved _user key and that the user is restored on resume, on every driver.
…orage Session now computes each session's expiry (startedAt + duration, or now + duration with refresh) and hands it to storages that implement the new optional ExpiringSessionStorage interface. GC removes sessions whose own expiry has passed, and falls back to the idle threshold for sessions without one. All built-in drivers implement it: - DatabaseSessionStorage: nullable sessions.expires_at column with an index. SessionSchemaMigration::run() adds both. Unmigrated databases keep the old behavior. - DefaultSessionStorage: <id>.exp sidecar file. - CacheSessionStorage: entry TTLs follow the remaining lifetime instead of a fixed 7200 seconds. - InMemorySessionStorage: expiry map; gc() now removes expired sessions. Also fixes: - setDuration() and setIsRefresh() persist _meta on a running session (#431). - Resuming a refresh session extends its stored expiry, at most once a minute (#433). - SESSION_GC is read as an idle period in seconds, not a Unix timestamp (#434). - A missing lifeTime in _meta falls back to 120 minutes, not 2 (#435). - SessionSchemaMigration called a non-existent createIfNotExists() and used ADD COLUMN, which SQL Server rejects. See ADR-0061 in webfiori/docs. Fixes #431 Fixes #432 Fixes #433 Fixes #434 Fixes #435
No behavior change. Only the files touched by this work are formatted; most of the change is the fixer's method ordering.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #436 +/- ##
============================================
+ Coverage 81.99% 82.25% +0.26%
- Complexity 3301 3355 +54
============================================
Files 106 106
Lines 9752 9868 +116
============================================
+ Hits 7996 8117 +121
+ Misses 1756 1751 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
PHPUnit 12 (used by the PHP 8.5 CI job) ignores @dataProvider annotations, so the driver-parametrized tests ran without arguments. Attributes work on PHPUnit 10 and 12.
CI runs PHPUnit 9.5 (PHP 8.1, 8.2), 10 (PHP 8.3, 8.4) and 12 (PHP 8.5). PHPUnit 9 only reads @dataProvider annotations and PHPUnit 12 only reads #[DataProvider] attributes, so declare both.
ubuntu-latest moved to Ubuntu 26.04, which no longer provides mysql-client-core-8.0, so every PHP job failed in 'Setup MySQL Client' before running tests.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Session garbage collection now removes each session by its own expiry time, instead of after 4 idle hours. Storages record the expiry through a new optional
ExpiringSessionStorageinterface, which all built-in drivers implement. The PR also fixes three smaller session lifetime bugs.Motivation
GC used one global idle threshold (4 hours) and never read a session's lifetime. Long-lived sessions, such as a 7 day "remember me" session, were deleted after 4 idle hours (#432). Sessions that were only read looked idle, because resuming them didn't update their last-use time (#433). Three more bugs broke lifetimes:
setDuration()/setIsRefresh()weren't saved (#431),SESSION_GCwas read as a timestamp (#434), and a missinglifeTimefell back to 2 minutes (#435).The design is recorded in ADR-0061.
User docs are updated in WebFiori/docs@7da9d3e. That commit changes Sessions Management (GC, custom storages, cache TTL, migration step) and the
SESSION_GCrow in env-vars.md.Changes
ExpiringSessionStorage(getExpiry(),setExpiry()), an optional extension ofSessionStorage.Sessioncomputes the expiry (startedAt + duration, ornow + durationwith refresh) and stores it whenever_metais written. On resume, a refresh session's expiry is rewritten when it drifts by more than 60 seconds, so a busy session costs at most one write per minute.DatabaseSessionStorage: nullablesessions.expires_atcolumn with indexidx_sessions_expires_at. GC removes expired rows, plus rows with no expiry that are past the idle threshold.DefaultSessionStorage:<id>.expsidecar file. GC also removes orphan sidecars.CacheSessionStorage: entry TTLs follow the remaining lifetime instead of a fixed 7200 seconds.InMemorySessionStorage: expiry map.gc()now removes expired sessions.SessionSchemaMigration::run()adds the column and index. It can be run more than once. Two existing bugs are fixed: it called a non-existentcreateIfNotExists(), and itsADD COLUMNsyntax fails on SQL Server.setDuration()andsetIsRefresh()save_metaon a running session (bug(session): setDuration()/setIsRefresh() not persisted in per-key storage, session reverts to its creation lifetime #431).SESSION_GCis an idle period in seconds (bug(session): SESSION_GC is used as an absolute Unix timestamp instead of a duration #434).lifeTimefalls back toDEFAULT_SESSION_DURATION, 120 minutes (bug(session): missing lifeTime in _meta falls back to 2 minutes instead of the default duration #435).How to Test / Verify
Unit and integration tests run against every built-in driver: default (file), in-memory, cache, SQLite, MySQL 8 and SQL Server 2022.
SessionUserPersistenceTest: the session user is saved and restored on every driver (bug(session): setUser() not persisted in per-key storage mode (v3.1.0) #418).SessionLifetimeBugsTest: reproduction tests for bug(session): setDuration()/setIsRefresh() not persisted in per-key storage, session reverts to its creation lifetime #431 to bug(session): missing lifeTime in _meta falls back to 2 minutes instead of the default duration #435. Before the fix they failed on every applicable driver. They now pass.ExpiringSessionStorageTest: interface contract per driver (round trip, destroy, GC by expiry, idle-threshold fallback, batch limit). Also covers an unmigrated database and running the migration twice, on SQLite, MySQL and SQL Server.SessionExpiryTest: howSessioncomputes, refreshes and throttles the expiry, and that storages without the interface keep working.Result: 264 tests pass and 1 is skipped on purpose (the cache driver has no GC batch limit). Unreachable databases are skipped once per run instead of timing out on every test.
The full suite has 55 failures, all in
tests/.../Cli. Those tests hardcode MySQL port 3306 and failed with "Access denied" against an unrelated local server. They don't touch session code.Code style:
php-cs-fixer(php_cs.php.dist) was applied to the 15 files this PR changes, in a separatestyle(session)commit with no behavior change. Most of that commit is the fixer's method ordering, so review thefeatcommit for the logic. It wasn't run on the whole repo, where 182 of 339 files already differ from the fixer's output.Breaking Changes and Migration Steps
No public API is removed or changed.
ExpiringSessionStorageis optional, and customSessionStorageimplementations keep the 3.1 behavior.Behavior changes to be aware of:
SESSION_GCis now a duration in seconds. It used to be an absolute Unix timestamp. A static timestamp could never give a moving threshold, so we don't expect anyone to rely on the old meaning. If you set it, change it to the idle period you want, for example604800for one week.Migration steps for database session storage:
SessionSchemaMigration::run('sessions-connection')once after deploying. It addssessions.expires_atand its index.Checklist
composer fix-cs)Related issues
Closes #431
Closes #432
Closes #433
Closes #434
Closes #435
Related: #418 (already fixed in v3.1.1; this PR adds per-driver regression tests)