e
This commit is contained in:
+24
-24
@@ -1,24 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
trait Alias
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($cds, bool $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
} else {
|
||||
return $cds . '_slave';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
trait Alias
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($cds, bool $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
} else {
|
||||
return $cds . '_slave';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+217
-217
@@ -1,217 +1,217 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
use Closure;
|
||||
use Database\Mysql\PDO;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Context;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Error;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Connection
|
||||
* @package Kiri\Pool
|
||||
*/
|
||||
class Connection extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @return bool
|
||||
*
|
||||
* db is in transaction
|
||||
* @throws Exception
|
||||
*/
|
||||
public function inTransaction($cds): bool
|
||||
{
|
||||
$name = $this->name('Mysql:' . $cds, true);
|
||||
$connection = Context::getContext($name);
|
||||
if ($connection instanceof PDO) {
|
||||
return $connection->inTransaction();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function beginTransaction($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->beginTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function commit($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->commit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function rollback($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->rollBack();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return PDO|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): ?PDO
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $config['cds'], $isMaster);
|
||||
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
$minx = Config::get('databases.pool.min', 1);
|
||||
|
||||
/** @var PDO $connections */
|
||||
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
|
||||
if (Context::hasContext('begin_' . $coroutineName)) {
|
||||
$connections->beginTransaction();
|
||||
}
|
||||
return Context::setContext($coroutineName, $connections);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param $config
|
||||
* @return Closure
|
||||
*/
|
||||
public function create($coroutineName, $config): Closure
|
||||
{
|
||||
return static function () use ($coroutineName, $config) {
|
||||
return Kiri::getDi()->create(PDO::class, [$config]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release($coroutineName, $isMaster)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
|
||||
/** @var PDO $client */
|
||||
if (!($client = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return;
|
||||
}
|
||||
if ($client->inTransaction()) {
|
||||
return;
|
||||
}
|
||||
$this->getPool()->push($coroutineName, $client);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @return bool
|
||||
*/
|
||||
private function hasClient($coroutineName): bool
|
||||
{
|
||||
return Context::hasContext($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* batch release
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear($name, $isMaster)
|
||||
{
|
||||
$this->getPool()->clean($this->name($name, $isMaster));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
{
|
||||
try {
|
||||
if (empty($client) || !($client instanceof PDO)) {
|
||||
$result = false;
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
} catch (Error | Throwable $exception) {
|
||||
$result = $this->addError($exception, 'mysql');
|
||||
} finally {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function disconnect($coroutineName, bool $isMaster = false)
|
||||
{
|
||||
Context::remove($coroutineName);
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
use Closure;
|
||||
use Database\Mysql\PDO;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Context;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Error;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Connection
|
||||
* @package Kiri\Pool
|
||||
*/
|
||||
class Connection extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @return bool
|
||||
*
|
||||
* db is in transaction
|
||||
* @throws Exception
|
||||
*/
|
||||
public function inTransaction($cds): bool
|
||||
{
|
||||
$name = $this->name('Mysql:' . $cds, true);
|
||||
$connection = Context::getContext($name);
|
||||
if ($connection instanceof PDO) {
|
||||
return $connection->inTransaction();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function beginTransaction($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->beginTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function commit($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->commit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function rollback($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
|
||||
$connection = Context::getContext($coroutineName);
|
||||
if ($connection instanceof PDO) {
|
||||
$connection->rollBack();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return PDO|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): ?PDO
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $config['cds'], $isMaster);
|
||||
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
$minx = Config::get('databases.pool.min', 1);
|
||||
|
||||
/** @var PDO $connections */
|
||||
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
|
||||
if (Context::hasContext('begin_' . $coroutineName)) {
|
||||
$connections->beginTransaction();
|
||||
}
|
||||
return Context::setContext($coroutineName, $connections);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param $config
|
||||
* @return Closure
|
||||
*/
|
||||
public function create($coroutineName, $config): Closure
|
||||
{
|
||||
return static function () use ($coroutineName, $config) {
|
||||
return Kiri::getDi()->create(PDO::class, [$config]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release($coroutineName, $isMaster)
|
||||
{
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
|
||||
/** @var PDO $client */
|
||||
if (!($client = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return;
|
||||
}
|
||||
if ($client->inTransaction()) {
|
||||
return;
|
||||
}
|
||||
$this->getPool()->push($coroutineName, $client);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @return bool
|
||||
*/
|
||||
private function hasClient($coroutineName): bool
|
||||
{
|
||||
return Context::hasContext($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* batch release
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear($name, $isMaster)
|
||||
{
|
||||
$this->getPool()->clean($this->name($name, $isMaster));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
{
|
||||
try {
|
||||
if (empty($client) || !($client instanceof PDO)) {
|
||||
$result = false;
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
} catch (Error | Throwable $exception) {
|
||||
$result = $this->addError($exception, 'mysql');
|
||||
} finally {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function disconnect($coroutineName, bool $isMaster = false)
|
||||
{
|
||||
Context::remove($coroutineName);
|
||||
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
interface QueueInterface
|
||||
{
|
||||
|
||||
|
||||
public function isEmpty(): bool;
|
||||
|
||||
public function push(mixed $data, float $timeout = -1): bool;
|
||||
|
||||
|
||||
public function pop(float $timeout = -1): mixed;
|
||||
|
||||
|
||||
public function stats(): array;
|
||||
|
||||
|
||||
public function close(): bool;
|
||||
|
||||
|
||||
public function length(): int;
|
||||
|
||||
|
||||
public function isFull(): bool;
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
interface QueueInterface
|
||||
{
|
||||
|
||||
|
||||
public function isEmpty(): bool;
|
||||
|
||||
public function push(mixed $data, float $timeout = -1): bool;
|
||||
|
||||
|
||||
public function pop(float $timeout = -1): mixed;
|
||||
|
||||
|
||||
public function stats(): array;
|
||||
|
||||
|
||||
public function close(): bool;
|
||||
|
||||
|
||||
public function length(): int;
|
||||
|
||||
|
||||
public function isFull(): bool;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SplQueue implements QueueInterface
|
||||
{
|
||||
|
||||
private \SplQueue $channel;
|
||||
|
||||
|
||||
public int $errCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
*/
|
||||
#[Pure] public function __construct(public int $max)
|
||||
{
|
||||
$this->channel = new \SplQueue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
// TODO: Implement isEmpty() method.
|
||||
return $this->channel->count() < 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param float $timeout
|
||||
* @return bool
|
||||
*/
|
||||
public function push(mixed $data, float $timeout = -1): bool
|
||||
{
|
||||
// TODO: Implement push() method.
|
||||
$this->channel->enqueue($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param float $timeout
|
||||
* @return mixed
|
||||
*/
|
||||
public function pop(float $timeout = -1): mixed
|
||||
{
|
||||
// TODO: Implement pop() method.
|
||||
return $this->channel->dequeue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function stats(): array
|
||||
{
|
||||
// TODO: Implement stats() method.
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
// TODO: Implement close() method.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
// TODO: Implement length() method.
|
||||
return $this->channel->count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFull(): bool
|
||||
{
|
||||
// TODO: Implement isFull() method.
|
||||
return $this->channel->count() >= $this->max;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SplQueue implements QueueInterface
|
||||
{
|
||||
|
||||
private \SplQueue $channel;
|
||||
|
||||
|
||||
public int $errCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
*/
|
||||
#[Pure] public function __construct(public int $max)
|
||||
{
|
||||
$this->channel = new \SplQueue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
// TODO: Implement isEmpty() method.
|
||||
return $this->channel->count() < 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param float $timeout
|
||||
* @return bool
|
||||
*/
|
||||
public function push(mixed $data, float $timeout = -1): bool
|
||||
{
|
||||
// TODO: Implement push() method.
|
||||
$this->channel->enqueue($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param float $timeout
|
||||
* @return mixed
|
||||
*/
|
||||
public function pop(float $timeout = -1): mixed
|
||||
{
|
||||
// TODO: Implement pop() method.
|
||||
return $this->channel->dequeue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function stats(): array
|
||||
{
|
||||
// TODO: Implement stats() method.
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
// TODO: Implement close() method.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
// TODO: Implement length() method.
|
||||
return $this->channel->count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFull(): bool
|
||||
{
|
||||
// TODO: Implement isFull() method.
|
||||
return $this->channel->count() >= $this->max;
|
||||
}
|
||||
}
|
||||
|
||||
+249
-249
@@ -1,249 +1,249 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Pool\Helper\SplQueue;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
|
||||
/**
|
||||
* Class Pool
|
||||
* @package Kiri\Pool
|
||||
*/
|
||||
class Pool extends Component
|
||||
{
|
||||
|
||||
/** @var Channel[] */
|
||||
private static array $_connections = [];
|
||||
|
||||
public int $max = 60;
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
public function flush($channel, $retain_number)
|
||||
{
|
||||
$this->pop($channel, $retain_number);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Channel $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function pop(Channel $channel, $retain_number): void
|
||||
{
|
||||
while ($channel->length() > $retain_number) {
|
||||
if (Context::inCoroutine()) {
|
||||
$connection = $channel->pop();
|
||||
if ($connection instanceof StopHeartbeatCheck) {
|
||||
$connection->stopHeartbeatCheck();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function initConnections($name, bool $isMaster = false, int $max = 60)
|
||||
{
|
||||
$name = $this->name($name, $isMaster);
|
||||
if (isset(static::$_connections[$name])) {
|
||||
$value = static::$_connections[$name];
|
||||
if ($value instanceof Channel || $value instanceof SplQueue) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->newChannel($name, $max);
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Channel|SplQueue
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getChannel($name): Channel|SplQueue
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
$this->newChannel($name);
|
||||
}
|
||||
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) {
|
||||
throw new Exception('Channel is Close.');
|
||||
}
|
||||
return static::$_connections[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function newChannel($name, $max = null)
|
||||
{
|
||||
if ($max == null) {
|
||||
$max = Config::get('databases.pool.max', 10);
|
||||
}
|
||||
if (Coroutine::getCid() === -1) {
|
||||
static::$_connections[$name] = new SplQueue($max);
|
||||
} else {
|
||||
static::$_connections[$name] = new Channel($max);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $callback
|
||||
* @param $minx
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($name, $callback, $minx): mixed
|
||||
{
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isEmpty()) {
|
||||
return $this->maxIdleQuantity($channel, $minx);
|
||||
}
|
||||
return $callback();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $minx
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function maxIdleQuantity($channel, $minx): mixed
|
||||
{
|
||||
$connection = $channel->pop();
|
||||
if ($channel->length() > $minx) {
|
||||
$this->pop($channel, $minx);
|
||||
}
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function isNull($name): bool
|
||||
{
|
||||
return $this->getChannel($name)->isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* 检查连接可靠性
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset(static::$_connections[$name])) {
|
||||
return !static::$_connections[$name]->isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return static::$_connections[$name]->length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isFull()) {
|
||||
$channel->push($client);
|
||||
}
|
||||
unset($client);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clean(string $name)
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
return;
|
||||
}
|
||||
while (static::$_connections[$name]->length() > 0) {
|
||||
if (static::$_connections[$name] instanceof Channel)
|
||||
{
|
||||
if (!Context::inCoroutine())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
$client = static::$_connections[$name]->pop();
|
||||
if ($client instanceof StopHeartbeatCheck) {
|
||||
$client->stopHeartbeatCheck();
|
||||
}
|
||||
}
|
||||
static::$_connections[$name] = null;
|
||||
unset(static::$_connections[$name]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Channel[]
|
||||
*/
|
||||
protected function getChannels(): array
|
||||
{
|
||||
return static::$_connections;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Pool\Helper\SplQueue;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
|
||||
/**
|
||||
* Class Pool
|
||||
* @package Kiri\Pool
|
||||
*/
|
||||
class Pool extends Component
|
||||
{
|
||||
|
||||
/** @var Channel[] */
|
||||
private static array $_connections = [];
|
||||
|
||||
public int $max = 60;
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
public function flush($channel, $retain_number)
|
||||
{
|
||||
$this->pop($channel, $retain_number);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Channel $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function pop(Channel $channel, $retain_number): void
|
||||
{
|
||||
while ($channel->length() > $retain_number) {
|
||||
if (Context::inCoroutine()) {
|
||||
$connection = $channel->pop();
|
||||
if ($connection instanceof StopHeartbeatCheck) {
|
||||
$connection->stopHeartbeatCheck();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function initConnections($name, bool $isMaster = false, int $max = 60)
|
||||
{
|
||||
$name = $this->name($name, $isMaster);
|
||||
if (isset(static::$_connections[$name])) {
|
||||
$value = static::$_connections[$name];
|
||||
if ($value instanceof Channel || $value instanceof SplQueue) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->newChannel($name, $max);
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Channel|SplQueue
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getChannel($name): Channel|SplQueue
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
$this->newChannel($name);
|
||||
}
|
||||
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) {
|
||||
throw new Exception('Channel is Close.');
|
||||
}
|
||||
return static::$_connections[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function newChannel($name, $max = null)
|
||||
{
|
||||
if ($max == null) {
|
||||
$max = Config::get('databases.pool.max', 10);
|
||||
}
|
||||
if (Coroutine::getCid() === -1) {
|
||||
static::$_connections[$name] = new SplQueue($max);
|
||||
} else {
|
||||
static::$_connections[$name] = new Channel($max);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $callback
|
||||
* @param $minx
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($name, $callback, $minx): mixed
|
||||
{
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isEmpty()) {
|
||||
return $this->maxIdleQuantity($channel, $minx);
|
||||
}
|
||||
return $callback();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $minx
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function maxIdleQuantity($channel, $minx): mixed
|
||||
{
|
||||
$connection = $channel->pop();
|
||||
if ($channel->length() > $minx) {
|
||||
$this->pop($channel, $minx);
|
||||
}
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function isNull($name): bool
|
||||
{
|
||||
return $this->getChannel($name)->isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* 检查连接可靠性
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset(static::$_connections[$name])) {
|
||||
return !static::$_connections[$name]->isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return static::$_connections[$name]->length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isFull()) {
|
||||
$channel->push($client);
|
||||
}
|
||||
unset($client);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clean(string $name)
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
return;
|
||||
}
|
||||
while (static::$_connections[$name]->length() > 0) {
|
||||
if (static::$_connections[$name] instanceof Channel)
|
||||
{
|
||||
if (!Context::inCoroutine())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
$client = static::$_connections[$name]->pop();
|
||||
if ($client instanceof StopHeartbeatCheck) {
|
||||
$client->stopHeartbeatCheck();
|
||||
}
|
||||
}
|
||||
static::$_connections[$name] = null;
|
||||
unset(static::$_connections[$name]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Channel[]
|
||||
*/
|
||||
protected function getChannels(): array
|
||||
{
|
||||
return static::$_connections;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+122
-122
@@ -1,122 +1,122 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Context;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
/**
|
||||
* Class RedisClient
|
||||
* @package Kiri\Kiri\Pool
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
if (Context::hasContext($coroutineName)) {
|
||||
return Context::getContext($coroutineName);
|
||||
}
|
||||
|
||||
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
|
||||
|
||||
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1);
|
||||
return Context::setContext($coroutineName, $clients);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return Closure
|
||||
*/
|
||||
public function create(string $name, mixed $config): Closure
|
||||
{
|
||||
return static function () use ($name, $config) {
|
||||
return Kiri::getDi()->create(\Kiri\Cache\Base\Redis::class, [$config]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
if (!Context::hasContext($coroutineName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Context;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
/**
|
||||
* Class RedisClient
|
||||
* @package Kiri\Kiri\Pool
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
if (Context::hasContext($coroutineName)) {
|
||||
return Context::getContext($coroutineName);
|
||||
}
|
||||
|
||||
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
|
||||
|
||||
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1);
|
||||
return Context::setContext($coroutineName, $clients);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return Closure
|
||||
*/
|
||||
public function create(string $name, mixed $config): Closure
|
||||
{
|
||||
return static function () use ($name, $config) {
|
||||
return Kiri::getDi()->create(\Kiri\Cache\Base\Redis::class, [$config]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
if (!Context::hasContext($coroutineName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
interface StopHeartbeatCheck
|
||||
{
|
||||
|
||||
|
||||
public function stopHeartbeatCheck();
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
interface StopHeartbeatCheck
|
||||
{
|
||||
|
||||
|
||||
public function stopHeartbeatCheck();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user