Compare commits

..

6 Commits

Author SHA1 Message Date
as2252258 c5b4f4795b Revert "改名"
This reverts commit fdf58326
2022-01-14 14:15:34 +08:00
as2252258 661206b94b Revert "改名"
This reverts commit fdf58326
2022-01-14 11:29:15 +08:00
as2252258 61f589058e Revert "改名"
This reverts commit fdf58326
2022-01-13 16:32:44 +08:00
as2252258 6d83f647f4 Revert "改名"
This reverts commit fdf58326
2022-01-12 17:51:00 +08:00
as2252258 870595b810 Revert "改名"
This reverts commit fdf58326
2022-01-12 17:49:02 +08:00
as2252258 970f22804d Revert "改名"
This reverts commit fdf58326
2022-01-12 17:48:48 +08:00
3 changed files with 32 additions and 16 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ use Exception;
* Class Get
* @package Annotation\Model
*/
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Kiri\Annotation\Attribute
{
+3 -5
View File
@@ -10,8 +10,7 @@ use Kiri\Abstracts\Providers;
use Kiri\Application;
use Kiri\Exception\ConfigException;
use Kiri;
use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Events\OnBeforeCommandExecute;
/**
* Class DatabasesProviders
@@ -27,8 +26,7 @@ class DatabasesProviders extends Providers
*/
public function onImport(Application $application)
{
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
$this->eventProvider->on(OnTaskerStart::class, [$this, 'createPool']);
$this->eventProvider->on(OnBeforeCommandExecute::class, [$this, 'createPool']);
}
@@ -47,7 +45,7 @@ class DatabasesProviders extends Providers
* @throws ConfigException
* @throws Exception
*/
public function createPool(OnWorkerStart|OnTaskerStart $onWorkerStart)
public function createPool(OnBeforeCommandExecute $onWorkerStart)
{
$databases = Config::get('databases.connections', []);
if (empty($databases)) {
+28 -10
View File
@@ -3,10 +3,11 @@
namespace Database\Mysql;
use Exception;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Logger;
use Kiri;
use Kiri\Abstracts\Config;
use Kiri\Events\EventProvider;
use Kiri\Pool\StopHeartbeatCheck;
use Kiri\Server\Events\OnWorkerExit;
use PDOStatement;
use Swoole\Timer;
@@ -38,6 +39,9 @@ class PDO implements StopHeartbeatCheck
public int $read_timeout;
private EventProvider $eventProvider;
public array $attributes = [];
@@ -57,9 +61,14 @@ class PDO implements StopHeartbeatCheck
}
public function init()
/**
* @return void
*/
public function init(): void
{
$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
{
if (env('state', 'start') == 'exit') {
return;
}
if ($this->_timer === -1) {
$this->_timer = Timer::tick(1000, fn() => $this->waite());
}
@@ -92,12 +108,14 @@ class PDO implements StopHeartbeatCheck
private function waite(): void
{
try {
if (env('state', 'start') == 'exit') {
Kiri::getDi()->get(Logger::class)->critical('timer end');
if ($this->_timer == -1) {
$this->stopHeartbeatCheck();
}
if (time() - $this->_last > (int)Config::get('databases.pool.tick', 60)) {
$this->stopHeartbeatCheck();
$this->eventProvider->off(OnWorkerExit::class, [$this, 'stopHeartbeatCheck']);
$this->pdo = null;
}
} catch (\Throwable $throwable) {
@@ -232,7 +250,7 @@ class PDO implements StopHeartbeatCheck
throw new Exception($this->_pdo()->errorInfo()[1]);
}
return $this->bindValue($statement, $params);
} catch (\PDOException | \Throwable $throwable) {
} catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->pdo = null;
@@ -313,7 +331,7 @@ class PDO implements StopHeartbeatCheck
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$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) {
$link->setAttribute($key, $attribute);
}