Skip to content

Commit

Permalink
Plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Jul 29, 2024
1 parent 21872da commit ed516ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Internal/WorkerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class WorkerProcess implements RunnableProcess, WorkerProcessInterface
public readonly int $id;
public readonly int $pid;
private int $exitCode = 0;
public LoggerInterface $logger;
private LoggerInterface $logger;
public TrafficStatus $trafficStatus;
public ReloadStrategyTrigger $reloadStrategyTrigger;

Expand Down
17 changes: 10 additions & 7 deletions src/Plugin/HttpServer/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\RequestsCounterMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Middleware\StaticMiddleware;
use Luzrain\PHPStreamServer\Plugin\Plugin;
use Luzrain\PHPStreamServer\WorkerProcessInterface;

final readonly class HttpServer implements Plugin
{
Expand Down Expand Up @@ -53,33 +54,35 @@ public function __construct(
) {
}

public function start(WorkerProcess $workerProcess): void
public function start(WorkerProcessInterface $worker): void
{
$serverSocketFactory = new HttpServerSocketFactory($this->connectionLimit, $workerProcess->trafficStatus);
$clientFactory = new HttpClientFactory($workerProcess->logger, $this->connectionLimitPerIp, $workerProcess->trafficStatus, $this->onConnect, $this->onClose);
\assert($worker instanceof WorkerProcess);

$serverSocketFactory = new HttpServerSocketFactory($this->connectionLimit, $worker->trafficStatus);
$clientFactory = new HttpClientFactory($worker->getLogger(), $this->connectionLimitPerIp, $worker->trafficStatus, $this->onConnect, $this->onClose);
$middleware = [];

if ($this->concurrencyLimit !== null) {
$middleware[] = new ConcurrencyLimitingMiddleware($this->concurrencyLimit);
}

$middleware[] = new RequestsCounterMiddleware($workerProcess->trafficStatus);
$middleware[] = new RequestsCounterMiddleware($worker->trafficStatus);
$middleware[] = new ClientExceptionHandleMiddleware();
$middleware[] = new ReloadStrategyTriggerMiddleware($workerProcess->reloadStrategyTrigger);
$middleware[] = new ReloadStrategyTriggerMiddleware($worker->reloadStrategyTrigger);
$middleware[] = new AddServerHeadersMiddleware();
\array_push($middleware, ...$this->middleware);

// Force move StaticMiddleware to the end of the chain
\usort($middleware, fn (mixed $a): int => $a instanceof StaticMiddleware ? 1 : -1);

$socketHttpServer = new SocketHttpServer(
logger: $workerProcess->logger,
logger: $worker->getLogger(),
serverSocketFactory: $serverSocketFactory,
clientFactory: $clientFactory,
middleware: $middleware,
allowedMethods: null,
httpDriverFactory: new DefaultHttpDriverFactory(
logger: $workerProcess->logger,
logger: $worker->getLogger(),
streamTimeout: $this->connectionTimeout,
connectionTimeout: $this->connectionTimeout,
headerSizeLimit: $this->headerSizeLimit,
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Luzrain\PHPStreamServer\Plugin;

use Luzrain\PHPStreamServer\Internal\WorkerProcess;
use Luzrain\PHPStreamServer\WorkerProcessInterface;

interface Plugin
{
public function start(WorkerProcess $workerProcess): void;
public function start(WorkerProcessInterface $worker): void;
}

0 comments on commit ed516ad

Please sign in to comment.