Skip to content

Commit

Permalink
feat:[Spanner] CacheSessionPool::maintain deletes sessions older than…
Browse files Browse the repository at this point in the history
… 28d (#5853)
  • Loading branch information
vishwarajanand authored Feb 22, 2023
1 parent 28ef340 commit fbf2081
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 15 deletions.
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;
}
// 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

0 comments on commit fbf2081

Please sign in to comment.