Skip to content

feature: separate connection per session so it is not shared #89

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

Merged
merged 6 commits into from
Apr 14, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Fixed

Changed
- Use google-cloud-php's CacheSessionPool since the [concerned bug](https://github.com/googleapis/google-cloud-php/issues/5567) has been fixed in [v1.53](https://github.com/googleapis/google-cloud-php-spanner/releases/tag/v1.58.2). (#90)
- Separate session pool and authentication per connection so transaction works properly. (#89)

# v5.0.0

Expand Down
14 changes: 8 additions & 6 deletions src/SpannerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ protected function createSpannerConnection(array $config): Connection
$config['database'],
$config['prefix'],
$config,
$this->createAuthCache(),
$this->createSessionPool($config['session_pool'] ?? [])
$this->createAuthCache($config['name']),
$this->createSessionPool($config['name'], $config['session_pool'] ?? [])
);
}

Expand All @@ -87,22 +87,24 @@ protected function parseConfig(array $config, string $name): array
}

/**
* @param string $name
* @param array<string, mixed> $sessionPoolConfig
* @return SessionPoolInterface
*/
protected function createSessionPool(array $sessionPoolConfig): SessionPoolInterface
protected function createSessionPool(string $name, array $sessionPoolConfig): SessionPoolInterface
{
$cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner']));
$cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner', $name]));
$adapter = new FilesystemAdapter('session', 0, $cachePath);
return new CacheSessionPool($adapter, $sessionPoolConfig);
}

/**
* @param string $name
* @return CacheItemPoolInterface
*/
protected function createAuthCache(): CacheItemPoolInterface
protected function createAuthCache(string $name): CacheItemPoolInterface
{
$cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner']));
$cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner', $name]));
return new FilesystemAdapter('auth', 0, $cachePath);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function test_AuthCache_with_FileSystemAdapter(): void
{
$conn = $this->getDefaultConnection();
$conn->select('SELECT 1');
self::assertDirectoryExists(storage_path('framework/spanner/auth'));
self::assertDirectoryExists(storage_path("framework/spanner/{$conn->getName()}/auth"));
}

public function testSessionPool(): void
Expand All @@ -272,7 +272,7 @@ public function test_session_pool_with_FileSystemAdapter(): void
{
$conn = $this->getDefaultConnection();
$conn->select('SELECT 1');
self::assertDirectoryExists(storage_path('framework/spanner/session'));
self::assertDirectoryExists(storage_path("framework/spanner/{$conn->getName()}/session"));
}

public function test_clearSessionPool(): void
Expand Down
8 changes: 2 additions & 6 deletions tests/Console/SessionsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ protected function createSessions(Connection $connection, int $amount): array

public function test_no_args(): void
{
if (getenv('SPANNER_EMULATOR_HOST')) {
$this->markTestSkipped('Cannot list sessions on emulator');
}

foreach (['main', 'alternative'] as $name) {
$conn = $this->getConnection($name);
assert($conn instanceof Connection);
Expand All @@ -57,8 +53,8 @@ public function test_no_args(): void
->run();

$this->artisan('spanner:sessions')
->expectsOutputToContain('main contains 1 session(s).')
->expectsOutputToContain('alternative contains 1 session(s).')
->expectsOutputToContain('main contains 2 session(s).')
->expectsOutputToContain('alternative contains 2 session(s).')
->assertSuccessful()
->run();
}
Expand Down
5 changes: 1 addition & 4 deletions tests/Console/WarmupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ public function test_no_args(): void
$this->setUpDatabaseOnce($conn);
}

// Emulator cannot distinguish different connections
$altWarmupCount = env('SPANNER_EMULATOR_HOST') ? 0 : 1;

$this->artisan('spanner:warmup')
->expectsOutputToContain("Warmed up 1 sessions for main")
->expectsOutputToContain("Warmed up {$altWarmupCount} sessions for alternative")
->expectsOutputToContain("Warmed up 1 sessions for alternative")
->assertSuccessful()
->run();

Expand Down