This commit is contained in:
2021-03-08 18:45:32 +08:00
parent b2693aa005
commit a2542b7ba1
4 changed files with 35 additions and 37 deletions
+9 -7
View File
@@ -6,11 +6,10 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context;
use PDO;
use Exception;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Swoole\Coroutine;
use Snowflake\Abstracts\Pool;
use Swoole\Timer;
use Swoole\Error;
use Throwable;
/**
* Class Connection
@@ -239,12 +238,14 @@ class Connection extends Pool
Context::remove($coroutineName);
}
/**
* @param $name
* @param $client
* @param string $name
* @param mixed $client
* @return bool
* @throws Exception
*/
public function checkCanUse($name, $client): bool
public function checkCanUse(string $name, mixed $client): bool
{
try {
if (empty($client) || !($client instanceof PDO)) {
@@ -254,7 +255,8 @@ class Connection extends Pool
return $result = false;
}
return $result = true;
} catch (\Swoole\Error | \Throwable $exception) {
} catch (Error | Throwable $exception) {
$this->addError($exception, 'mysql');
return $result = false;
} finally {
if (!$result) {
+8 -12
View File
@@ -5,7 +5,10 @@ declare(strict_types=1);
namespace Snowflake\Pool;
use ReflectionException;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
@@ -15,12 +18,14 @@ use Snowflake\Snowflake;
* @property Connection $db
* @property $memcached
*/
class Pool extends \Snowflake\Abstracts\Pool
class Pool extends BaseObject
{
/**
* @return Redis
* @throws ComponentException
* @throws ReflectionException
* @throws NotFindClassException
*/
public function getRedis(): Redis
{
@@ -30,21 +35,12 @@ class Pool extends \Snowflake\Abstracts\Pool
/**
* @return Connection
* @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
*/
public function getDb(): Connection
{
return Snowflake::app()->get('connections');
}
/**
* @param string $name
* @param mixed $config
* @return mixed
*/
public function createClient(string $name, mixed $config): mixed
{
// TODO: Implement createClient() method.
return null;
}
}
+11 -13
View File
@@ -7,12 +7,10 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context;
use Redis as SRedis;
use RedisException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine;
use Exception;
use Swoole\Timer;
use Snowflake\Abstracts\Pool;
/**
* Class RedisClient
@@ -54,11 +52,12 @@ class Redis extends Pool
* @param string $name
* @param mixed $config
* @return SRedis
* @throws RedisConnectException|ComponentException
* @throws RedisConnectException
* @throws ComponentException
*/
public function createClient(string $name, mixed $config): SRedis
{
// $this->printClients($config['host'], $name, true);
$this->printClients($config['host'], $name, true);
$redis = new SRedis();
if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port']));
@@ -120,13 +119,12 @@ class Redis extends Pool
Context::remove($coroutineName);
}
/**
* @param $name
* @param $time
* @param $client
* @return bool
* @throws Exception
*/
/**
* @param string $name
* @param mixed $client
* @return bool
* @throws Exception
*/
public function checkCanUse(string $name, mixed $client): bool
{
try {
@@ -138,7 +136,7 @@ class Redis extends Pool
$result = true;
}
} catch (\Throwable $exception) {
$this->error($exception);
$this->addError($exception, 'redis');
$result = false;
} finally {
if (!$result) {