Skip to content

Commit

Permalink
Move files from root internal directory to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Oct 29, 2024
1 parent 92f8dd0 commit cfd7f14
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public function __construct(private NetworkTrafficCounter $trafficStatisticStore

public function handleRequest(Request $request, RequestHandler $requestHandler): Response
{
$this->trafficStatisticStore->incRequests();

return $requestHandler->handleRequest($request);
try {
return $requestHandler->handleRequest($request);
} finally {
$this->trafficStatisticStore->incRequests();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\HttpServer\ReloadStrategy;

use Amp\Http\Server\Request;
use Luzrain\PHPStreamServer\Internal\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\ReloadStrategyInterface;

/**
* Reload worker after each request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\HttpServer\ReloadStrategy;

use Amp\Http\Server\Request;
use Luzrain\PHPStreamServer\Internal\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\ReloadStrategyInterface;

/**
* Reload worker on every $maxRequests requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Internal;

use Amp\DeferredFuture;
use Amp\Future;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Internal\ProcessScheduledEvent;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\PeriodicProcess;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger\TriggerFactory;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger\TriggerInterface;
use Luzrain\PHPStreamServer\Exception\PHPStreamServerException;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageBus;
use Luzrain\PHPStreamServer\Internal\Scheduler\Trigger\TriggerFactory;
use Luzrain\PHPStreamServer\Internal\Scheduler\Trigger\TriggerInterface;
use Luzrain\PHPStreamServer\Internal\SIGCHLDHandler;
use Luzrain\PHPStreamServer\Internal\Status;
use Psr\Log\LoggerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Internal;

use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\PeriodicProcess;
use Luzrain\PHPStreamServer\Exception\PHPStreamServerException;
Expand Down
2 changes: 1 addition & 1 deletion src/BundledPlugin/Scheduler/SchedulerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Amp\Future;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Command\SchedulerCommand;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Internal\Scheduler;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Internal\SchedulerStatus;
use Luzrain\PHPStreamServer\Internal\Container;
use Luzrain\PHPStreamServer\Internal\Logger\LoggerInterface;
use Luzrain\PHPStreamServer\Internal\MasterProcess;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageBus;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageHandler;
use Luzrain\PHPStreamServer\Internal\Scheduler\Scheduler;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Process;
use Revolt\EventLoop\Suspension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

use Cron\CronExpression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Scheduler\Trigger;
namespace Luzrain\PHPStreamServer\BundledPlugin\Scheduler\Trigger;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Supervisor;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy;

interface ReloadStrategyInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy;

use Revolt\EventLoop;

Expand All @@ -14,11 +14,10 @@ final class ReloadStrategyTrigger
/** @var list<ReloadStrategyInterface> */
private array $reloadStrategies = [];

public function __construct(private readonly \Closure $reloadCallback)
{
}

public function addReloadStrategy(ReloadStrategyInterface ...$reloadStrategies): void
/**
* @param array<ReloadStrategyInterface> $reloadStrategies
*/
public function __construct(private readonly \Closure $reloadCallback, array $reloadStrategies)
{
foreach ($reloadStrategies as $reloadStrategy) {
if ($reloadStrategy instanceof TimerReloadStrategyInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy;

interface TimerReloadStrategyInterface extends ReloadStrategyInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Supervisor;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal;

use Amp\DeferredFuture;
use Amp\Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal\Supervisor;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal;

use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\WorkerProcess;
use Luzrain\PHPStreamServer\Exception\PHPStreamServerException;
Expand All @@ -22,7 +22,7 @@ final class WorkerPool
private array $workerPool = [];

/**
* @var array<int, array<int, ProcessStatus>>
* @var ProcessStatus
*/
private array $processStatusMap = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\ReloadStrategy;

use Amp\Http\Server\ClientException;
use Amp\Http\Server\HttpErrorException;
use Luzrain\PHPStreamServer\Internal\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\ReloadStrategyInterface;

/**
* Reload worker each time after exception occurs
* Reload worker each time when exception occurs
*/
final class ExceptionReloadStrategy implements ReloadStrategyInterface
{
/** @var array<class-string<\Throwable>> */
private array $allowedExceptions = [
ClientException::class,
HttpErrorException::class,
'Amp\Http\Server\HttpErrorException',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\ReloadStrategy;

use Luzrain\PHPStreamServer\Internal\ReloadStrategy\TimerReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\TimerReloadStrategyInterface;

/**
* Reload worker if worker memory usage has increased $maxMemory value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor\ReloadStrategy;

use Luzrain\PHPStreamServer\Internal\ReloadStrategy\TimerReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\TimerReloadStrategyInterface;

/**
* Reload worker after $ttl working time
Expand Down
2 changes: 1 addition & 1 deletion src/BundledPlugin/Supervisor/SupervisorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Amp\Future;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Command\ProcessesCommand;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\Supervisor;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Status\SupervisorStatus;
use Luzrain\PHPStreamServer\Internal\Container;
use Luzrain\PHPStreamServer\Internal\Logger\LoggerInterface;
use Luzrain\PHPStreamServer\Internal\MasterProcess;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageBus;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageHandler;
use Luzrain\PHPStreamServer\Internal\Supervisor\Supervisor;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Process;
use Revolt\EventLoop\Suspension;
Expand Down
23 changes: 6 additions & 17 deletions src/BundledPlugin/Supervisor/WorkerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Luzrain\PHPStreamServer\BundledPlugin\Supervisor;

use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\Internal\ReloadStrategy\ReloadStrategyTrigger;
use Luzrain\PHPStreamServer\Internal\ErrorHandler;
use Luzrain\PHPStreamServer\Internal\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\Internal\ReloadStrategy\ReloadStrategyTrigger;
use Luzrain\PHPStreamServer\Process;
use Revolt\EventLoop;

Expand All @@ -15,13 +15,14 @@ class WorkerProcess extends Process
final public const RELOAD_EXIT_CODE = 100;
private const GC_PERIOD = 180;

private ReloadStrategyTrigger $reloadStrategyTrigger;
protected readonly ReloadStrategyTrigger $reloadStrategyTrigger;
private bool $isReloading = false;

/**
* @param null|\Closure(self):void $onStart
* @param null|\Closure(self):void $onStop
* @param null|\Closure(self):void $onReload
* @param array<ReloadStrategyInterface> $reloadStrategies
*/
public function __construct(
string $name = 'none',
Expand All @@ -32,6 +33,7 @@ public function __construct(
\Closure|null $onStart = null,
private readonly \Closure|null $onStop = null,
private readonly \Closure|null $onReload = null,
private readonly array $reloadStrategies = [],
) {
parent::__construct(name: $name, user: $user, group: $group, onStart: $onStart, onStop: $this->onStop(...));
}
Expand Down Expand Up @@ -60,27 +62,14 @@ protected function start(): void
\gc_mem_caches();
});

$this->reloadStrategyTrigger = new ReloadStrategyTrigger($this->reload(...));
$this->reloadStrategyTrigger = new ReloadStrategyTrigger($this->reload(...), $this->reloadStrategies);

EventLoop::setErrorHandler(function (\Throwable $exception) {
ErrorHandler::handleException($exception);
$this->reloadStrategyTrigger->emitEvent($exception);
});
}

public function addReloadStrategy(ReloadStrategyInterface ...$reloadStrategies): void
{
$this->reloadStrategyTrigger->addReloadStrategy(...$reloadStrategies);
}

/**
* @TODO get rid of this
*/
public function emitReloadEvent(mixed $event): void
{
$this->reloadStrategyTrigger->emitEvent($event);
}

public function stop(int $code = 0): void
{
$this->isReloading = $this->reloadable && $code === self::RELOAD_EXIT_CODE;
Expand Down

0 comments on commit cfd7f14

Please sign in to comment.