mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
add Constants.php
Signed-off-by: reasno <guxi99@gmail.com>
This commit is contained in:
parent
5ef6ea68ad
commit
f9c65e05ed
@ -19,6 +19,7 @@ use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use Psr\Container\ContainerInterface;
|
||||
@ -72,7 +73,7 @@ class BootProcessListener implements ListenerInterface
|
||||
retry(INF, function () use ($interval) {
|
||||
$prevConfig = [];
|
||||
while (true) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator = CoordinatorManager::get(Constants::ON_WORKER_EXIT);
|
||||
$workerExited = $coordinator->yield($interval);
|
||||
if ($workerExited) {
|
||||
break;
|
||||
|
@ -18,6 +18,7 @@ use Hyperf\ConfigApollo\PipeMessage;
|
||||
use Hyperf\ConfigApollo\ReleaseKey;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
|
||||
@ -72,7 +73,7 @@ class BootProcessListener extends OnPipeMessageListener
|
||||
$interval = $this->config->get('apollo.interval', 5);
|
||||
retry(INF, function () use ($namespaces, $callbacks, $interval) {
|
||||
while (true) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator = CoordinatorManager::get(Constants::ON_WORKER_EXIT);
|
||||
$workerExited = $coordinator->yield($interval);
|
||||
if ($workerExited) {
|
||||
break;
|
||||
|
@ -21,6 +21,7 @@ use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use Hyperf\Utils\Packer\JsonPacker;
|
||||
@ -88,7 +89,7 @@ class BootProcessListener implements ListenerInterface
|
||||
retry(INF, function () use ($interval) {
|
||||
$prevConfig = [];
|
||||
while (true) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator = CoordinatorManager::get(Constants::ON_WORKER_EXIT);
|
||||
$workerExited = $coordinator->yield($interval);
|
||||
if ($workerExited) {
|
||||
break;
|
||||
|
@ -19,6 +19,7 @@ use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Process\Event\BeforeProcessHandle;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
|
||||
@ -67,7 +68,7 @@ class BootProcessListener implements ListenerInterface
|
||||
retry(INF, function () use ($interval) {
|
||||
$prevConfig = [];
|
||||
while (true) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator = CoordinatorManager::get(Constants::ON_WORKER_EXIT);
|
||||
$workerExited = $coordinator->yield($interval);
|
||||
if ($workerExited) {
|
||||
break;
|
||||
|
@ -13,7 +13,7 @@ declare(strict_types=1);
|
||||
namespace Hyperf\Framework\Bootstrap;
|
||||
|
||||
use Hyperf\Framework\Event\OnWorkerExit;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
@ -35,8 +35,7 @@ class WorkerExitCallback
|
||||
{
|
||||
$this->dispatcher->dispatch(new OnWorkerExit($server, $workerId));
|
||||
Coroutine::create(function () {
|
||||
$coordinator = CoordinatorManager::get(SwooleEvent::ON_WORKER_EXIT);
|
||||
$coordinator->resume();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_EXIT)->resume();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use Hyperf\Framework\Event\AfterWorkerStart;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Framework\Event\MainWorkerStart;
|
||||
use Hyperf\Framework\Event\OtherWorkerStart;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Swoole\Server as SwooleServer;
|
||||
@ -60,7 +60,6 @@ class WorkerStartCallback
|
||||
}
|
||||
|
||||
$this->eventDispatcher->dispatch(new AfterWorkerStart($server, $workerId));
|
||||
$coordinator = CoordinatorManager::get(SwooleEvent::ON_WORKER_START);
|
||||
$coordinator->resume();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_START)->resume();
|
||||
}
|
||||
}
|
||||
|
@ -25,9 +25,8 @@ use Hyperf\HttpServer\Contract\CoreMiddlewareInterface;
|
||||
use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
|
||||
use Hyperf\HttpServer\Router\Dispatched;
|
||||
use Hyperf\HttpServer\Router\DispatcherFactory;
|
||||
use Hyperf\HttpServer\Router\Handler;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
use Hyperf\Utils\Context;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
@ -99,8 +98,7 @@ class Server implements OnRequestInterface, MiddlewareInitializerInterface
|
||||
public function onRequest(SwooleRequest $request, SwooleResponse $response): void
|
||||
{
|
||||
try {
|
||||
$coordinator = CoordinatorManager::get(SwooleEvent::ON_WORKER_START);
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_START)->yield();
|
||||
|
||||
[$psr7Request, $psr7Response] = $this->initRequestAndResponse($request, $response);
|
||||
|
||||
|
@ -17,6 +17,7 @@ use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Metric\Contract\MetricFactoryInterface;
|
||||
use Hyperf\Metric\Event\MetricFactoryReady;
|
||||
use Hyperf\Metric\MetricSetter;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Swoole\Coroutine;
|
||||
@ -110,8 +111,7 @@ class OnMetricFactoryReady implements ListenerInterface
|
||||
});
|
||||
// Clean up timer on worker exit;
|
||||
Coroutine::create(function () use ($timerId) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_EXIT)->yield();
|
||||
Timer::clear($timerId);
|
||||
});
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ use Hyperf\Metric\Contract\MetricFactoryInterface;
|
||||
use Hyperf\Metric\Event\MetricFactoryReady;
|
||||
use Hyperf\Metric\MetricSetter;
|
||||
use Hyperf\Retry\Retry;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use Psr\Container\ContainerInterface;
|
||||
@ -145,8 +146,7 @@ class OnWorkerStart implements ListenerInterface
|
||||
});
|
||||
// Clean up timer on worker exit;
|
||||
Coroutine::create(function () use ($timerId) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_EXIT)->yield();
|
||||
Timer::clear($timerId);
|
||||
});
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Framework\Event\BeforeWorkerStart;
|
||||
use Hyperf\Metric\Contract\MetricFactoryInterface;
|
||||
use Hyperf\Pool\Pool;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use Psr\Container\ContainerInterface;
|
||||
@ -79,8 +80,7 @@ abstract class PoolWatcher
|
||||
$connectionsInUseGauge->set((float) $pool->getCurrentConnections());
|
||||
});
|
||||
Coroutine::create(function () use ($timerId) {
|
||||
$coordinator = CoordinatorManager::get('workerExit');
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_EXIT)->yield();
|
||||
Timer::clear($timerId);
|
||||
});
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
|
||||
use Hyperf\Rpc\Context as RpcContext;
|
||||
use Hyperf\Rpc\Protocol;
|
||||
use Hyperf\Server\ServerManager;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
use Hyperf\Utils\Context;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
@ -106,8 +106,7 @@ abstract class Server implements OnReceiveInterface, MiddlewareInitializerInterf
|
||||
{
|
||||
$request = $response = null;
|
||||
try {
|
||||
$coordinator = CoordinatorManager::get(SwooleEvent::ON_WORKER_START);
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_START)->yield();
|
||||
|
||||
// Initialize PSR-7 Request and Response objects.
|
||||
Context::set(ServerRequestInterface::class, $request = $this->buildRequest($fd, $fromId, $data));
|
||||
|
18
src/utils/src/Coordinator/Constants.php
Normal file
18
src/utils/src/Coordinator/Constants.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Hyperf\Utils\Coordinator;
|
||||
|
||||
|
||||
class Constants
|
||||
{
|
||||
/**
|
||||
* Swoole onWorkerStart event.
|
||||
*/
|
||||
const ON_WORKER_START = 'workerStart';
|
||||
/**
|
||||
* Swoole onWorkerExit event.
|
||||
*/
|
||||
const ON_WORKER_EXIT = 'workerExit';
|
||||
|
||||
}
|
@ -36,11 +36,7 @@ class Coordinator
|
||||
public function yield($timeout = -1): bool
|
||||
{
|
||||
$this->channel->pop((float) $timeout);
|
||||
$code = $this->channel->errCode;
|
||||
if ($code == -2) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return $this->channel->errCode === -2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,8 +26,8 @@ use Hyperf\HttpMessage\Server\Response as Psr7Response;
|
||||
use Hyperf\HttpServer\Contract\CoreMiddlewareInterface;
|
||||
use Hyperf\HttpServer\MiddlewareManager;
|
||||
use Hyperf\HttpServer\Router\Dispatched;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
use Hyperf\Utils\Context;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\WebSocketServer\Collector\FdCollector;
|
||||
use Hyperf\WebSocketServer\Exception\Handler\WebSocketExceptionHandler;
|
||||
@ -116,8 +116,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
public function onHandShake(SwooleRequest $request, SwooleResponse $response): void
|
||||
{
|
||||
try {
|
||||
$coordinator = CoordinatorManager::get(SwooleEvent::ON_WORKER_START);
|
||||
$coordinator->yield();
|
||||
CoordinatorManager::get(Constants::ON_WORKER_START)->yield();
|
||||
|
||||
$security = $this->container->get(Security::class);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user