diff --git a/Database/Connection.php b/Database/Connection.php index c5a5c3ea..bdf94b25 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -70,8 +70,8 @@ class Connection extends Component public function init() { $event = Snowflake::app()->getEvent(); - $event->on(Event::RELEASE_ALL, [$this, 'disconnect']); - $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'clear_connection']); + $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); + $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); } diff --git a/Database/Pagination.php b/Database/Pagination.php index 425cc006..51d7a49f 100644 --- a/Database/Pagination.php +++ b/Database/Pagination.php @@ -182,7 +182,7 @@ class Pagination extends Component $this->addError($exception->getMessage()); } finally { $event = Snowflake::app()->getEvent(); - $event->trigger(Event::EVENT_AFTER_REQUEST); + $event->trigger(Event::SYSTEM_RESOURCE_RELEASES); $this->_group->done(); } }, $callback, $data, $param); diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php index 5cf923c6..3dc6f9b8 100644 --- a/HttpServer/Abstracts/Callback.php +++ b/HttpServer/Abstracts/Callback.php @@ -41,6 +41,7 @@ abstract class Callback extends Application $event = Snowflake::app()->getEvent(); $event->offName(Event::EVENT_AFTER_REQUEST); + $event->offName(Event::SYSTEM_RESOURCE_RELEASES); $event->offName(Event::EVENT_BEFORE_REQUEST); $this->eventNotify($message, $event); } catch (\Throwable $exception) { diff --git a/HttpServer/Controller.php b/HttpServer/Controller.php index 2ff8a580..fc594988 100644 --- a/HttpServer/Controller.php +++ b/HttpServer/Controller.php @@ -4,25 +4,12 @@ declare(strict_types=1); namespace HttpServer; -use Database\DatabasesProviders; -use HttpServer\Client\Client; -use HttpServer\Client\Curl; -use HttpServer\Client\Http2; use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; use Exception; -use HttpServer\Route\Router; -use Kafka\Producer; -use Snowflake\Abstracts\BaseGoto; use Snowflake\Abstracts\TraitApplication; -use Snowflake\Cache\Redis; -use Snowflake\Error\Logger; -use Snowflake\Event; use Snowflake\Exception\ComponentException; -use Snowflake\Jwt\Jwt; -use Snowflake\Pool\Connection; -use Snowflake\Pool\Pool as SPool; use Snowflake\Snowflake; /** diff --git a/HttpServer/Events/OnClose.php b/HttpServer/Events/OnClose.php index b9644e10..3747fd71 100644 --- a/HttpServer/Events/OnClose.php +++ b/HttpServer/Events/OnClose.php @@ -31,10 +31,8 @@ class OnClose extends Callback */ public function onHandler(Server $server, int $fd) { - Coroutine::defer(function () { - fire(Event::EVENT_AFTER_REQUEST); - }); $this->execute($server, $fd); + fire(Event::SYSTEM_RESOURCE_RELEASES); } diff --git a/HttpServer/Events/OnHandshake.php b/HttpServer/Events/OnHandshake.php index fa56861b..861844f5 100644 --- a/HttpServer/Events/OnHandshake.php +++ b/HttpServer/Events/OnHandshake.php @@ -87,10 +87,8 @@ class OnHandshake extends Callback */ public function onHandler(SRequest $request, SResponse $response): void { - Coroutine::defer(function () { - fire(Event::EVENT_AFTER_REQUEST); - }); $this->execute($request, $response); + fire(Event::SYSTEM_RESOURCE_RELEASES); } diff --git a/HttpServer/Events/OnMessage.php b/HttpServer/Events/OnMessage.php index eeab3454..fcef5a05 100644 --- a/HttpServer/Events/OnMessage.php +++ b/HttpServer/Events/OnMessage.php @@ -28,7 +28,6 @@ class OnMessage extends Callback */ public function onHandler(Server $server, Frame $frame) { - Coroutine::defer([$this, 'onAfter']); try { if ($frame->opcode != 0x08) { $event = Snowflake::app()->getEvent(); @@ -41,19 +40,11 @@ class OnMessage extends Callback $this->addError($exception->getMessage(), 'websocket'); $server->send($frame->fd, $exception->getMessage()); } finally { + fire(Event::SYSTEM_RESOURCE_RELEASES); logger()->insert(); } } - - /** - * @throws ComponentException - */ - public function onAfter() - { - fire(Event::EVENT_AFTER_REQUEST); - } - /** * @param $event * @param $frame diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 043803d4..3a00f65f 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -35,10 +35,6 @@ class OnRequest extends Callback */ public function onHandler(Request $request, Response $response): mixed { - defer(function () use ($request) { - write(Json::encode(get_object_vars($request)), 'request'); - fire(Event::EVENT_AFTER_REQUEST); - }); try { [$req, $rep] = static::create($request, $response); if ($req->is('favicon.ico')) { @@ -50,6 +46,9 @@ class OnRequest extends Callback return \send($exception->getMessage(), $exception->getCode()); } return $this->sendErrorMessage($exception); + } finally { + write(Json::encode(get_object_vars($request)), 'request'); + fire(Event::SYSTEM_RESOURCE_RELEASES); } } diff --git a/HttpServer/Events/OnTask.php b/HttpServer/Events/OnTask.php index c5a568bd..1f2b6b1a 100644 --- a/HttpServer/Events/OnTask.php +++ b/HttpServer/Events/OnTask.php @@ -109,7 +109,7 @@ class OnTask extends Callback $this->error($exception, 'Task'); } finally { $event = Snowflake::app()->getEvent(); - $event->trigger(Event::RELEASE_ALL); + $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); Timer::clearAll(); } return $finish; diff --git a/HttpServer/Route/Reduce.php b/HttpServer/Route/Reduce.php index 79b0a700..9389252c 100644 --- a/HttpServer/Route/Reduce.php +++ b/HttpServer/Route/Reduce.php @@ -40,8 +40,6 @@ class Reduce return $pipe->onHandler($request, $passable); } catch (\Throwable $throwable) { return Json::to(0, $throwable); - } finally { - fire(Event::EVENT_AFTER_REQUEST); } }; }); diff --git a/Kafka/Producer.php b/Kafka/Producer.php index a11f6ef6..f7313434 100644 --- a/Kafka/Producer.php +++ b/Kafka/Producer.php @@ -89,7 +89,7 @@ class Producer extends Component }); $event = Snowflake::app()->getEvent(); - $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'flush']); + $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'flush']); if ($this->producer === null) { $this->producer = Snowflake::createObject(\RdKafka\Producer::class, [$this->conf]); diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index 1eefcc97..1fe902c8 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -37,8 +37,8 @@ class Redis extends Component public function init() { $event = Snowflake::app()->getEvent(); - $event->on(Event::RELEASE_ALL, [$this, 'destroy']); - $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']); + $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']); + $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']); $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); $event->on(Event::SERVER_TASK_START, [$this, 'createPool']); } diff --git a/System/Error/ErrorHandler.php b/System/Error/ErrorHandler.php index 8a24799c..754abe5e 100644 --- a/System/Error/ErrorHandler.php +++ b/System/Error/ErrorHandler.php @@ -75,7 +75,7 @@ class ErrorHandler extends Component implements ErrorInterface $this->category = 'exception'; $event = Snowflake::app()->getEvent(); - $event->trigger(Event::RELEASE_ALL); + $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); } @@ -103,7 +103,7 @@ class ErrorHandler extends Component implements ErrorInterface logger()->error($data, 'error'); $event = Snowflake::app()->getEvent(); - $event->trigger(Event::RELEASE_ALL); + $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } diff --git a/System/Event.php b/System/Event.php index fac5cd41..bf8f828c 100644 --- a/System/Event.php +++ b/System/Event.php @@ -31,9 +31,11 @@ class Event extends BaseObject const RECEIVE_CONNECTION = 'SERVER:RECEIVE:CONNECTION'; - const PROCESS_WORKER_STOP = 'SERVER:PROCESS:WORKER:STOP'; + const SYSTEM_RESOURCE_RELEASES = 'SYSTEM::RESOURCE::RELEASES'; + const SYSTEM_RESOURCE_CLEAN = 'SYSTEM::RESOURCE::CLEAN'; - const RELEASE_ALL = 'SERVER:RELEASE:ALL'; + + const PROCESS_WORKER_STOP = 'SERVER:PROCESS:WORKER:STOP'; const SERVER_AFTER_RELOAD = 'SERVER:AFTER:RELOAD'; const SERVER_BEFORE_RELOAD = 'SERVER:BEFORE:RELOAD';