public static function enable($sec) { Gc::enable(); self::$task_ = Timer::getInstance()->runEvery($sec, function () { Gc::trigger(); }); }
<?php require dirname(__DIR__) . '/vendor/autoload.php'; use ServerBench\Core\Singleton; use ServerBench\Timer\Timer; $timer = new Timer(); function millitime() { return (int) (gettimeofday(true) * 1000); } $timer->runAfterMs(100, function () { echo "\nafter 100 ms\n"; }); $timer->runAtMs((int) millitime() + 200, function () { echo "\nat 200 ms\n"; }); $timer->runEveryMs(3000, function () { echo "\nevery 3000 ms\n"; }); while (!$timer->isEmpty()) { $timer->execute(); var_dump($timer->nearestTimeMs() - (int) (gettimeofday(true) * 1000)); usleep(100000); }
public function run() { $acceptor = $this->acceptor_; $ipc0 = $this->ipc0_; $ipc1 = $this->ipc1_; $timer = Timer::getInstance(); $poller = new ZMQPoll(); $poller->add($acceptor, ZMQ::POLL_IN); $poller->add($ipc1, ZMQ::POLL_IN); $loop = Loop::getInstance(); $loop->start(); while ($loop->running()) { // not execute timer too many times $timer->execute(64); $writable = []; $readable = []; try { if ($timer->isEmpty()) { $events = $poller->poll($readable, $writable, $this->max_wait_ms_); } else { $nearest_delta_ms = $timer->nearestDeltaTimeMs(); if ($this->max_wait_ms_ > 0 && $this->max_wait_ms_ < $nearest_delta_ms) { $real_wait_ms = $this->max_wait_ms_; } else { if ($nearest_delta_ms < 0) { $nearest_delta_ms = 0; } $real_wait_ms = $nearest_delta_ms; } $events = $poller->poll($readable, $writable, $real_wait_ms); } } catch (ZMQPollException $e) { if (4 == $e->getCode()) { continue; } throw $e; } if ($events > 0) { foreach ($readable as $socket) { if ($socket === $ipc1) { while (true) { try { $msg = $socket->recvMulti(ZMQ::MODE_NOBLOCK); if (false === $msg) { // would block break; } if (count($msg) !== 3 || !empty($msg[1])) { \ServerBench\syslog_error('invalid msg(%s) from worker.', [var_export($msg, true)]); break; } $rc = false; do { try { $rc = $acceptor->sendmulti([$msg[0], '', $msg[2]], ZMQ::MODE_NOBLOCK); } catch (ZMQSocketException $e) { if (4 == $e->getCode()) { continue; } throw $e; } break; } while (true); if (false === $rc) { \ServerBench\syslog_error('sending\'s mq is out of memory, msg(%s) would lose', [$msg[2]]); } } catch (ZMQSocketException $e) { if (4 == $e->getCode()) { continue; } throw $e; } } } elseif ($socket === $acceptor) { // acceptor for ($i = 0; $i < 4096; ++$i) { try { $msg = $acceptor->recvMulti(ZMQ::MODE_NOBLOCK); if (false === $msg) { break; } if (count($msg) !== 3 || !empty($msg[1])) { \ServerBench\syslog_error('invalid msg(%s) from client.', [var_export($msg, true)]); break; } $rc = false; do { try { $rc = $ipc0->sendmulti([$msg[0], '', $msg[2]], ZMQ::MODE_NOBLOCK); } catch (ZMQSocketException $e) { if (4 == $e->getCode()) { continue; } } break; } while (true); if (false === $rc) { \ServerBench\syslog_error('ipc0\'s mq is out of memory, msg(%s) would lose.', [var_export($msg, true)]); } } catch (ZMQSocketException $e) { if (4 == $e->getCode()) { continue; } throw $e; } } } else { // unexpected branch. } } } } }
public function run() { $ipc0 = $this->ipc0_; $ipc1 = $this->ipc1_; $timer = Timer::getInstance(); $loop = Loop::getInstance(); $poller = new ZMQPoll(); $poller->add($ipc0, ZMQ::POLL_IN); while ($loop->running()) { // not execute timer too many times $timer->execute(64); $readable = []; $writable = []; try { if ($timer->isEmpty()) { $events = $poller->poll($readable, $writable, $this->max_wait_ms_); } else { $nearest_delta_ms = $timer->nearestDeltaTimeMs(); if ($this->max_wait_ms_ > 0 && $this->max_wait_ms_ < $nearest_delta_ms) { $real_wait_ms = $this->max_wait_ms_; } else { if ($nearest_delta_ms < 0) { $nearest_delta_ms = 0; } $real_wait_ms = $nearest_delta_ms; } $events = $poller->poll($readable, $writable, $real_wait_ms); } if ($events <= 0) { continue; } } catch (ZMQPollException $e) { if (4 == $e->getCode()) { continue; } throw $e; } while (true) { try { $msg = $ipc0->recvMulti(ZMQ::MODE_NOBLOCK); if (false === $msg) { // would block break; } if (count($msg) !== 3 || !empty($msg[1])) { break; } try { $reply = call_user_func($this->message_callback_, $msg[2]); } catch (\Exception $e) { \ServerBench\syslog_error('caught exception from message_callback: %s', [$e]); continue; } $rc = false; do { try { $rc = $ipc1->sendmulti([$msg[0], '', $reply]); } catch (ZMQPollException $e) { if (4 == $e->getCode()) { continue; } throw $e; } break; } while (true); if (false === $rc) { \ServerBench\syslog_error('replying\'s mq of worker is out of memory, msg(%s) would lose', [$reply]); } } catch (ZMQSocketException $e) { if (4 == $e->getCode()) { continue; } throw $e; } } } }