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
+7 -5
View File
@@ -12,8 +12,10 @@ namespace Snowflake\Abstracts;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
@@ -128,7 +130,7 @@ class BaseObject implements Configure
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws ComponentException * @throws Exception
*/ */
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{ {
@@ -147,7 +149,7 @@ class BaseObject implements Configure
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws ComponentException * @throws Exception
*/ */
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{ {
@@ -165,7 +167,7 @@ class BaseObject implements Configure
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws ComponentException * @throws Exception
*/ */
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{ {
@@ -185,7 +187,7 @@ class BaseObject implements Configure
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws ComponentException * @throws Exception
*/ */
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{ {
@@ -206,7 +208,7 @@ class BaseObject implements Configure
* @param mixed $message * @param mixed $message
* @param null $method * @param null $method
* @param null $file * @param null $file
* @throws ComponentException * @throws Exception
*/ */
public function error(mixed $message, $method = null, $file = null) public function error(mixed $message, $method = null, $file = null)
{ {
+9 -7
View File
@@ -6,11 +6,10 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use PDO; use PDO;
use Exception; use Exception;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Timer; use Swoole\Error;
use Throwable;
/** /**
* Class Connection * Class Connection
@@ -239,12 +238,14 @@ class Connection extends Pool
Context::remove($coroutineName); Context::remove($coroutineName);
} }
/** /**
* @param $name * @param string $name
* @param $client * @param mixed $client
* @return bool * @return bool
* @throws Exception
*/ */
public function checkCanUse($name, $client): bool public function checkCanUse(string $name, mixed $client): bool
{ {
try { try {
if (empty($client) || !($client instanceof PDO)) { if (empty($client) || !($client instanceof PDO)) {
@@ -254,7 +255,8 @@ class Connection extends Pool
return $result = false; return $result = false;
} }
return $result = true; return $result = true;
} catch (\Swoole\Error | \Throwable $exception) { } catch (Error | Throwable $exception) {
$this->addError($exception, 'mysql');
return $result = false; return $result = false;
} finally { } finally {
if (!$result) { if (!$result) {
+8 -12
View File
@@ -5,7 +5,10 @@ declare(strict_types=1);
namespace Snowflake\Pool; namespace Snowflake\Pool;
use ReflectionException;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -15,12 +18,14 @@ use Snowflake\Snowflake;
* @property Connection $db * @property Connection $db
* @property $memcached * @property $memcached
*/ */
class Pool extends \Snowflake\Abstracts\Pool class Pool extends BaseObject
{ {
/** /**
* @return Redis * @return Redis
* @throws ComponentException * @throws ComponentException
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function getRedis(): Redis public function getRedis(): Redis
{ {
@@ -30,21 +35,12 @@ class Pool extends \Snowflake\Abstracts\Pool
/** /**
* @return Connection * @return Connection
* @throws ComponentException * @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
*/ */
public function getDb(): Connection public function getDb(): Connection
{ {
return Snowflake::app()->get('connections'); 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 HttpServer\Http\Context;
use Redis as SRedis; use Redis as SRedis;
use RedisException;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\RedisConnectException; use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine;
use Exception; use Exception;
use Swoole\Timer; use Snowflake\Abstracts\Pool;
/** /**
* Class RedisClient * Class RedisClient
@@ -54,11 +52,12 @@ class Redis extends Pool
* @param string $name * @param string $name
* @param mixed $config * @param mixed $config
* @return SRedis * @return SRedis
* @throws RedisConnectException|ComponentException * @throws RedisConnectException
* @throws ComponentException
*/ */
public function createClient(string $name, mixed $config): SRedis public function createClient(string $name, mixed $config): SRedis
{ {
// $this->printClients($config['host'], $name, true); $this->printClients($config['host'], $name, true);
$redis = new SRedis(); $redis = new SRedis();
if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) { if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); 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); Context::remove($coroutineName);
} }
/** /**
* @param $name * @param string $name
* @param $time * @param mixed $client
* @param $client * @return bool
* @return bool * @throws Exception
* @throws Exception */
*/
public function checkCanUse(string $name, mixed $client): bool public function checkCanUse(string $name, mixed $client): bool
{ {
try { try {
@@ -138,7 +136,7 @@ class Redis extends Pool
$result = true; $result = true;
} }
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->error($exception); $this->addError($exception, 'redis');
$result = false; $result = false;
} finally { } finally {
if (!$result) { if (!$result) {