变更
This commit is contained in:
@@ -173,29 +173,6 @@ class AsyncServer implements ServerInterface
|
|||||||
$this->container->get(LocalService::class)->set($config->getName(), $port);
|
$this->container->get(LocalService::class)->set($config->getName(), $port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $signal
|
|
||||||
* @return void
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function onSignal(array $signal): void
|
|
||||||
{
|
|
||||||
pcntl_signal(SIGINT, [$this, 'onSigint']);
|
|
||||||
foreach ($signal as $sig => $value) {
|
|
||||||
if (is_array($value) && is_string($value[0])) {
|
|
||||||
$value[0] = $this->container->get($value[0]);
|
|
||||||
}
|
|
||||||
if (!is_callable($value, true)) {
|
|
||||||
throw new Exception('Register signal callback must can exec.');
|
|
||||||
}
|
|
||||||
pcntl_signal($sig, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $no
|
* @param $no
|
||||||
* @param array $signInfo
|
* @param array $signInfo
|
||||||
@@ -254,7 +231,6 @@ class AsyncServer implements ServerInterface
|
|||||||
* @return void
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
*/
|
||||||
public function start(): void
|
public function start(): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
namespace Kiri\Server\Abstracts;
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Swoole\Http\Server as HServer;
|
use Swoole\Http\Server as HServer;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Kiri\Server\Constant;
|
use Kiri\Server\Constant;
|
||||||
@@ -32,6 +34,30 @@ trait TraitServer
|
|||||||
$container->add($name);
|
$container->add($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $signal
|
||||||
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function onSignal(array $signal): void
|
||||||
|
{
|
||||||
|
pcntl_signal(SIGINT, [$this, 'onSigint']);
|
||||||
|
foreach ($signal as $sig => $value) {
|
||||||
|
if (is_array($value) && is_string($value[0])) {
|
||||||
|
$value[0] = $this->container->get($value[0]);
|
||||||
|
}
|
||||||
|
if (!is_callable($value, true)) {
|
||||||
|
throw new Exception('Register signal callback must can exec.');
|
||||||
|
}
|
||||||
|
pcntl_signal($sig, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,240 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Server;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Di\ContainerInterface;
|
||||||
|
use Kiri\Di\LocalService;
|
||||||
|
use Kiri\Events\EventDispatch;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Exception\NotFindClassException;
|
||||||
|
use Kiri\Server\Abstracts\ProcessManager;
|
||||||
|
use Kiri\Server\Abstracts\TraitServer;
|
||||||
|
use Kiri\Server\Config as SConfig;
|
||||||
|
use Kiri\Server\Events\OnServerBeforeStart;
|
||||||
|
use Kiri\Server\Events\OnShutdown;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use ReflectionException;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Coroutine\Server as ScServer;
|
||||||
|
use Swoole\Coroutine\Http\Server as SchServer;
|
||||||
|
use Swoole\Http\Request;
|
||||||
|
use Swoole\Http\Response;
|
||||||
|
use Swoole\Server;
|
||||||
|
|
||||||
|
class CoroutineServer implements ServerInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
use TraitServer;
|
||||||
|
|
||||||
|
|
||||||
|
/** @var array<SchServer|ScServer> */
|
||||||
|
private array $servers = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Server|null
|
||||||
|
*/
|
||||||
|
private Server|null $server = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @param ContainerInterface $container
|
||||||
|
* @param EventDispatch $dispatch
|
||||||
|
* @param LoggerInterface $logger
|
||||||
|
* @param ProcessManager $processManager
|
||||||
|
*/
|
||||||
|
public function __construct(public Config $config,
|
||||||
|
public ContainerInterface $container,
|
||||||
|
public EventDispatch $dispatch,
|
||||||
|
public LoggerInterface $logger,
|
||||||
|
public ProcessManager $processManager)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $service
|
||||||
|
* @param int $daemon
|
||||||
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function initCoreServers(array $service, int $daemon = 0): void
|
||||||
|
{
|
||||||
|
$service = $this->genConfigService($service);
|
||||||
|
foreach ($service as $value) {
|
||||||
|
if ($value['type'] == Constant::SERVER_TYPE_HTTP) {
|
||||||
|
$this->addListener($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$rpcService = Config::get('rpc', []);
|
||||||
|
if (!empty($rpcService)) {
|
||||||
|
$this->addListener(instance(SConfig::class, [], $rpcService));
|
||||||
|
}
|
||||||
|
// $this->processManager->batch(Config::get('processes', []));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SConfig $config
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function addListener(SConfig $config): void
|
||||||
|
{
|
||||||
|
$server = new Coroutine\Http\Server($config->getHost(), $config->getPort(), false, true);
|
||||||
|
|
||||||
|
$events = $config->getEvents()[Constant::REQUEST] ?? null;
|
||||||
|
if (is_null($events)) {
|
||||||
|
$events = [\Kiri\Message\Server::class, 'onRequest'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$events[0] = $this->container->get($events[0]);
|
||||||
|
$server->handle('/', $events);
|
||||||
|
|
||||||
|
$this->servers[] = $server;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return ScServer|SchServer|null
|
||||||
|
*/
|
||||||
|
public function getServer(string $name = ''): ScServer|SchServer|null
|
||||||
|
{
|
||||||
|
return $this->servers[$name] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function shutdown(): bool
|
||||||
|
{
|
||||||
|
foreach ($this->servers as $server) {
|
||||||
|
$server->shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->dispatch->dispatch(new OnShutdown());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $no
|
||||||
|
* @param array $signInfo
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onSigint($no, array $signInfo): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->logger->alert('Pid ' . getmypid() . ' get signo ' . $no);
|
||||||
|
$this->shutdown();
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
$this->logger->error($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Server\Port|Server $base
|
||||||
|
* @param array $events
|
||||||
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
private function onEventListen(Server\Port|Server $base, array $events): void
|
||||||
|
{
|
||||||
|
foreach ($events as $name => $event) {
|
||||||
|
if (is_array($event) && is_string($event[0])) {
|
||||||
|
$event[0] = $this->container->get($event[0]);
|
||||||
|
}
|
||||||
|
$base->on($name, $event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function start(): void
|
||||||
|
{
|
||||||
|
Coroutine\run(function () {
|
||||||
|
$this->dispatch->dispatch(new OnServerBeforeStart());
|
||||||
|
|
||||||
|
$this->onTasker();
|
||||||
|
foreach ($this->servers as $server) {
|
||||||
|
Coroutine::create(function () use ($server) {
|
||||||
|
$server->start();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Coroutine\Channel $channel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
private function onTasker(): void
|
||||||
|
{
|
||||||
|
$config = Config::get('server.settings', []);
|
||||||
|
|
||||||
|
if (isset($config[Constant::OPTION_TASK_WORKER_NUM])) {
|
||||||
|
if ($config[Constant::OPTION_TASK_WORKER_NUM] < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$taskEvents = $config['events'][Constant::TASK] ?? null;
|
||||||
|
$finishEvents = $config['events'][Constant::FINISH] ?? null;
|
||||||
|
|
||||||
|
if (is_null($taskEvents)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$taskEvents[0] = $this->container->get($taskEvents[0]);
|
||||||
|
if (!is_null($finishEvents)) {
|
||||||
|
$finishEvents[0] = $this->container->get($finishEvents[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->channel = new Coroutine\Channel($config[Constant::OPTION_TASK_WORKER_NUM]);
|
||||||
|
for ($i = 0; $i < $config[Constant::OPTION_TASK_WORKER_NUM]; $i++) {
|
||||||
|
Coroutine::create(fn() => $this->taskRunner($i, $taskEvents, $finishEvents));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $taskId
|
||||||
|
* @param $callback
|
||||||
|
* @param $finishEvents
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function taskRunner($taskId, $callback, $finishEvents): void
|
||||||
|
{
|
||||||
|
$taskData = $this->channel->pop();
|
||||||
|
if (!is_null($taskData)) {
|
||||||
|
$result = $callback($taskId, $taskData);
|
||||||
|
if (is_callable($finishEvents, true)) {
|
||||||
|
$finishEvents($taskId, $result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->taskRunner($taskId, $callback, $finishEvents);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+29
-39
@@ -38,13 +38,13 @@ defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
|||||||
*/
|
*/
|
||||||
class Server extends HttpService
|
class Server extends HttpService
|
||||||
{
|
{
|
||||||
|
|
||||||
private mixed $daemon = 0;
|
private mixed $daemon = 0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param State $state
|
* @param State $state
|
||||||
* @param AsyncServer $manager
|
* @param CoroutineServer $manager
|
||||||
* @param ContainerInterface $container
|
* @param ContainerInterface $container
|
||||||
* @param ProcessManager $processManager
|
* @param ProcessManager $processManager
|
||||||
* @param EventDispatch $dispatch
|
* @param EventDispatch $dispatch
|
||||||
@@ -54,7 +54,7 @@ class Server extends HttpService
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(public State $state,
|
public function __construct(public State $state,
|
||||||
public AsyncServer $manager,
|
public CoroutineServer $manager,
|
||||||
public ContainerInterface $container,
|
public ContainerInterface $container,
|
||||||
public ProcessManager $processManager,
|
public ProcessManager $processManager,
|
||||||
public EventDispatch $dispatch,
|
public EventDispatch $dispatch,
|
||||||
@@ -64,8 +64,8 @@ class Server extends HttpService
|
|||||||
{
|
{
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -77,15 +77,15 @@ class Server extends HttpService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Coroutine::set([
|
Coroutine::set([
|
||||||
'hook_flags' => (SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL) ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
|
'hook_flags' => (SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL) ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
|
||||||
'enable_deadlock_check' => FALSE,
|
'enable_deadlock_check' => FALSE,
|
||||||
'exit_condition' => function () {
|
'exit_condition' => function () {
|
||||||
return Coroutine::stats()['coroutine_num'] === 0;
|
return Coroutine::stats()['coroutine_num'] === 0;
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $process
|
* @param $process
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -94,8 +94,8 @@ class Server extends HttpService
|
|||||||
{
|
{
|
||||||
$this->processManager->add($process);
|
$this->processManager->add($process);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -110,8 +110,8 @@ class Server extends HttpService
|
|||||||
$this->manager->onSignal(Config::get('signal', []));
|
$this->manager->onSignal(Config::get('signal', []));
|
||||||
$this->manager->start();
|
$this->manager->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -122,8 +122,8 @@ class Server extends HttpService
|
|||||||
$this->provider->on(OnWorkerStart::class, [$this, 'setWorkerName']);
|
$this->provider->on(OnWorkerStart::class, [$this, 'setWorkerName']);
|
||||||
$this->provider->on(OnTaskerStart::class, [$this, 'setTaskerName']);
|
$this->provider->on(OnTaskerStart::class, [$this, 'setTaskerName']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnWorkerStart $onWorkerStart
|
* @param OnWorkerStart $onWorkerStart
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -135,11 +135,11 @@ class Server extends HttpService
|
|||||||
}
|
}
|
||||||
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
|
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
|
||||||
set_env('environmental', Kiri::WORKER);
|
set_env('environmental', Kiri::WORKER);
|
||||||
|
|
||||||
Kiri::setProcessName($prefix);
|
Kiri::setProcessName($prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnTaskerStart $onWorkerStart
|
* @param OnTaskerStart $onWorkerStart
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -151,11 +151,11 @@ class Server extends HttpService
|
|||||||
}
|
}
|
||||||
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
|
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
|
||||||
set_env('environmental', Kiri::TASK);
|
set_env('environmental', Kiri::TASK);
|
||||||
|
|
||||||
Kiri::setProcessName($prefix);
|
Kiri::setProcessName($prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -172,8 +172,8 @@ class Server extends HttpService
|
|||||||
$this->router->scan_build_route();
|
$this->router->scan_build_route();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -189,8 +189,8 @@ class Server extends HttpService
|
|||||||
}
|
}
|
||||||
$this->dispatch->dispatch(new OnShutdown());
|
$this->dispatch->dispatch(new OnShutdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
@@ -200,8 +200,8 @@ class Server extends HttpService
|
|||||||
{
|
{
|
||||||
return $this->state->isRunner();
|
return $this->state->isRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $daemon
|
* @param $daemon
|
||||||
* @return Server
|
* @return Server
|
||||||
@@ -214,14 +214,4 @@ class Server extends HttpService
|
|||||||
$this->daemon = $daemon;
|
$this->daemon = $daemon;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return HServer|SServer|WsServer|null
|
|
||||||
*/
|
|
||||||
#[Pure] public function getServer(): HServer|SServer|WsServer|null
|
|
||||||
{
|
|
||||||
return $this->manager->getServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user