Skip to content

Commit

Permalink
修复临时设置服务器到请求上下文后的恢复 (#637)
Browse files Browse the repository at this point in the history
* 修复临时设置服务器到请求上下文后的恢复

* 修复临时设置服务器到请求上下文后的恢复

* 修复测试
  • Loading branch information
Yurunsoft authored Nov 8, 2023
1 parent b3e6027 commit 8936a21
Show file tree
Hide file tree
Showing 18 changed files with 12 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/Components/swoole/src/Listener/AfterWorkerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public function handle(WorkerStartEventParam $e): void

ImiCommand::getOutput()->writeln('<info>App Inited</info>');
}
$originServer = RequestContext::get('server');
foreach (ServerManager::getServers() as $name => $server)
{
RequestContext::set('server', $server);
Server::getInstance($name);
}
RequestContext::set('server', $originServer);
$httpRouteInit = new HttpRouteInit();
$httpRouteInit->handle($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function handle(EventParam $e): void
$this->outputServerInfo();
}
}
$originServer = RequestContext::get('server');
foreach (ServerManager::getServers(ISwooleServer::class) as $server)
{
if ($server->isLongConnection())
Expand All @@ -52,6 +53,7 @@ public function handle(EventParam $e): void
}
}
}
RequestContext::set('server', $originServer);
if ($serverStart ?? false)
{
Log::info('Server start');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = Server::closeByFlag($data['flag'], $serverName, false);
if (($data['needResponse'] ?? true) && !SwooleWorker::isWorkerIdProcess($workerId))
{
Expand Down
3 changes: 0 additions & 3 deletions src/Components/swoole/src/Server/Listener/OnExistsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = false;
if (isset($data['flag']))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = Server::sendRawByFlag($data['data'], $data['flag'], $serverName, false);
if (($data['needResponse'] ?? true) && !SwooleWorker::isWorkerIdProcess($workerId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = Server::sendRawToAll($data['data'], $serverName, false);
if (($data['needResponse'] ?? true) && !SwooleWorker::isWorkerIdProcess($workerId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = Server::sendRaw($data['data'], $data['clientIds'], $serverName, false);
if (($data['needResponse'] ?? true) && !SwooleWorker::isWorkerIdProcess($workerId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Server;
use Imi\Swoole\SwooleWorker;

Expand All @@ -28,7 +26,6 @@ public function handle(EventParam $e): void
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', ServerManager::getServer($serverName));
$result = Server::sendRawToGroup($data['groups'], $data['data'], $serverName, false);
if (($data['needResponse'] ?? true) && !SwooleWorker::isWorkerIdProcess($workerId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Imi\Log\Log;
use Imi\RequestContext;
use Imi\Server\Server;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Util\AmqpServerUtil;
use Imi\Worker;

Expand Down Expand Up @@ -82,7 +81,6 @@ protected function consume(IMessage $message)
{
$data = json_decode($message->getBody(), true);
$serverName = $data['serverName'];
RequestContext::set('server', $server = ServerManager::getServer($serverName));
switch ($data['action'] ?? null)
{
case 'sendRawByFlag':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Workerman\Server\Util\LocalServerUtil;

/**
Expand All @@ -26,7 +24,6 @@ public function handle(EventParam $e): void
{
$data = $e->getData();
['flag' => $flag, 'serverName' => $serverName] = $data['data'];
RequestContext::set('server', ServerManager::getServer($serverName));

/** @var LocalServerUtil $serverUtil */
$serverUtil = App::getBean(LocalServerUtil::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Workerman\Server\Util\LocalServerUtil;

/**
Expand All @@ -26,7 +24,6 @@ public function handle(EventParam $e): void
{
$data = $e->getData();
['data' => $data, 'flag' => $flag, 'serverName' => $serverName] = $data['data'];
RequestContext::set('server', ServerManager::getServer($serverName));

/** @var LocalServerUtil $serverUtil */
$serverUtil = App::getBean(LocalServerUtil::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Workerman\Server\Util\LocalServerUtil;

/**
Expand All @@ -26,7 +24,6 @@ public function handle(EventParam $e): void
{
$data = $e->getData();
['data' => $data, 'serverName' => $serverName] = $data['data'];
RequestContext::set('server', ServerManager::getServer($serverName));

/** @var LocalServerUtil $serverUtil */
$serverUtil = App::getBean(LocalServerUtil::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Workerman\Server\Util\LocalServerUtil;

/**
Expand All @@ -26,7 +24,6 @@ public function handle(EventParam $e): void
{
$data = $e->getData();
['data' => $data, 'groupName' => $groupName, 'serverName' => $serverName] = $data['data'];
RequestContext::set('server', ServerManager::getServer($serverName));

/** @var LocalServerUtil $serverUtil */
$serverUtil = App::getBean(LocalServerUtil::class);
Expand Down
5 changes: 1 addition & 4 deletions src/Server/Contract/BaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ public function getBean(string $name, ...$params)
}
finally
{
if ($server)
{
$context['server'] = $server;
}
$context['server'] = $server;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Server/Http/Listener/HttpRouteInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@ protected function parseAnnotations(): void
}
unset($context['server']);
}
if ($originServer)
{
$context['server'] = $originServer;
}
$context['server'] = $originServer;
}
}
2 changes: 2 additions & 0 deletions src/Server/TcpServer/Listener/TcpRouteInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function parseAnnotations(EventParam $e): void
{
$controllerParser = TcpControllerParser::getInstance();
$context = RequestContext::getContext();
$originServer = $context['server'] ?? null;
foreach (ServerManager::getServers(ITcpServer::class) as $name => $server)
{
$context['server'] = $server;
Expand Down Expand Up @@ -96,5 +97,6 @@ private function parseAnnotations(EventParam $e): void
}
unset($context['server']);
}
$context['server'] = $originServer;
}
}
2 changes: 2 additions & 0 deletions src/Server/UdpServer/Listener/UdpRouteInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function parseAnnotations(EventParam $e): void
{
$controllerParser = UdpControllerParser::getInstance();
$context = RequestContext::getContext();
$originServer = $context['server'] ?? null;
foreach (ServerManager::getServers() as $name => $server)
{
if (Protocol::UDP !== $server->getProtocol())
Expand Down Expand Up @@ -100,5 +101,6 @@ private function parseAnnotations(EventParam $e): void
}
unset($context['server']);
}
$context['server'] = $originServer;
}
}
2 changes: 2 additions & 0 deletions src/Server/WebSocket/Listener/WSRouteInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function parseAnnotations(EventParam $e): void
{
$controllerParser = WSControllerParser::getInstance();
$context = RequestContext::getContext();
$originServer = $context['server'] ?? null;
foreach (ServerManager::getServers() as $name => $server)
{
if (Protocol::WEBSOCKET !== $server->getProtocol())
Expand Down Expand Up @@ -106,5 +107,6 @@ private function parseAnnotations(EventParam $e): void
}
unset($context['server']);
}
$context['server'] = $originServer;
}
}

0 comments on commit 8936a21

Please sign in to comment.