Skip to content

Commit

Permalink
feat(config): Add env TRAP_TCP_POLLING_INTERVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 25, 2024
1 parent 0a73206 commit b75e890
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Buggregator\Trap\Config\Server\Files\XHProf as XHProfFileConfig;
use Buggregator\Trap\Config\Server\Frontend as FrontendConfig;
use Buggregator\Trap\Config\Server\SocketServer;
use Buggregator\Trap\Config\Server\TcpPorts;
use Buggregator\Trap\Handler\Http\Handler\Websocket;
use Buggregator\Trap\Handler\Http\Middleware;
use Buggregator\Trap\Proto\Buffer;
Expand Down Expand Up @@ -195,7 +196,7 @@ function (): void {
* @param Inspector $inspector
* @return \Fiber
*/
public function prepareServerFiber(SocketServer $config, Inspector $inspector, Logger $logger): \Fiber
private function prepareServerFiber(SocketServer $config, Inspector $inspector, Logger $logger): \Fiber

Check warning on line 199 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L199

Added line #L199 was not covered by tests
{
return $this->fibers[] = new \Fiber(function () use ($config, $inspector, $logger): void {
do {
Expand All @@ -210,7 +211,7 @@ public function prepareServerFiber(SocketServer $config, Inspector $inspector, L
});
}

public function configureFrontend(bool $separated): void
private function configureFrontend(bool $separated): void

Check warning on line 214 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L214

Added line #L214 was not covered by tests
{
$this->processors[] = $this->senders[] = $wsSender = Sender\FrontendSender::create($this->logger);
$this->container->set($wsSender);
Expand All @@ -230,8 +231,14 @@ public function configureFrontend(bool $separated): void
),
]);
$this->processors[] = $inspector;
/** @var TcpPorts $tcpConfig */
$tcpConfig = $this->container->get(TcpPorts::class);

Check warning on line 235 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L235

Added line #L235 was not covered by tests
$config = $this->container->get(FrontendConfig::class);
$this->prepareServerFiber(new SocketServer(port: $config->port), $inspector, $this->logger);
$this->prepareServerFiber(
new SocketServer(port: $config->port, pollingInterval: $tcpConfig->pollingInterval),
$inspector,
$this->logger,

Check warning on line 240 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L237-L240

Added lines #L237 - L240 were not covered by tests
);
}

/**
Expand Down Expand Up @@ -260,7 +267,7 @@ private function createServer(SocketServer $config, Inspector $inspector): Serve
return Server::init(
$config->port,
payloadSize: 524_288,
acceptPeriod: .001,
acceptPeriod: \max(50, $config->pollingInterval) / 1e6,

Check warning on line 270 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L270

Added line #L270 was not covered by tests
clientInflector: $clientInflector,
logger: $this->logger,
);
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function configure(): void
*/
public function getServers(Container $container): array
{
/** @var TcpPorts $config */
$config = $container->get(TcpPorts::class);

$servers = [];
Expand All @@ -73,7 +74,7 @@ public function getServers(Container $container): array
$port > 0 && $port < 65536 or throw new \InvalidArgumentException(
\sprintf('Invalid port `%s`. It must be in range 1-65535.', $port),
);
$servers[] = new SocketServer($port, $config->host, $config->type);
$servers[] = new SocketServer($port, $config->host, $config->type, $config->pollingInterval);

Check warning on line 77 in src/Command/Run.php

View check run for this annotation

Codecov / codecov/patch

src/Command/Run.php#L77

Added line #L77 was not covered by tests
}
return $servers;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Config/Server/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ final class SocketServer
{
/**
* @param int<1, 65535> $port
* @param int<50, max> $pollingInterval Time to wait between socket_accept() and socket_select() calls
* in microseconds.
*/
public function __construct(
public readonly int $port,
public readonly string $host = '127.0.0.1',
public readonly string $type = 'tcp',
public int $pollingInterval = 1_000,
) {}
}
8 changes: 8 additions & 0 deletions src/Config/Server/TcpPorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ final class TcpPorts
public string $host = '127.0.0.1';

public string $type = 'tcp';

/**
* Time to wait between socket_accept() and socket_select() calls in microseconds.
*
* @var int<50, max>
*/
#[Env('TRAP_TCP_POLLING_INTERVAL')]
public int $pollingInterval = 1_000;
}

0 comments on commit b75e890

Please sign in to comment.