This commit is contained in:
2021-04-23 18:37:18 +08:00
parent 9b0f919ba9
commit 8cd5562b20
3 changed files with 202 additions and 171 deletions
+59 -10
View File
@@ -28,6 +28,36 @@ abstract class Pool extends Component
public int $lastTime = 0;
protected array $hasCreate = [];
/**
* @param string $name
*/
public function increment(string $name)
{
if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0;
}
$this->hasCreate[$name] += 1;
}
/**
* @param string $name
*/
public function decrement(string $name)
{
if (!isset($this->hasCreate[$name])) {
return;
}
if ($this->hasCreate[$name] <= 0) {
return;
}
$this->hasCreate[$name] -= 1;
}
/**
* @return array
* @throws ConfigException
@@ -77,6 +107,18 @@ abstract class Pool extends Component
}
/**
* @param $name
*/
protected function clearCreateLog($name)
{
if (!isset($this->hasCreate[$name])) {
return;
}
$this->hasCreate[$name] = 0;
}
/**
* @param $channel
* @param $name
@@ -90,7 +132,7 @@ abstract class Pool extends Component
if ($connection) {
unset($connection);
}
$this->desc($name);
$this->decrement($name);
}
}
@@ -145,6 +187,9 @@ abstract class Pool extends Component
*/
private function createByCallback($name, mixed $callback)
{
if (!$this->canCreate($name)) {
return;
}
if ($this->creates === -1 && !is_callable($callback)) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
}
@@ -182,15 +227,6 @@ abstract class Pool extends Component
return true;
}
/**
* @param $name
* @throws Exception
*/
public function desc(string $name)
{
throw new Exception('Undefined system processing function.');
}
/**
* @param array $config
@@ -204,6 +240,19 @@ abstract class Pool extends Component
}
/**
* @param string $name
* @return bool
*/
public function canCreate(string $name): bool
{
if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0;
}
return $this->hasCreate[$name] >= $this->max;
}
/**
* @param $name
* @return bool
+13 -37
View File
@@ -3,11 +3,11 @@ declare(strict_types=1);
namespace Snowflake\Pool;
use Exception;
use HttpServer\Http\Context;
use PDO;
use Exception;
use Swoole\Coroutine;
use Snowflake\Abstracts\Pool;
use Swoole\Coroutine;
use Swoole\Error;
use Throwable;
@@ -19,8 +19,6 @@ class Connection extends Pool
{
public array $hasCreate = [];
public int $timeout = 1900;
/**
@@ -133,9 +131,6 @@ class Connection extends Pool
public function get(mixed $config, $isMaster = false): mixed
{
$coroutineName = $this->name('mysql', $config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo;
}
@@ -167,6 +162,9 @@ class Connection extends Pool
if (!empty($charset)) {
$link->query('SET NAMES ' . $charset);
}
$this->increment($name);
return $link;
}
@@ -178,7 +176,6 @@ class Connection extends Pool
*/
public function printClients($cds, $coroutineName, $isBefore = false)
{
// $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']');
}
@@ -243,19 +240,19 @@ class Connection extends Pool
{
try {
if (empty($client) || !($client instanceof PDO)) {
return $result = false;
$result = false;
} else if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) {
$result = false;
} else {
$result = true;
}
if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) {
return $result = false;
}
return $result = true;
} catch (Error | Throwable $exception) {
$this->addError($exception, 'mysql');
return $result = false;
$result = $this->addError($exception, 'mysql');
} finally {
if (!$result) {
$this->desc($name);
$this->decrement($name);
}
return $result;
}
}
@@ -274,25 +271,4 @@ class Connection extends Pool
$this->clean($coroutineName);
}
/**
* @param $coroutineName
*/
public function incr($coroutineName)
{
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
$this->hasCreate[$coroutineName] += 1;
}
/**
* @param string $name
*/
public function desc(string $name)
{
if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0;
}
$this->hasCreate[$name] -= 1;
}
}
+18 -12
View File
@@ -5,11 +5,11 @@ declare(strict_types=1);
namespace Snowflake\Pool;
use Exception;
use HttpServer\Http\Context;
use Redis as SRedis;
use Snowflake\Exception\RedisConnectException;
use Exception;
use Snowflake\Abstracts\Pool;
use Snowflake\Exception\RedisConnectException;
/**
* Class RedisClient
@@ -30,6 +30,19 @@ class Redis extends Pool
}
/**
* @param string $name
* @return bool
*/
public function canCreate(string $name): bool
{
if (!isset($this->hasCreate[$name])) {
$this->hasCreate[$name] = 0;
}
return $this->hasCreate[$name] >= $this->max;
}
/**
* @param mixed $config
* @param bool $isMaster
@@ -70,7 +83,7 @@ class Redis extends Pool
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
$this->_create += 1;
$this->increment($name);
return $redis;
}
@@ -103,7 +116,7 @@ class Redis extends Pool
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (Context::hasContext($coroutineName)) {
$this->desc($coroutineName);
$this->decrement($coroutineName);
$this->remove($coroutineName);
}
@@ -139,19 +152,12 @@ class Redis extends Pool
$result = false;
} finally {
if (!$result) {
$this->desc($name);
$this->decrement($name);
}
return $result;
}
}
/**
* @param string $name
*/
public function desc(string $name)
{
$this->_create -= 1;
}
}