diff --git a/src/Socket/Client.php b/src/Socket/Client.php index 61d0ec5..bd9a63e 100644 --- a/src/Socket/Client.php +++ b/src/Socket/Client.php @@ -27,13 +27,17 @@ final class Client implements Destroyable private \Closure $onClose; + private Timer $selectTimer; + /** * @param positive-int $payloadSize */ private function __construct( private readonly \Socket $socket, private readonly int $payloadSize, + float $selectPeriod, ) { + $this->selectTimer = new Timer($selectPeriod); \socket_set_nonblock($this->socket); $this->setOnPayload(static fn(string $payload) => null); $this->setOnClose(static fn() => null); @@ -41,12 +45,18 @@ private function __construct( /** * @param positive-int $payloadSize Max payload size. + * @param float $selectPeriod Time to wait between socket_select() calls in seconds. */ public static function init( \Socket $socket, int $payloadSize = 10485760, + float $selectPeriod = .001, ): self { - return new self($socket, $payloadSize); + return new self( + socket: $socket, + payloadSize: $payloadSize, + selectPeriod: $selectPeriod, + ); } public function destroy(): void @@ -103,6 +113,7 @@ public function process(): void throw new ClientDisconnected(); } \Fiber::suspend(); + $this->selectTimer->reset()->wait(); } while (true); } diff --git a/src/Socket/Server.php b/src/Socket/Server.php index 1500291..46a6315 100644 --- a/src/Socket/Server.php +++ b/src/Socket/Server.php @@ -97,7 +97,7 @@ public function process(): void $client = null; try { /** @psalm-suppress MixedArgument */ - $client = Client::init($socket, $this->payloadSize); + $client = Client::init($socket, $this->payloadSize, $this->acceptPeriod); $key = (int) \array_key_last($this->clients) + 1; $this->clients[$key] = $client; $this->clientInflector !== null and ($this->clientInflector)($client, $key);