Skip to content

Commit

Permalink
Merge pull request #2 from fising/master
Browse files Browse the repository at this point in the history
Validate the connection when it is returned.
  • Loading branch information
hhxsv5 committed May 28, 2019
2 parents a727a72 + 1520f2e commit efea7c1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().');
}
Expand Down
8 changes: 8 additions & 0 deletions src/Connectors/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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): bool;
}
5 changes: 5 additions & 0 deletions src/Connectors/CoroutineMySQLConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public function reset($connection, array $config)
{

}

public function validate($connection): bool
{
return $connection instanceof MySQL;
}
}
5 changes: 5 additions & 0 deletions src/Connectors/CoroutinePostgreSQLConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function reset($connection, array $config)
{
/**@var PostgreSQL $connection */
}

public function validate($connection): bool
{
return $connection instanceof PostgreSQL;
}
}
5 changes: 5 additions & 0 deletions src/Connectors/CoroutineRedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function reset($connection, array $config)
}
$connection->setDefer(false);
}

public function validate($connection): bool
{
return $connection instanceof Redis;
}
}
5 changes: 5 additions & 0 deletions src/Connectors/PhpRedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function reset($connection, array $config)
$connection->select($config['database']);
}
}

public function validate($connection): bool
{
return $connection instanceof \Redis;
}
}

0 comments on commit efea7c1

Please sign in to comment.