Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c5b4f4795b | |||
| 661206b94b | |||
| 61f589058e | |||
| 6d83f647f4 | |||
| 870595b810 | |||
| 970f22804d | |||
| dad6a63ee2 | |||
| 47ed967e46 |
+1
-1
@@ -13,7 +13,7 @@ use Exception;
|
|||||||
* Class Get
|
* Class Get
|
||||||
* @package Annotation\Model
|
* @package Annotation\Model
|
||||||
*/
|
*/
|
||||||
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
|
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Kiri\Annotation\Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@ use Database\Traits\HasBase;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use ReturnTypeWillChange;
|
use ReturnTypeWillChange;
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ use Kiri\Abstracts\Component;
|
|||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\Annotation\Inject;
|
use Kiri\Annotation\Inject;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Kiri\Server\Events\OnWorkerExit;
|
use Kiri\Server\Events\OnWorkerExit;
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use Kiri\Abstracts\Config;
|
|||||||
use Kiri\Abstracts\Providers;
|
use Kiri\Abstracts\Providers;
|
||||||
use Kiri\Application;
|
use Kiri\Application;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\Server\Events\OnWorkerStart;
|
use Kiri\Events\OnBeforeCommandExecute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DatabasesProviders
|
* Class DatabasesProviders
|
||||||
@@ -26,7 +26,7 @@ class DatabasesProviders extends Providers
|
|||||||
*/
|
*/
|
||||||
public function onImport(Application $application)
|
public function onImport(Application $application)
|
||||||
{
|
{
|
||||||
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
$this->eventProvider->on(OnBeforeCommandExecute::class, [$this, 'createPool']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ class DatabasesProviders extends Providers
|
|||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function createPool(OnWorkerStart $onWorkerStart)
|
public function createPool(OnBeforeCommandExecute $onWorkerStart)
|
||||||
{
|
{
|
||||||
$databases = Config::get('databases.connections', []);
|
$databases = Config::get('databases.connections', []);
|
||||||
if (empty($databases)) {
|
if (empty($databases)) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use Database\Base\Getter;
|
|||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
|
|||||||
+28
-10
@@ -3,10 +3,11 @@
|
|||||||
namespace Database\Mysql;
|
namespace Database\Mysql;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Kiri;
|
|
||||||
use Kiri\Pool\StopHeartbeatCheck;
|
use Kiri\Pool\StopHeartbeatCheck;
|
||||||
|
use Kiri\Server\Events\OnWorkerExit;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
|
|
||||||
@@ -38,6 +39,9 @@ class PDO implements StopHeartbeatCheck
|
|||||||
public int $read_timeout;
|
public int $read_timeout;
|
||||||
|
|
||||||
|
|
||||||
|
private EventProvider $eventProvider;
|
||||||
|
|
||||||
|
|
||||||
public array $attributes = [];
|
public array $attributes = [];
|
||||||
|
|
||||||
|
|
||||||
@@ -57,9 +61,14 @@ class PDO implements StopHeartbeatCheck
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function init()
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function init(): void
|
||||||
{
|
{
|
||||||
$this->heartbeat_check();
|
$this->heartbeat_check();
|
||||||
|
$this->eventProvider = Kiri::getDi()->get(EventProvider::class);
|
||||||
|
$this->eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -72,14 +81,21 @@ class PDO implements StopHeartbeatCheck
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Kiri\Server\Events\OnWorkerExit $exit
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onWorkerExit(OnWorkerExit $exit)
|
||||||
|
{
|
||||||
|
$this->stopHeartbeatCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function heartbeat_check(): void
|
public function heartbeat_check(): void
|
||||||
{
|
{
|
||||||
if (env('state', 'start') == 'exit') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ($this->_timer === -1) {
|
if ($this->_timer === -1) {
|
||||||
$this->_timer = Timer::tick(1000, fn() => $this->waite());
|
$this->_timer = Timer::tick(1000, fn() => $this->waite());
|
||||||
}
|
}
|
||||||
@@ -92,12 +108,14 @@ class PDO implements StopHeartbeatCheck
|
|||||||
private function waite(): void
|
private function waite(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (env('state', 'start') == 'exit') {
|
if ($this->_timer == -1) {
|
||||||
Kiri::getDi()->get(Logger::class)->critical('timer end');
|
|
||||||
$this->stopHeartbeatCheck();
|
$this->stopHeartbeatCheck();
|
||||||
}
|
}
|
||||||
if (time() - $this->_last > (int)Config::get('databases.pool.tick', 60)) {
|
if (time() - $this->_last > (int)Config::get('databases.pool.tick', 60)) {
|
||||||
$this->stopHeartbeatCheck();
|
$this->stopHeartbeatCheck();
|
||||||
|
|
||||||
|
$this->eventProvider->off(OnWorkerExit::class, [$this, 'stopHeartbeatCheck']);
|
||||||
|
|
||||||
$this->pdo = null;
|
$this->pdo = null;
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
@@ -232,7 +250,7 @@ class PDO implements StopHeartbeatCheck
|
|||||||
throw new Exception($this->_pdo()->errorInfo()[1]);
|
throw new Exception($this->_pdo()->errorInfo()[1]);
|
||||||
}
|
}
|
||||||
return $this->bindValue($statement, $params);
|
return $this->bindValue($statement, $params);
|
||||||
} catch (\PDOException | \Throwable $throwable) {
|
} catch (\PDOException|\Throwable $throwable) {
|
||||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||||
$this->pdo = null;
|
$this->pdo = null;
|
||||||
|
|
||||||
@@ -313,7 +331,7 @@ class PDO implements StopHeartbeatCheck
|
|||||||
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
||||||
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
||||||
if (!empty($this->attributes) && is_array($this->attributes)) {
|
if (!empty($this->attributes)) {
|
||||||
foreach ($this->attributes as $key => $attribute) {
|
foreach ($this->attributes as $key => $attribute) {
|
||||||
$link->setAttribute($key, $attribute);
|
$link->setAttribute($key, $attribute);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ use Database\Query;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use Database\Query;
|
|||||||
use Database\SqlBuilder;
|
use Database\SqlBuilder;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user