From e257237b61da7cb25a59a95299e232e9b536d93c Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 13 Sep 2018 20:51:10 +0200 Subject: [PATCH] Improve naming Relates to #1. --- composer.json | 1 + src/AbstractPool.php | 27 +++++++++++++++------------ src/CommandResult.php | 2 +- src/Link.php | 2 +- src/Pool.php | 5 +---- src/PooledResultSet.php | 2 +- src/PooledStatement.php | 4 ++-- src/PooledTransaction.php | 4 ++-- src/StatementPool.php | 6 +++--- src/TransientResource.php | 4 ++-- test/StatementPoolTest.php | 8 ++++---- 11 files changed, 33 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 03a9f8e..7afaaed 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "homepage": "http://amphp.org", "license": "MIT", "require": { + "php": ">=7", "amphp/amp": "^2" }, "require-dev": { diff --git a/src/AbstractPool.php b/src/AbstractPool.php index 2d1e74c..4a4058b 100644 --- a/src/AbstractPool.php +++ b/src/AbstractPool.php @@ -13,6 +13,9 @@ abstract class AbstractPool implements Pool { use CallableMaker; + const DEFAULT_MAX_CONNECTIONS = 100; + const DEFAULT_IDLE_TIMEOUT = 60; + /** @var Connector */ private $connector; @@ -28,10 +31,10 @@ abstract class AbstractPool implements Pool /** @var \SplObjectStorage */ private $connections; - /** @var \Amp\Promise|null */ + /** @var Promise|null */ private $promise; - /** @var \Amp\Deferred|null */ + /** @var Deferred|null */ private $deferred; /** @var callable */ @@ -106,8 +109,8 @@ abstract class AbstractPool implements Pool */ public function __construct( ConnectionConfig $config, - int $maxConnections = Pool::DEFAULT_MAX_CONNECTIONS, - int $idleTimeout = Pool::DEFAULT_IDLE_TIMEOUT, + int $maxConnections = self::DEFAULT_MAX_CONNECTIONS, + int $idleTimeout = self::DEFAULT_IDLE_TIMEOUT, Connector $connector = null ) { $this->connector = $connector ?? $this->createDefaultConnector(); @@ -136,7 +139,7 @@ abstract class AbstractPool implements Pool $connection = $idle->bottom(); \assert($connection instanceof Link); - if ($connection->lastUsedAt() + $idleTimeout > $now) { + if ($connection->getLastUsedAt() + $idleTimeout > $now) { return; } @@ -160,7 +163,7 @@ abstract class AbstractPool implements Pool return $this->idleTimeout; } - public function lastUsedAt(): int + public function getLastUsedAt(): int { // Simple implementation... can be improved if needed. @@ -168,7 +171,7 @@ abstract class AbstractPool implements Pool foreach ($this->connections as $connection) { \assert($connection instanceof Link); - if (($lastUsedAt = $connection->lastUsedAt()) > $time) { + if (($lastUsedAt = $connection->getLastUsedAt()) > $time) { $time = $lastUsedAt; } } @@ -229,7 +232,7 @@ abstract class AbstractPool implements Pool /** * {@inheritdoc} */ - public function getMaxConnections(): int + public function getConnectionLimit(): int { return $this->maxConnections; } @@ -248,14 +251,14 @@ abstract class AbstractPool implements Pool throw new \Error("The pool has been closed"); } - while ($this->promise !== null && $this->connections->count() + $this->pending >= $this->getMaxConnections()) { + while ($this->promise !== null && $this->connections->count() + $this->pending >= $this->getConnectionLimit()) { yield $this->promise; // Prevent simultaneous connection creation when connection count is at maximum - 1. } do { // While loop to ensure an idle connection is available after promises below are resolved. while ($this->idle->isEmpty()) { - if ($this->connections->count() + $this->pending < $this->getMaxConnections()) { + if ($this->connections->count() + $this->pending < $this->getConnectionLimit()) { // Max connection count has not been reached, so open another connection. ++$this->pending; try { @@ -409,14 +412,14 @@ abstract class AbstractPool implements Pool /** * {@inheritdoc} */ - public function transaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise + public function beginTransaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise { return call(function () use ($isolation) { $connection = yield from $this->pop(); \assert($connection instanceof Link); try { - $transaction = yield $connection->transaction($isolation); + $transaction = yield $connection->beginTransaction($isolation); \assert($transaction instanceof Transaction); } catch (\Throwable $exception) { $this->push($connection); diff --git a/src/CommandResult.php b/src/CommandResult.php index 046b531..b441dbd 100644 --- a/src/CommandResult.php +++ b/src/CommandResult.php @@ -9,5 +9,5 @@ interface CommandResult * * @return int */ - public function affectedRows(): int; + public function getAffectedRowCount(): int; } diff --git a/src/Link.php b/src/Link.php index 5d117a3..88cf9aa 100644 --- a/src/Link.php +++ b/src/Link.php @@ -13,5 +13,5 @@ interface Link extends Executor * * @return Promise */ - public function transaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise; + public function beginTransaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise; } diff --git a/src/Pool.php b/src/Pool.php index 2903042..98f55b5 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -6,9 +6,6 @@ use Amp\Promise; interface Pool extends Link { - const DEFAULT_MAX_CONNECTIONS = 100; - const DEFAULT_IDLE_TIMEOUT = 60; - /** * @return Promise */ @@ -27,7 +24,7 @@ interface Pool extends Link /** * @return int Maximum number of connections this pool will create. */ - public function getMaxConnections(): int; + public function getConnectionLimit(): int; /** * @return int Number of seconds a connection may remain idle before it is automatically closed. diff --git a/src/PooledResultSet.php b/src/PooledResultSet.php index 14fab65..671e00b 100644 --- a/src/PooledResultSet.php +++ b/src/PooledResultSet.php @@ -9,7 +9,7 @@ class PooledResultSet implements ResultSet /** @var ResultSet */ private $result; - /** @var callable|null */ + /** @var callable */ private $release; public function __construct(ResultSet $result, callable $release) diff --git a/src/PooledStatement.php b/src/PooledStatement.php index c3b1087..7c53ce6 100644 --- a/src/PooledStatement.php +++ b/src/PooledStatement.php @@ -74,8 +74,8 @@ abstract class PooledStatement implements Statement return $this->statement->getQuery(); } - public function lastUsedAt(): int + public function getLastUsedAt(): int { - return $this->statement->lastUsedAt(); + return $this->statement->getLastUsedAt(); } } diff --git a/src/PooledTransaction.php b/src/PooledTransaction.php index 67e54b3..0088860 100644 --- a/src/PooledTransaction.php +++ b/src/PooledTransaction.php @@ -123,13 +123,13 @@ abstract class PooledTransaction implements Transaction return $this->transaction && $this->transaction->isAlive(); } - public function lastUsedAt(): int + public function getLastUsedAt(): int { if (!$this->transaction) { throw new TransactionError("The transaction has been committed or rolled back"); } - return $this->transaction->lastUsedAt(); + return $this->transaction->getLastUsedAt(); } public function close() diff --git a/src/StatementPool.php b/src/StatementPool.php index b75aaa2..f6b7800 100644 --- a/src/StatementPool.php +++ b/src/StatementPool.php @@ -57,7 +57,7 @@ abstract class StatementPool implements Statement $statement = $statements->bottom(); \assert($statement instanceof Statement); - if ($statement->lastUsedAt() + $idleTimeout > $now) { + if ($statement->getLastUsedAt() + $idleTimeout > $now) { return; } @@ -113,7 +113,7 @@ abstract class StatementPool implements Statement */ protected function push(Statement $statement) { - $maxConnections = $this->pool->getMaxConnections(); + $maxConnections = $this->pool->getConnectionLimit(); if ($this->statements->count() > ($maxConnections / 10)) { return; @@ -160,7 +160,7 @@ abstract class StatementPool implements Statement } /** {@inheritdoc} */ - public function lastUsedAt(): int + public function getLastUsedAt(): int { return $this->lastUsedAt; } diff --git a/src/TransientResource.php b/src/TransientResource.php index 20f55bc..3eb5fa4 100644 --- a/src/TransientResource.php +++ b/src/TransientResource.php @@ -14,7 +14,7 @@ interface TransientResource /** * Get the timestamp of the last usage of this resource. * - * @return int + * @return int Unix timestamp in seconds. */ - public function lastUsedAt(): int; + public function getLastUsedAt(): int; } diff --git a/test/StatementPoolTest.php b/test/StatementPoolTest.php index 6d433a0..7005dd9 100644 --- a/test/StatementPoolTest.php +++ b/test/StatementPoolTest.php @@ -36,14 +36,14 @@ class StatementPoolTest extends TestCase ->getMockForAbstractClass(); $this->assertTrue($statementPool->isAlive()); - $this->assertSame(\time(), $statementPool->lastUsedAt()); + $this->assertSame(\time(), $statementPool->getLastUsedAt()); yield new Delayed(1500); // Give timeout watcher enough time to execute. $statementPool->execute(); $this->assertTrue($statementPool->isAlive()); - $this->assertSame(\time(), $statementPool->lastUsedAt()); + $this->assertSame(\time(), $statementPool->getLastUsedAt()); }); } @@ -72,7 +72,7 @@ class StatementPoolTest extends TestCase ->getMockForAbstractClass(); $this->assertTrue($statementPool->isAlive()); - $this->assertSame(\time(), $statementPool->lastUsedAt()); + $this->assertSame(\time(), $statementPool->getLastUsedAt()); $statementPool->execute(); @@ -81,7 +81,7 @@ class StatementPoolTest extends TestCase $statementPool->execute(); $this->assertTrue($statementPool->isAlive()); - $this->assertSame(\time(), $statementPool->lastUsedAt()); + $this->assertSame(\time(), $statementPool->getLastUsedAt()); }); } }