Skip to content

Commit

Permalink
Plugin and Module interfaces rename
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Sep 4, 2024
1 parent c4267d0 commit 1306d60
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Luzrain\PHPStreamServer\Plugin\HttpServer\HttpServer;
use Luzrain\PHPStreamServer\Plugin\HttpServer\HttpServerModule;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Listen;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\WorkerProcess;
Expand All @@ -61,7 +61,7 @@ $server->addWorkersProcess(new WorkerProcess(
};
});

$worker->startPlugin(new HttpServer(
$worker->startWorkerModule(new HttpServerModule(
listen: new Listen(listen: '0.0.0.0:8087'),
requestHandler: $requestHandler,
));
Expand Down
20 changes: 10 additions & 10 deletions src/Internal/MasterProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Luzrain\PHPStreamServer\Internal\ServerStatus\ServerStatus;
use Luzrain\PHPStreamServer\Internal\Supervisor\Supervisor;
use Luzrain\PHPStreamServer\PeriodicProcess;
use Luzrain\PHPStreamServer\Plugin\Module;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\WorkerProcess;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,9 +41,9 @@ final class MasterProcess
private Scheduler $scheduler;

/**
* @var array<class-string<Module>, Module>
* @var array<class-string<Plugin>, Plugin>
*/
private array $modules = [];
private array $plugins = [];

public function __construct(
string|null $pidFile,
Expand Down Expand Up @@ -94,14 +94,14 @@ public function addPeriodicProcess(PeriodicProcess...$workers): void
}
}

public function addModules(Module ...$modules): void
public function addPlugins(Plugin ...$plugins): void
{
foreach ($modules as $module) {
foreach ($plugins as $module) {
$hash = \hash('xxh128', $module::class);
if (isset($this->modules[$hash])) {
if (isset($this->plugins[$hash])) {
throw new PHPStreamServerException('Can not load more than one instance of the same module');
}
$this->modules[$hash] = $module;
$this->plugins[$hash] = $module;
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ private function start(): void
\gc_mem_caches();
});

foreach ($this->modules as $module) {
foreach ($this->plugins as $module) {
$module->start($this);
}
}
Expand Down Expand Up @@ -256,7 +256,7 @@ public function stop(int $code = 0): void
$stopFutures = [];
$stopFutures[] = $this->supervisor->stop();
$stopFutures[] = $this->scheduler->stop();
foreach ($this->modules as $module) {
foreach ($this->plugins as $module) {
$stopFutures[] = $module->stop();
}

Expand Down Expand Up @@ -305,7 +305,7 @@ private function getStatus(): Status

private function free(): void
{
unset($this->modules);
unset($this->plugins);
unset($this->serverStatus);
unset($this->messageHandler);
unset($this->supervisor);
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/FileMonitor/FileMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Amp\Future;
use Luzrain\PHPStreamServer\Internal\MasterProcess;
use Luzrain\PHPStreamServer\Plugin\Module;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use function Amp\async;

final readonly class FileMonitor implements Module
final readonly class FileMonitor implements Plugin
{
public function __construct(
private string $sourceDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\ReloadStrategyTriggerMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\RequestsCounterMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Middleware\StaticMiddleware;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Plugin\WorkerModule;
use Luzrain\PHPStreamServer\WorkerProcess;
use Luzrain\PHPStreamServer\WorkerProcessInterface;

final readonly class HttpServer implements Plugin
final readonly class HttpServerModule implements WorkerModule
{
private const DEFAULT_TCP_BACKLOG = 65536;

/**
* @param Listen|array<Listen> $listen
* @param RequestHandler $requestHandler
* @param array<Middleware> $middleware
* @param positive-int|null $connectionLimit
* @param positive-int|null $connectionLimitPerIp
Expand Down
21 changes: 0 additions & 21 deletions src/Plugin/Module.php

This file was deleted.

13 changes: 11 additions & 2 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

namespace Luzrain\PHPStreamServer\Plugin;

use Luzrain\PHPStreamServer\WorkerProcessInterface;
use Amp\Future;
use Luzrain\PHPStreamServer\Internal\MasterProcess;

interface Plugin
{
public function start(WorkerProcessInterface $worker): void;
/**
* Initialize module before event loop starts
*/
public function start(MasterProcess $masterProcess): void;

/**
* If module has to finish some work right before server stop, master process will wait for it
*/
public function stop(): Future;
}
12 changes: 12 additions & 0 deletions src/Plugin/WorkerModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Plugin;

use Luzrain\PHPStreamServer\WorkerProcessInterface;

interface WorkerModule
{
public function start(WorkerProcessInterface $worker): void;
}
6 changes: 3 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Luzrain\PHPStreamServer\Console\App;
use Luzrain\PHPStreamServer\Internal\Logger\Logger;
use Luzrain\PHPStreamServer\Internal\MasterProcess;
use Luzrain\PHPStreamServer\Plugin\Module;
use Luzrain\PHPStreamServer\Plugin\Plugin;

final class Server
{
Expand Down Expand Up @@ -57,9 +57,9 @@ public function addPeriodicProcess(PeriodicProcess ...$workers): self
return $this;
}

public function addModules(Module ...$module): self
public function addPlugins(Plugin ...$plugin): self
{
$this->masterProcess->addModules(...$module);
$this->masterProcess->addPlugins(...$plugin);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/WorkerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Luzrain\PHPStreamServer\Internal\ServerStatus\Message\Heartbeat;
use Luzrain\PHPStreamServer\Internal\ServerStatus\Message\Spawn;
use Luzrain\PHPStreamServer\Internal\ServerStatus\TrafficStatus;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Plugin\WorkerModule;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategy;
use Revolt\EventLoop;
use Revolt\EventLoop\DriverFactory;
Expand Down Expand Up @@ -152,8 +152,8 @@ public function addReloadStrategies(ReloadStrategy ...$reloadStrategies): void
$this->reloadStrategyTrigger->addReloadStrategies(...$reloadStrategies);
}

public function startPlugin(Plugin $plugin): void
public function startWorkerModule(WorkerModule $module): void
{
$plugin->start($this);
$module->start($this);
}
}
6 changes: 3 additions & 3 deletions src/WorkerProcessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Luzrain\PHPStreamServer;

use Luzrain\PHPStreamServer\Internal\RunnableProcess;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\Plugin\WorkerModule;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategy;

interface WorkerProcessInterface extends ProcessInterface, RunnableProcess
Expand All @@ -26,7 +26,7 @@ public function reload(): void;
public function addReloadStrategies(ReloadStrategy ...$reloadStrategies): void;

/**
* Start plugin in this worker
* Start worker module in this worker
*/
public function startPlugin(Plugin $plugin): void;
public function startWorkerModule(WorkerModule $module): void;
}

0 comments on commit 1306d60

Please sign in to comment.