Search code examples
phpwebsocketratchetreactphp

Maximum interval on react loop->addPeriodicTimer is 2147 seconds


I'm running a timer using react\eventloop on my ratchet wamp app. I would like to have it run hourly at 3600 seconds, but for some reason if I set the interval higher than 2147 seconds I get this warning:

Warning: stream_select(): The microseconds parameter must be greater than 0 in C
:\wamp\www\vendor\react\event-loop\StreamSelectLoop.php on line 255

What's so special about 2147 seconds? And what can I do to bypass this contraint?

The Event Handler

class Pusher implements WampServerInterface, MessageComponentInterface {   
    private $loop;
}

public function __construct(LoopInterface $loop) {
    $this->loop = $loop;

    $this->loop->addPeriodicTimer(2147, function() {
         //code   
    });
}

public function onSubscribe(ConnectionInterface $conn, $topic) {}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {}
public function onOpen(ConnectionInterface $conn) {}
public function onClose(ConnectionInterface $conn) {}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {}
public function onPublish(ConnectionInterface $conn, $topic, $event {}
public function onError(ConnectionInterface $conn, \Exception $e) {}

The Server

$loop = Factory::create(); 

$webSock = new Server($loop);
$webSock->listen(8080, '0.0.0.0'); 

new IoServer(
    new HttpServer(
        new WsServer(
            new SessionProvider(
                new WampServer(
                    new Pusher($loop)),                 
                $sesshandler
            )
        )

    ),
    $webSock
);

$loop->run();

Solution

  • It is because of the limit of PHP integers on 32-bit platforms.

    2147 (seconds) * 1000000 (microseconds in one second) ~= PHP_INT_MAX on 32-bit platforms. On 64-bit platforms the limit would be ~ 300k years.

    The strange thing is that React's React\EventLoop\StreamSelectLoop calls stream_select() only with microseconds parameter, while it also accepts seconds. Maybe they should fix this issue. As a workaround you could override StreamSelectLoop implementation so that it make use of $tv_sec parameter in stream_select().


    I created a pull request, let's see if it will be accepted