This commit is contained in:
2021-03-02 18:33:12 +08:00
parent 0eee8d6cff
commit b2fea0250a
4 changed files with 160 additions and 118 deletions
+39
View File
@@ -4,13 +4,17 @@ declare(strict_types=1);
namespace HttpServer\Abstracts; namespace HttpServer\Abstracts;
use Database\Connection;
use Exception; use Exception;
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\SMTP;
use ReflectionException;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine\Server; use Swoole\Coroutine\Server;
use Swoole\Timer; use Swoole\Timer;
@@ -132,4 +136,39 @@ abstract class Callback extends HttpService
} }
} }
/**
* @throws ConfigException
* @throws ComponentException
* @throws Exception
*/
protected function clearMysqlClient()
{
$databases = Config::get('databases', false, []);
if (empty($databases)) {
return;
}
$application = Snowflake::app();
foreach ($databases as $name => $database) {
/** @var Connection $connection */
$connection = $application->get('databases.' . $name, false);
if (empty($connection)) {
continue;
}
$connection->disconnect();
}
}
/**
* @throws ConfigException
* @throws ComponentException
* @throws Exception
*/
protected function clearRedisClient()
{
$redis = Snowflake::app()->getRedis();
$redis->destroy();
}
} }
+3
View File
@@ -25,6 +25,9 @@ class OnBeforeReload extends Callback
{ {
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]); $event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
$this->clearMysqlClient();
$this->clearRedisClient();
} }
} }
+4 -9
View File
@@ -146,19 +146,13 @@ abstract class Pool extends Component
/** /**
* @param $name * @param $name
* @param mixed $callback * @param mixed $callback
* @throws ComponentException
* @throws Exception * @throws Exception
*/ */
private function createByCallback($name, mixed $callback) private function createByCallback($name, mixed $callback)
{ {
// if ($this->creates === -1 && !is_callable($callback)) { if ($this->creates === -1 && !is_callable($callback)) {
// $this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
// }
// $event = Snowflake::app()->getEvent();
// $event->on(Event::SERVER_WORKER_STOP, function () use ($creates) {
// Timer::clear($creates);
// });
// }
if (!Context::hasContext('create::client::ing::' . $name)) { if (!Context::hasContext('create::client::ing::' . $name)) {
$this->push($name, $this->createClient($name, $callback)); $this->push($name, $this->createClient($name, $callback));
Context::remove('create::client::ing::' . $name); Context::remove('create::client::ing::' . $name);
@@ -283,6 +277,7 @@ abstract class Pool extends Component
*/ */
public function clean(string $name) public function clean(string $name)
{ {
Timer::clear($this->creates);
if (!Context::inCoroutine() || !isset($this->_items[$name])) { if (!Context::inCoroutine() || !isset($this->_items[$name])) {
return; return;
} }
+8 -3
View File
@@ -34,16 +34,21 @@ class Service extends Component
/** /**
* @param $id * @param $id
* * @param bool $try
* @return mixed * @return mixed
* @throws * @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
*/ */
public function get($id): mixed public function get($id, $try = true): mixed
{ {
if (isset($this->_components[$id])) { if (isset($this->_components[$id])) {
return $this->_components[$id]; return $this->_components[$id];
} }
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) { if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
if ($try === false) {
return null;
}
throw new ComponentException("Unknown component ID: $id"); throw new ComponentException("Unknown component ID: $id");
} }
if (isset($this->_definition[$id])) { if (isset($this->_definition[$id])) {