Skip to content

Commit

Permalink
TrafficCountingClientFactory decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Sep 27, 2024
1 parent cf113ce commit c240277
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
16 changes: 9 additions & 7 deletions src/Plugin/HttpServer/HttpServerModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\ClientExceptionHandleMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\ReloadStrategyTriggerMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\Middleware\RequestsCounterMiddleware;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\TrafficCountingClientFactory;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Internal\TrafficCountingSocketFactory;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Middleware\StaticMiddleware;
use Luzrain\PHPStreamServer\Plugin\WorkerModule;
use Luzrain\PHPStreamServer\ReloadStrategy\ReloadStrategyAwareInterface;
use Luzrain\PHPStreamServer\WorkerProcessInterface;

final readonly class HttpServerModule implements WorkerModule
Expand Down Expand Up @@ -58,15 +60,13 @@ public function __construct(

public function start(WorkerProcessInterface $worker): void
{
$serverSocketFactory = new ResourceServerSocketFactory();
$middleware = [];

$networkTrafficCounter = $worker instanceof NetworkTrafficCounterAwareInterface ? $worker->getNetworkTrafficCounter() : null;
$serverSocketFactory = new ResourceServerSocketFactory();

$clientFactory = new HttpClientFactory(
logger: $worker->getLogger(),
connectionLimitPerIp: $this->connectionLimitPerIp,
trafficStatisticStore: $networkTrafficCounter,
onConnectCallback: $this->onConnect,
onCloseCallback: $this->onClose,
);
Expand All @@ -75,16 +75,18 @@ public function start(WorkerProcessInterface $worker): void
$serverSocketFactory = new ConnectionLimitingServerSocketFactory(new LocalSemaphore($this->connectionLimit), $serverSocketFactory);
}

if ($networkTrafficCounter !== null) {
$serverSocketFactory = new TrafficCountingSocketFactory($worker->getNetworkTrafficCounter(), $serverSocketFactory);
$middleware[] = new RequestsCounterMiddleware($worker->getNetworkTrafficCounter());
if ($worker instanceof NetworkTrafficCounterAwareInterface) {
$networkTrafficCounter = $worker->getNetworkTrafficCounter();
$serverSocketFactory = new TrafficCountingSocketFactory($serverSocketFactory, $networkTrafficCounter);
$clientFactory = new TrafficCountingClientFactory($clientFactory, $networkTrafficCounter);
$middleware[] = new RequestsCounterMiddleware($networkTrafficCounter);
}

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

if ($networkTrafficCounter !== null) {
if ($worker instanceof ReloadStrategyAwareInterface) {
$middleware[] = new ReloadStrategyTriggerMiddleware($worker);
}

Expand Down
27 changes: 8 additions & 19 deletions src/Plugin/HttpServer/Internal/HttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Amp\Http\Server\Driver\ConnectionLimitingClientFactory;
use Amp\Http\Server\Driver\SocketClientFactory;
use Amp\Socket\Socket;
use Luzrain\PHPStreamServer\Internal\ServerStatus\NetworkTrafficCounter;
use Psr\Log\LoggerInterface;

final readonly class HttpClientFactory implements ClientFactory
Expand All @@ -19,7 +18,6 @@
public function __construct(
LoggerInterface $logger,
int|null $connectionLimitPerIp,
private NetworkTrafficCounter|null $trafficStatisticStore = null,
private \Closure|null $onConnectCallback = null,
private \Closure|null $onCloseCallback = null,
) {
Expand All @@ -36,29 +34,20 @@ public function createClient(Socket $socket): Client|null
{
$client = $this->clientFactory->createClient($socket);

if ($client !== null) {
$this->onConnect($socket, $client);
$client->onClose(fn() => $this->onClose($socket, $client));
if ($client === null) {
return null;
}

return $client;
}

private function onConnect(Socket $socket, Client $client): void
{
$this->trafficStatisticStore?->addConnection($socket);

if ($this->onConnectCallback !== null) {
($this->onConnectCallback)($client);
}
}

private function onClose(Socket $socket, Client $client): void
{
$this->trafficStatisticStore?->removeConnection($socket);
$client->onClose(function () use ($socket, $client) {
if ($this->onCloseCallback !== null) {
($this->onCloseCallback)($client);
}
});

if ($this->onCloseCallback !== null) {
($this->onCloseCallback)($client);
}
return $client;
}
}
36 changes: 36 additions & 0 deletions src/Plugin/HttpServer/Internal/TrafficCountingClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Plugin\HttpServer\Internal;

use Amp\Http\Server\Driver\Client;
use Amp\Http\Server\Driver\ClientFactory;
use Amp\Socket\Socket;
use Luzrain\PHPStreamServer\Internal\ServerStatus\NetworkTrafficCounter;

final readonly class TrafficCountingClientFactory implements ClientFactory
{
public function __construct(
private ClientFactory $clientFactory,
private NetworkTrafficCounter $trafficStatisticStore,
) {
}

public function createClient(Socket $socket): Client|null
{
$client = $this->clientFactory->createClient($socket);

if ($client === null) {
return null;
}

$this->trafficStatisticStore->addConnection($socket);

$client->onClose(function () use ($socket) {
$this->trafficStatisticStore->removeConnection($socket);
});

return $client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
final readonly class TrafficCountingSocketFactory implements ServerSocketFactory
{
public function __construct(
private NetworkTrafficCounter $trafficStatisticStore,
private ServerSocketFactory $socketServerFactory,
private NetworkTrafficCounter $trafficStatisticStore,
) {
}

Expand Down

0 comments on commit c240277

Please sign in to comment.