Encapsulate session handling from protocol to new SessionManager (#247)
This commit is contained in:
parent
a6ed01bec0
commit
ae320646bf
1 changed files with 444 additions and 200 deletions
69
src/Server/Session/SessionManager.php
Normal file
69
src/Server/Session/SessionManager.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Server\Session;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
* Default implementation of SessionManagerInterface.
|
||||
*
|
||||
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
|
||||
*/
|
||||
class SessionManager implements SessionManagerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SessionStoreInterface $store,
|
||||
private readonly LoggerInterface $logger = new NullLogger(),
|
||||
) {
|
||||
}
|
||||
|
||||
public function create(): SessionInterface
|
||||
{
|
||||
return new Session($this->store, Uuid::v4());
|
||||
}
|
||||
|
||||
public function createWithId(Uuid $id): SessionInterface
|
||||
{
|
||||
return new Session($this->store, $id);
|
||||
}
|
||||
|
||||
public function exists(Uuid $id): bool
|
||||
{
|
||||
return $this->store->exists($id);
|
||||
}
|
||||
|
||||
public function destroy(Uuid $id): bool
|
||||
{
|
||||
return $this->store->destroy($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run garbage collection on expired sessions.
|
||||
* Uses the session store's internal TTL configuration.
|
||||
*/
|
||||
public function gc(): void
|
||||
{
|
||||
if (random_int(0, 100) > 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$deletedSessions = $this->store->gc();
|
||||
if (!empty($deletedSessions)) {
|
||||
$this->logger->debug('Garbage collected expired sessions.', [
|
||||
'count' => \count($deletedSessions),
|
||||
'session_ids' => array_map(static fn (Uuid $id) => $id->toRfc4122(), $deletedSessions),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue