diff --git a/src/Application.php b/src/Application.php index d9d566ed..3c4d7ed8 100644 --- a/src/Application.php +++ b/src/Application.php @@ -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; @@ -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 { return $this->fibers[] = new \Fiber(function () use ($config, $inspector, $logger): void { do { @@ -210,7 +211,7 @@ public function prepareServerFiber(SocketServer $config, Inspector $inspector, L }); } - public function configureFrontend(bool $separated): void + private function configureFrontend(bool $separated): void { $this->processors[] = $this->senders[] = $wsSender = Sender\FrontendSender::create($this->logger); $this->container->set($wsSender); @@ -230,8 +231,14 @@ public function configureFrontend(bool $separated): void ), ]); $this->processors[] = $inspector; + /** @var TcpPorts $tcpConfig */ + $tcpConfig = $this->container->get(TcpPorts::class); $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, + ); } /** @@ -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, clientInflector: $clientInflector, logger: $this->logger, ); diff --git a/src/Command/Run.php b/src/Command/Run.php index 62ddb1a0..a940ed80 100644 --- a/src/Command/Run.php +++ b/src/Command/Run.php @@ -60,6 +60,7 @@ public function configure(): void */ public function getServers(Container $container): array { + /** @var TcpPorts $config */ $config = $container->get(TcpPorts::class); $servers = []; @@ -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); } return $servers; } diff --git a/src/Config/Server/SocketServer.php b/src/Config/Server/SocketServer.php index 9f47d9d7..26c6781a 100644 --- a/src/Config/Server/SocketServer.php +++ b/src/Config/Server/SocketServer.php @@ -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, ) {} } diff --git a/src/Config/Server/TcpPorts.php b/src/Config/Server/TcpPorts.php index dab78ed6..d609cefb 100644 --- a/src/Config/Server/TcpPorts.php +++ b/src/Config/Server/TcpPorts.php @@ -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; }