提交 c5665fd3 编写于 作者: weixin_47267244's avatar weixin_47267244

test

上级 9d0e1c22
<?php
declare(strict_types=1);
namespace Imi\Swoole\Server\Listener;
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\RequestContext;
use Imi\Server\ServerManager;
use Imi\Swoole\Server\Contract\ISwooleServer;
use Imi\Swoole\Server\Server;
/**
* 获取当前连接数量-请求
*
* @Listener(eventName="IMI.PIPE_MESSAGE.getConnectionCountRequest")
*/
class OnGetConnectionCountRequest implements IEventListener
{
/**
* {@inheritDoc}
*/
public function handle(EventParam $e): void
{
$eData = $e->getData();
$workerId = $eData['workerId'] ?? -1;
$data = $eData['data'];
$serverName = $data['serverName'];
RequestContext::set('server', $server = ServerManager::getServer($serverName, ISwooleServer::class));
/** @var ISwooleServer $server */
if ($data['needResponse'] ?? true)
{
Server::sendMessage('getConnectionCountResponse', [
'messageId' => $data['messageId'],
'result' => iterator_count(clone $server->getSwoolePort()->connections),
'serverName' => $serverName,
], $workerId, $serverName);
}
}
}
<?php
declare(strict_types=1);
namespace Imi\Swoole\Server\Listener;
use Imi\Bean\Annotation\Listener;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\Swoole\Util\Co\ChannelContainer;
/**
* 获取当前连接数量-响应.
*
* @Listener(eventName="IMI.PIPE_MESSAGE.getConnectionCountResponse")
*/
class OnGetConnectionCountResponse implements IEventListener
{
/**
* {@inheritDoc}
*/
public function handle(EventParam $e): void
{
$data = $e->getData()['data'];
if (ChannelContainer::hasChannel($data['messageId']))
{
ChannelContainer::push($data['messageId'], $data);
}
}
}
......@@ -643,7 +643,45 @@ class LocalServerUtil implements ISwooleServerUtil
*/
public function getConnectionCount(?string $serverName = null): int
{
return $this->getServer($serverName)->getSwoolePort()->connections->count();
$server = $this->getServer($serverName);
$swooleServer = $server->getSwooleServer();
if (\SWOOLE_BASE === $swooleServer->mode)
{
$result = 0;
$id = uniqid('', true);
try
{
$channel = ChannelContainer::getChannel($id);
$count = $this->sendMessage('getConnectionCountRequest', [
'messageId' => $id,
'serverName' => $server->getName(),
'needResponse' => true,
]);
if (ProcessType::PROCESS !== App::get(ProcessAppContexts::PROCESS_TYPE))
{
for ($i = $count; $i > 0; --$i)
{
$popResult = $channel->pop($this->waitResponseTimeout);
var_dump(__METHOD__, $popResult);
if (false === $popResult)
{
break;
}
$result += ($popResult['result'] ?? 0);
}
}
return $result;
}
finally
{
ChannelContainer::removeChannel($id);
}
}
else
{
return iterator_count(clone $this->getServer($serverName)->getSwoolePort()->connections);
}
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册