This commit is contained in:
2023-08-28 12:01:30 +08:00
parent 4a607a5687
commit 9b4f5d59dc
7 changed files with 234 additions and 192 deletions
+86 -74
View File
@@ -8,91 +8,103 @@ use Swoole\Coroutine;
class ActorManager
{
/** @var array<string, ActorInterface> */
private array $nodes = [];
/** @var array<string, ActorInterface> */
private array $nodes = [];
/**
* @param Actor $actor
* @return void
*/
public function addActor(ActorInterface $actor): void
{
$this->nodes[$actor->uniqueId] = $actor;
Coroutine::create(function (Actor $actor) {
$actor->run();
}, $actor);
}
/**
* @param Actor $actor
* @return void
*/
public function addActor(ActorInterface $actor): void
{
$this->nodes[$actor->uniqueId] = $actor;
Coroutine::create(function (Actor $actor) {
$actor->run();
}, $actor);
}
/**
* @param $name
* @return void
*/
public function closeActor($name): void
{
$node = $this->nodes[$name] ?? null;
if (is_null($node)) {
return;
}
foreach ($node as $actor) {
$actor->shutdown();
}
}
/**
* @param $name
* @return void
*/
public function closeActor($name): void
{
$node = $this->nodes[$name] ?? null;
if (is_null($node)) {
return;
}
foreach ($node as $actor) {
$actor->shutdown();
}
}
/**
* @param $name
* @param $message
* @return bool
*/
public function write($name, $message): bool
{
$actor = $this->nodes[$name] ?? null;
if (is_null($actor)) {
return false;
}
return $actor->write($message);
}
/**
* @param $name
* @param $message
* @return bool
*/
public function write($name, $message): bool
{
$actor = $this->nodes[$name] ?? null;
if (is_null($actor)) {
return false;
}
return $actor->write($message);
}
/**
* @param $name
* @return array
*/
public function lists($name): array
{
$array = [];
foreach ($this->nodes[$name] as $actor) {
$array[] = [
'id' => $actor->getName(),
'state' => $actor->getState()->name,
'runTime' => $actor->getRunTime()
];
}
return $array;
}
/**
* @param $name
* @return array
*/
public function lists($name): array
{
$array = [];
foreach ($this->nodes[$name] as $actor) {
$array[] = [
'id' => $actor->getName(),
'state' => $actor->getState()->name,
'runTime' => $actor->getRunTime()
];
}
return $array;
}
/**
* @param string $uniqueId
* @return bool
*/
public function hasActor(string $uniqueId): bool
{
return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface;
}
/**
* @param string $uniqueId
* @return bool
*/
public function hasActor(string $uniqueId): bool
{
return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface;
}
/**
* @return void
*/
public function clean(): void
{
foreach ($this->nodes as $actor) {
$actor->shutdown();
}
$this->nodes = [];
}
/**
* @param array|null $data
* @return void
*/
public static function exec(?array $data): void
{
if (is_null($data)) {
return;
}
}
/**
* @return void
*/
public function clean(): void
{
foreach ($this->nodes as $actor) {
$actor->shutdown();
}
$this->nodes = [];
}
}
+4 -1
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace Kiri\Actor;
use JetBrains\PhpStorm\ArrayShape;
class ActorMessage implements \JsonSerializable
{
@@ -63,7 +65,8 @@ class ActorMessage implements \JsonSerializable
/**
* @return array
*/
public function jsonSerialize(): array
#[ArrayShape(['userId' => "int", 'event' => "string", 'body' => "array"])]
public function jsonSerialize(): array
{
return [
'userId' => $this->userId,
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Kiri\Actor;
use Kiri\Server\Abstracts\BaseProcess;
use Swoole\Coroutine;
use Swoole\Process;
class ActorProcess extends BaseProcess
{
/**
* @var bool
*/
protected bool $enable_coroutine = true;
/**
* @return string
*/
public function getName(): string
{
// TODO: Change the autogenerated stub
return '[' . \config('id', 'system-service') . '].Actor Manager';
}
public function process(?Process $process): void
{
// TODO: Implement process() method.
while ($this->isStop() === false) {
$read = $process->read();
ActorManager::exec(json_decode($read, true));
Coroutine::sleep(1000 / 120);
}
}
/**
* @return $this
*/
public function onSigterm(): static
{
// TODO: Implement onSigterm() method.
Coroutine::create(function () {
$sign = Coroutine::waitSignal(SIGINT | SIGTERM);
if ($sign) {
$this->onShutdown(true);
}
});
return $this;
}
}
+62 -52
View File
@@ -22,66 +22,76 @@ class Component implements Configure
{
/**
* BaseAbstract constructor.
*/
public function __construct()
{
}
/**
* BaseAbstract constructor.
*/
public function __construct()
{
}
/**
* @return void
*/
public function init(): void
{
}
/**
* @return void
*/
public function init(): void
{
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public function __get(string $name)
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
}
}
/**
* @return Kiri\Error\StdoutLogger
* @throws \ReflectionException
*/
public function getLogger(): Kiri\Error\StdoutLogger
{
return Kiri::getLogger();
}
/**
* @param string $name
* @param $value
* @return void
* @throws Exception
*/
public function __set(string $name, $value): void
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
}
}
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public function __get(string $name)
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
}
}
/**
* @param string $name
* @param $value
* @return void
* @throws Exception
*/
public function __set(string $name, $value): void
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
} else {
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
}
}
}
+8 -4
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Kiri\Error;
use Closure;
use ErrorException;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
@@ -18,6 +19,7 @@ use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Kiri\Events\OnSystemError;
use Throwable;
/**
* Class ErrorHandler
@@ -110,13 +112,13 @@ class ErrorHandler extends Component implements ErrorInterface
/**
* @param \Throwable $exception
* @param Throwable $exception
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function exceptionHandler(\Throwable $exception): void
public function exceptionHandler(Throwable $exception): void
{
$this->category = 'exception';
@@ -127,7 +129,7 @@ class ErrorHandler extends Component implements ErrorInterface
/**
* @throws \ErrorException
* @throws ErrorException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
@@ -136,8 +138,10 @@ class ErrorHandler extends Component implements ErrorInterface
{
$error = func_get_args();
var_dump($error);
event(new OnSystemError());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
throw new ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
}
+10 -13
View File
@@ -1,17 +1,14 @@
<?php
use function Swoole\Coroutine\run;
use Swoole\Runtime;
use Swoole\Coroutine\Http\Server;
run(function () {
Runtime::enableCoroutine(true);
\Co\run(function () {
Swoole\Coroutine::create(function () {
var_dump(1);
});
$server = new Server('0.0.0.0',9501);
$server->handle('/', function () {
});
$server->start();
});
$channel = new \Swoole\Coroutine\Channel(100);
for ($i = 0; $i < 90; $i++) {
$channel->push(100);
}
$channel->close();
$channel = null;
var_dump($channel);
});
+9 -48
View File
@@ -1,52 +1,13 @@
<?php
$i = 0;
namespace Ar;
while ($i <= 100) {
for ($ii = 0; $ii < 10; $ii++) {
echo "\r";
$i++;
//require_once "a.php";
//
//
//$time = microtime();
//
//echo \count([]) . PHP_EOL;
//
//echo $time . PHP_EOL;
//echo microtime() . PHP_EOL;
//
//
//
//$time = microtime();
//
//echo count([]) . PHP_EOL;
//
//echo $time . PHP_EOL;
//echo microtime() . PHP_EOL;
//
use Swoole\Coroutine\Http\Client;
use function Swoole\Coroutine\run;
//run(function () {
//
// $client = new Client('47.92.194.207',8500);
// $client->get('/v1/agent/services?filter=Service == FriendRpcService');
// $client->close();
// var_dump($client->getBody());
//
//});
$spl = new \SplPriorityQueue();
$spl->insert(1,0);
$spl->insert(2,0);
$spl->insert(3,0);
$spl->insert(4,0);
$spl->insert(5,0);
$spl->insert(6,0);
$spl->insert(7,0);
$spl->compare();
$spl->extract();
var_dump($spl);
echo '[' . $i . '%]' . "\r";
}
sleep(1);
}