From 6a0d1292fb32578d756877b62ba9fa77c49512b6 Mon Sep 17 00:00:00 2001 From: fising Date: Tue, 28 May 2019 11:14:00 +0800 Subject: [PATCH 1/3] Validate the connection when it is returned. --- src/ConnectionPool.php | 4 ++++ src/Connectors/ConnectorInterface.php | 8 ++++++++ src/Connectors/CoroutineMySQLConnector.php | 5 +++++ src/Connectors/CoroutinePostgreSQLConnector.php | 5 +++++ src/Connectors/CoroutineRedisConnector.php | 5 +++++ src/Connectors/PhpRedisConnector.php | 5 +++++ 6 files changed, 32 insertions(+) diff --git a/src/ConnectionPool.php b/src/ConnectionPool.php index 5abb2d6..3c46b49 100644 --- a/src/ConnectionPool.php +++ b/src/ConnectionPool.php @@ -148,6 +148,10 @@ public function borrow() */ public function return($connection): bool { + if (!$this->connector->validate($connection)) { + throw new \RuntimeException('Connection of unexpected type returned.'); + } + if (!$this->initialized) { throw new \RuntimeException('Please initialize the connection pool first, call $pool->init().'); } diff --git a/src/Connectors/ConnectorInterface.php b/src/Connectors/ConnectorInterface.php index 538f571..826037f 100644 --- a/src/Connectors/ConnectorInterface.php +++ b/src/Connectors/ConnectorInterface.php @@ -32,4 +32,12 @@ public function isConnected($connection): bool; * @return mixed */ public function reset($connection, array $config); + + /** + * Validate the connection + * + * @param mixed $connection + * @return bool + */ + public function validate($connection); } \ No newline at end of file diff --git a/src/Connectors/CoroutineMySQLConnector.php b/src/Connectors/CoroutineMySQLConnector.php index dcc51fe..26d30b8 100644 --- a/src/Connectors/CoroutineMySQLConnector.php +++ b/src/Connectors/CoroutineMySQLConnector.php @@ -31,4 +31,9 @@ public function reset($connection, array $config) { } + + public function validate($connection) + { + return $connection instanceof MySQL; + } } \ No newline at end of file diff --git a/src/Connectors/CoroutinePostgreSQLConnector.php b/src/Connectors/CoroutinePostgreSQLConnector.php index 424efae..03f2e30 100644 --- a/src/Connectors/CoroutinePostgreSQLConnector.php +++ b/src/Connectors/CoroutinePostgreSQLConnector.php @@ -34,4 +34,9 @@ public function reset($connection, array $config) { /**@var PostgreSQL $connection */ } + + public function validate($connection) + { + return $connection instanceof PostgreSQL; + } } \ No newline at end of file diff --git a/src/Connectors/CoroutineRedisConnector.php b/src/Connectors/CoroutineRedisConnector.php index 635b9ad..75be847 100644 --- a/src/Connectors/CoroutineRedisConnector.php +++ b/src/Connectors/CoroutineRedisConnector.php @@ -45,4 +45,9 @@ public function reset($connection, array $config) } $connection->setDefer(false); } + + public function validate($connection) + { + return $connection instanceof Redis; + } } \ No newline at end of file diff --git a/src/Connectors/PhpRedisConnector.php b/src/Connectors/PhpRedisConnector.php index 6e38da3..63d35d3 100644 --- a/src/Connectors/PhpRedisConnector.php +++ b/src/Connectors/PhpRedisConnector.php @@ -45,4 +45,9 @@ public function reset($connection, array $config) $connection->select($config['database']); } } + + public function validate($connection) + { + return $connection instanceof \Redis; + } } \ No newline at end of file From 0cee64a5d77cc069728d42c526e5a4527f44c09e Mon Sep 17 00:00:00 2001 From: fising Date: Tue, 28 May 2019 11:44:49 +0800 Subject: [PATCH 2/3] Validate the connection when it is returned. --- src/Connectors/ConnectorInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Connectors/ConnectorInterface.php b/src/Connectors/ConnectorInterface.php index 826037f..e04e946 100644 --- a/src/Connectors/ConnectorInterface.php +++ b/src/Connectors/ConnectorInterface.php @@ -39,5 +39,5 @@ public function reset($connection, array $config); * @param mixed $connection * @return bool */ - public function validate($connection); + public function validate($connection): bool; } \ No newline at end of file From 1520f2e293d479df36470ecf89086b5ef590efd6 Mon Sep 17 00:00:00 2001 From: fising Date: Tue, 28 May 2019 11:46:03 +0800 Subject: [PATCH 3/3] Validate the connection when it is returned. --- src/Connectors/CoroutineMySQLConnector.php | 2 +- src/Connectors/CoroutinePostgreSQLConnector.php | 2 +- src/Connectors/CoroutineRedisConnector.php | 2 +- src/Connectors/PhpRedisConnector.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Connectors/CoroutineMySQLConnector.php b/src/Connectors/CoroutineMySQLConnector.php index 26d30b8..7b01c73 100644 --- a/src/Connectors/CoroutineMySQLConnector.php +++ b/src/Connectors/CoroutineMySQLConnector.php @@ -32,7 +32,7 @@ public function reset($connection, array $config) } - public function validate($connection) + public function validate($connection): bool { return $connection instanceof MySQL; } diff --git a/src/Connectors/CoroutinePostgreSQLConnector.php b/src/Connectors/CoroutinePostgreSQLConnector.php index 03f2e30..d3b6930 100644 --- a/src/Connectors/CoroutinePostgreSQLConnector.php +++ b/src/Connectors/CoroutinePostgreSQLConnector.php @@ -35,7 +35,7 @@ public function reset($connection, array $config) /**@var PostgreSQL $connection */ } - public function validate($connection) + public function validate($connection): bool { return $connection instanceof PostgreSQL; } diff --git a/src/Connectors/CoroutineRedisConnector.php b/src/Connectors/CoroutineRedisConnector.php index 75be847..968e0a8 100644 --- a/src/Connectors/CoroutineRedisConnector.php +++ b/src/Connectors/CoroutineRedisConnector.php @@ -46,7 +46,7 @@ public function reset($connection, array $config) $connection->setDefer(false); } - public function validate($connection) + public function validate($connection): bool { return $connection instanceof Redis; } diff --git a/src/Connectors/PhpRedisConnector.php b/src/Connectors/PhpRedisConnector.php index 63d35d3..bbcb11a 100644 --- a/src/Connectors/PhpRedisConnector.php +++ b/src/Connectors/PhpRedisConnector.php @@ -46,7 +46,7 @@ public function reset($connection, array $config) } } - public function validate($connection) + public function validate($connection): bool { return $connection instanceof \Redis; }