Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:[Spanner] CacheSessionPool::maintain deletes sessions older than 28d #5853

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions Spanner/src/Session/CacheSessionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class CacheSessionPool implements SessionPoolInterface
use SysvTrait;

const CACHE_KEY_TEMPLATE = 'cache-session-pool.%s.%s.%s';
const DURATION_SESSION_LIFETIME = 28*24*3600; // 28 days
const DURATION_TWENTY_MINUTES = 1200;
const DURATION_ONE_MINUTE = 60;
const WINDOW_SIZE = 600;
Expand Down Expand Up @@ -349,11 +350,16 @@ public function release(Session $session)
$name = $session->name();

if (isset($data['inUse'][$name])) {
// set creation time to an expired time if no value is found
$creationTime = isset($data['inUse'][$name]['creation'])
? $data['inUse'][$name]['creation']
: $this->time() - self::DURATION_SESSION_LIFETIME;
unset($data['inUse'][$name]);
array_push($data['queue'], [
'name' => $name,
'expiration' => $session->expiration()
?: $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS
?: $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS,
'creation' => $creationTime,
]);
$this->save($item->set($data));
}
Expand Down Expand Up @@ -697,7 +703,8 @@ private function createSessions($count)
foreach ($res['session'] as $result) {
$sessions[] = [
'name' => $result['name'],
'expiration' => $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS
'expiration' => $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS,
'creation' => $this->time(),
];

$created++;
Expand All @@ -716,11 +723,19 @@ private function createSessions($count)
*/
private function isSessionValid(array $session)
{
$halfHourBeforeExpiration = $session['expiration'] - (SessionPoolInterface::SESSION_EXPIRATION_SECONDS / 2);
$halfHourBeforeExpiration = $session['expiration'] - 1800;

// sessions more than 28 days old are auto deleted by server
if (self::DURATION_SESSION_LIFETIME + $session['creation'] <
$this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS) {
return false;
}
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
// session expires in more than half hour
if ($this->time() < $halfHourBeforeExpiration) {
return true;
} elseif ($halfHourBeforeExpiration < $this->time() && $this->time() < $session['expiration']) {
}
// session expires in less than a half hour, but is not expired
if ($this->time() < $session['expiration']) {
return $this->database
->session($session['name'])
->exists();
Expand Down Expand Up @@ -920,6 +935,14 @@ public function maintain()
}

$sessions = $cachedData['queue'];
foreach ($sessions as $id => $session) {
if (self::DURATION_SESSION_LIFETIME + $session['creation'] <
$this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS) {
// sessions more than 28 days old are auto deleted by server
$this->deleteQueue += $session;
unset($sessions[$id]);
}
}
// Sort sessions by expiration time, "oldest" first.
// acquire() method picks sessions from the beginning of the queue,
// so make sure that "oldest" ones will be picked first.
Expand Down Expand Up @@ -976,6 +999,7 @@ public function maintain()
$sessions[] = [
'name' => $item['name'],
'expiration' => $session->expiration(),
'creation' => $item['creation'],
];
$freshSessionsCount++;
} else {
Expand Down Expand Up @@ -1007,6 +1031,7 @@ public function maintain()
$sessions[] = [
'name' => $item['name'],
'expiration' => $session->expiration(),
'creation' => $item['creation'],
];
}
}
Expand Down
3 changes: 3 additions & 0 deletions Spanner/tests/System/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function testCacheSessionPool()

$this->assertPoolCounts($cache, $cacheKey, 0, 10, 0);

$pool->maintain();
$this->assertPoolCounts($cache, $cacheKey, 0, 10, 0);

$exception = null;
try {
$pool->acquire();
Expand Down
Loading