Skip to content

Commit

Permalink
feat(Spanner): CacheSessionPool::maintain deletes
Browse files Browse the repository at this point in the history
sessions older than 28d
  • Loading branch information
vishwarajanand committed Feb 3, 2023
1 parent 5c24f16 commit 481998c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 14 deletions.
24 changes: 21 additions & 3 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 @@ -342,11 +343,13 @@ public function release(Session $session)
$name = $session->name();

if (isset($data['inUse'][$name])) {
$creationTime = $data['inUse'][$name]['creation'];
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 @@ -690,7 +693,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 @@ -711,7 +715,11 @@ private function isSessionValid(array $session)
{
$halfHourBeforeExpiration = $session['expiration'] - (SessionPoolInterface::SESSION_EXPIRATION_SECONDS / 2);

if ($this->time() < $halfHourBeforeExpiration) {
if (self::DURATION_SESSION_LIFETIME + $session['creation'] <
$this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS) {
// sessions more than 28 days old are auto deleted by server
return false;
} elseif ($this->time() < $halfHourBeforeExpiration) {
return true;
} elseif ($halfHourBeforeExpiration < $this->time() && $this->time() < $session['expiration']) {
return $this->database
Expand Down Expand Up @@ -904,6 +912,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 @@ -960,6 +976,7 @@ public function maintain()
$sessions[] = [
'name' => $item['name'],
'expiration' => $session->expiration(),
'creation' => $item['creation'],
];
$freshSessionsCount++;
} else {
Expand Down Expand Up @@ -991,6 +1008,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 481998c

Please sign in to comment.