Skip to content

Commit

Permalink
Remove $eventCode from reload strategies. Made classes final. Change …
Browse files Browse the repository at this point in the history
…namespaces
  • Loading branch information
luzrain committed Sep 6, 2024
1 parent a50d73b commit 8c3761e
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 81 deletions.
16 changes: 0 additions & 16 deletions src/ReloadStrategy/EachRequestReloadStrategy.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/ReloadStrategy/ReloadStrategy.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/ReloadStrategy/ReloadStrategyAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ReloadStrategyAwareInterface
/**
* Add reload strategy for worker
*/
public function addReloadStrategy(ReloadStrategy ...$reloadStrategies): void;
public function addReloadStrategy(ReloadStrategyInterface ...$reloadStrategies): void;

/**
* Emit event for checking by reload strategies
Expand Down
15 changes: 15 additions & 0 deletions src/ReloadStrategy/ReloadStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;

interface ReloadStrategyInterface
{
/**
* If the method returns true, the worker should be reloaded immediately.
*
* @param mixed $eventObject could be a request object, exception object, or null, depending on the eventCode.
*/
public function shouldReload(mixed $eventObject = null): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Internal;
namespace Luzrain\PHPStreamServer\ReloadStrategy;

use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategy;
use Luzrain\PHPStreamServer\ReloadStrategy\TimerReloadStrategy;
use Revolt\EventLoop;

final class ReloadStrategyTrigger
{
/** @var array<ReloadStrategy> */
/** @var list<ReloadStrategyInterface> */
private array $reloadStrategies = [];

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

public function addReloadStrategy(ReloadStrategy ...$reloadStrategies): void
public function addReloadStrategy(ReloadStrategyInterface ...$reloadStrategies): void
{
foreach ($reloadStrategies as $reloadStrategy) {
if ($reloadStrategy instanceof TimerReloadStrategy) {
if ($reloadStrategy instanceof TimerReloadStrategyInterface) {
EventLoop::repeat($reloadStrategy->getInterval(), function () use ($reloadStrategy): void {
$reloadStrategy->shouldReload($reloadStrategy::EVENT_CODE_TIMER) && $this->reload();
$reloadStrategy->shouldReload() && $this->reload();
});
} else {
$this->reloadStrategies[] = $reloadStrategy;
}
}
}

public function emitEvent(mixed $request): void
public function emitEvent(mixed $event): void
{
foreach ($this->reloadStrategies as $reloadStrategy) {
$eventCode = $request instanceof \Throwable ? ReloadStrategy::EVENT_CODE_EXCEPTION : ReloadStrategy::EVENT_CODE_REQUEST;
if ($reloadStrategy->shouldReload($eventCode, $request)) {
if ($reloadStrategy->shouldReload($event)) {
$this->reload();
break;
}
Expand Down
19 changes: 19 additions & 0 deletions src/ReloadStrategy/Strategy/EachRequestReloadStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy\Strategy;

use Amp\Http\Server\Request;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyInterface;

/**
* Reload worker after each request.
*/
final class EachRequestReloadStrategy implements ReloadStrategyInterface
{
public function shouldReload(mixed $eventObject = null): bool
{
return $eventObject instanceof Request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\ReloadStrategy\Strategy;

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

/**
* Reload worker each time after exception occurs
*/
class ExceptionReloadStrategy implements ReloadStrategy
final class ExceptionReloadStrategy implements ReloadStrategyInterface
{
/** @var array<class-string<\Throwable>> */
private array $allowedExceptions = [
Expand All @@ -26,9 +27,9 @@ public function __construct(array $allowedExceptions = [])
\array_push($this->allowedExceptions, ...$allowedExceptions);
}

public function shouldReload(int $eventCode, mixed $eventObject = null): bool
public function shouldReload(mixed $eventObject = null): bool
{
if ($eventCode !== self::EVENT_CODE_EXCEPTION) {
if (!$eventObject instanceof \Throwable) {
return false;
}

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

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\ReloadStrategy\Strategy;

use Luzrain\PHPStreamServer\ReloadStrategy\TimerReloadStrategyInterface;

/**
* Reload worker if worker memory usage has increased $maxMemory value
*/
class MaxMemoryReloadStrategy implements TimerReloadStrategy
final class MaxMemoryReloadStrategy implements TimerReloadStrategyInterface
{
private const TIMER_INTERVAL = 30;

Expand All @@ -20,12 +22,8 @@ public function getInterval(): int
return self::TIMER_INTERVAL;
}

public function shouldReload(int $eventCode, mixed $eventObject = null): bool
public function shouldReload(mixed $eventObject = null): bool
{
if ($eventCode !== self::EVENT_CODE_TIMER) {
return false;
}

return \max(\memory_get_peak_usage(), \memory_get_usage()) > $this->maxMemory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\ReloadStrategy\Strategy;

use Amp\Http\Server\Request;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyInterface;

/**
* Reload worker on every $maxRequests requests.
* To prevent simultaneous restart of all workers $dispersionPercentage can be set.
* 1000 $maxRequests and 20% $dispersionPercentage will restart between 800 and 1000
*/
class MaxRequestsReloadStrategy implements ReloadStrategy
final class MaxRequestsReloadStrategy implements ReloadStrategyInterface
{
private int $requestsCount = 0;
private readonly int $maxRequests;
Expand All @@ -20,8 +23,8 @@ public function __construct(int $maxRequests, int $dispersionPercentage = 0)
$this->maxRequests = \random_int($minRequests, $maxRequests);
}

public function shouldReload(int $eventCode, mixed $eventObject = null): bool
public function shouldReload(mixed $eventObject = null): bool
{
return $eventCode === self::EVENT_CODE_REQUEST && ++$this->requestsCount > $this->maxRequests;
return $eventObject instanceof Request && ++$this->requestsCount > $this->maxRequests;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\ReloadStrategy;
namespace Luzrain\PHPStreamServer\ReloadStrategy\Strategy;

use Luzrain\PHPStreamServer\ReloadStrategy\TimerReloadStrategyInterface;

/**
* Reload worker after $ttl working time
*/
class TTLReloadStrategy implements TimerReloadStrategy
final class TTLReloadStrategy implements TimerReloadStrategyInterface
{
/**
* @param int $ttl TTL in seconds
Expand All @@ -21,8 +23,8 @@ public function getInterval(): int
return $this->ttl;
}

public function shouldReload(int $eventCode, mixed $eventObject = null): bool
public function shouldReload(mixed $eventObject = null): bool
{
return $eventCode === self::EVENT_CODE_TIMER;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Luzrain\PHPStreamServer\ReloadStrategy;

interface TimerReloadStrategy extends ReloadStrategy
interface TimerReloadStrategyInterface extends ReloadStrategyInterface
{
/**
* Strategy will be triggered repeatedly every N seconds.
Expand Down
6 changes: 3 additions & 3 deletions src/WorkerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageBus;
use Luzrain\PHPStreamServer\Internal\MessageBus\SocketFileMessageBus;
use Luzrain\PHPStreamServer\Internal\ProcessTrait;
use Luzrain\PHPStreamServer\Internal\ReloadStrategyTrigger;
use Luzrain\PHPStreamServer\Internal\ServerStatus\Message\Detach;
use Luzrain\PHPStreamServer\Internal\ServerStatus\Message\Heartbeat;
use Luzrain\PHPStreamServer\Internal\ServerStatus\Message\Spawn;
use Luzrain\PHPStreamServer\Internal\ServerStatus\TrafficStatus;
use Luzrain\PHPStreamServer\Internal\ServerStatus\TrafficStatusAwareInterface;
use Luzrain\PHPStreamServer\Plugin\WorkerModule;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategy;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyInterface;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyAwareInterface;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyTrigger;
use Revolt\EventLoop;
use Revolt\EventLoop\DriverFactory;

Expand Down Expand Up @@ -152,7 +152,7 @@ public function startWorkerModule(WorkerModule $module): void
$module->start($this);
}

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

0 comments on commit 8c3761e

Please sign in to comment.