/** * {@inheritdoc} */ public function start(Timer $timer) { if (!isset($this->timers[$timer])) { $handle = \uv_timer_init($this->loopHandle); $interval = $timer->getInterval() * self::MILLISEC_PER_SEC; \uv_timer_start($handle, $interval, $timer->isPeriodic() ? $interval : 0, $this->callback); $this->timers[$timer] = $handle; $this->handles[(int) $handle] = $timer; } }
public function setInterval($callback, $delay) { $args = func_get_args(); array_shift($args); //skip $callback array_shift($args); //skip $delay $interval_id = uv_timer_init(); uv_timer_start($interval_id, $delay, $delay, function ($stat) use($callback, $args) { call_user_func_array($callback, $args); }); return $interval_id; }
/** * {@inheritdoc} */ protected function enableRepeatWatcher(TimerWatcher $watcher) { if ($watcher->event === null) { $watcher->event = \uv_timer_init($this->loop); } $this->timerWatchers[(int) $watcher->event] = $watcher; if (\uv_is_active($watcher->event)) { \uv_timer_stop($watcher->event); } $delay = (int) \ceil($watcher->delay * 1000); \uv_timer_start($watcher->event, $delay, $delay, $this->repeatCallback); }
/** * {@inheritdoc} */ public function listen(Io $io, float $timeout = 0) { $resource = $io->getResource(); $id = (int) $resource; if (!isset($this->sockets[$id]) || $io !== $this->sockets[$id]) { throw new FreedError(); } // If no poll handle exists for the socket, create one now. if (!isset($this->polls[$id])) { $this->polls[$id] = \uv_poll_init_socket($this->loopHandle, $resource); } // Begin polling for events. \uv_poll_start($this->polls[$id], $this->type, $this->pollCallback); // If a timeout is given, set up a separate timer for cancelling the poll in the future. if ($timeout) { if (self::MIN_TIMEOUT > $timeout) { $timeout = self::MIN_TIMEOUT; } if (!isset($this->timers[$id])) { $timer = \uv_timer_init($this->loopHandle); $this->handles[(int) $timer] = $id; $this->timers[$id] = $timer; } $timeout *= self::MILLISEC_PER_SEC; \uv_timer_start($this->timers[$id], $timeout, $timeout, $this->timerCallback); } elseif (isset($this->timers[$id]) && \uv_is_active($this->timers[$id])) { \uv_timer_stop($this->timers[$id]); } }
<?php $loop = uv_default_loop(); $timer = uv_timer_init(); $i = 0; uv_timer_start($timer, 1000, 1000, function ($stat) use(&$i, $timer, $loop) { echo "count: {$i}" . PHP_EOL; $i++; if ($i > 3) { uv_timer_stop($timer); uv_unref($timer); } }); uv_run(); echo "finished";
/** * {@inheritDoc} */ public function enable($watcherId) { if (!isset($this->watchers[$watcherId])) { return; } $watcher = $this->watchers[$watcherId]; if ($watcher->isEnabled) { return; } $watcher->isEnabled = true; $this->keepAliveCount += $watcher->keepAlive; switch ($watcher->type) { case Watcher::TIMER_ONCE: // fallthrough // fallthrough case Watcher::TIMER_REPEAT: \uv_timer_start($watcher->uvHandle, $watcher->msDelay, $watcher->msInterval, $watcher->callback); break; case Watcher::IO_READER: // fallthrough // fallthrough case Watcher::IO_WRITER: $this->enablePollFromWatcher($watcher); break; case Watcher::SIGNAL: \uv_signal_start($watcher->uvHandle, $watcher->callback, $watcher->signo); break; case Watcher::IMMEDIATE: $this->immediates[$watcherId] = $watcher; break; default: throw new \RuntimeException("Unexpected Watcher type encountered"); } }
$result = $store->get($request->getGet()->getKey()); break; } } catch (ProtocolBuffers\InvalidProtocoBufferException $e) { echo $e->getMessage(); } if ($result) { $response = new ProtocolCached_Response(); $response->setType(ProtocolCached_Response_ResponseType::GET); $payload = new ProtocolCached_GetResponse(); $payload->setValue($result); $response->setGet($payload); uv_write($socket, $response->serializeToString(), function ($socket, $stat) { echo "# Connection closed\n"; uv_close($socket); }); } else { echo "# Connection closed\n"; uv_close($socket); } }); }); $timer = uv_timer_init(); $stat = array(); $stat['begin'] = memory_get_usage(); uv_timer_start($timer, 10, 1000, function ($stat, $timer) use(&$stat, $store) { $stat["current"] = memory_get_usage(); printf("# memory: %d\n", $stat["current"] - $stat['begin']); printf("# collection size: %d\n", $store->count()); }); uv_run();
$processor = new Chatwork\Server\QueueProcessor($loop, $timer, $queue); $stat = new \Chatwork\Server\Statistics(); $container = array("chatwork" => $client, "queue" => $queue, "config" => $config, "stat" => $stat); foreach ((array) $config['plugins'] as $plugin_class) { $plugin_klass = new $plugin_class($container); $processor->addPlugin($plugin_klass); unset($plugin_klass); } $kernel = new Chatwork\Server\Kernel(); $kernel->setRouter(new \Chatwork\Server\Router()); $kernel->setContainer($container); foreach ((array) $config['providers'] as $provider_class) { $provider_klass = new $provider_class($container); $kernel->registerProviders(array($provider_klass)); } uv_timer_start($timer, SECONDS_MS, SECONDS_MS, array($processor, "process")); createServer(function ($request = array(), \Chatwork\Server\HttpResponse $response, $client) use($kernel, $stat) { parse_str(ltrim($request['QUERY_STRING'], "/?"), $params); $peer = uv_tcp_getpeername($client); $request['peer'] = $peer; try { $result = $kernel->process($request, $params); $response->writeHead(200, array("Content-Type" => "text/plain")); $response->write($result); $response->end(); $stat->increment("http.200"); } catch (\Chatwork\Exception\RouteNotFoundException $e) { $response->writeHead(404, array("Content-Type" => "text/html")); $response->write("<h3>Not Found</h3>"); $response->write("<div>" . $e->getMessage() . "</div>"); $response->end();
<?php $timer = uv_timer_init(); $stat = array(); $stat['begin'] = memory_get_usage(); uv_timer_start($timer, 10, 1000, function ($stat, $timer) use(&$stat) { $stat["current"] = memory_get_usage(); printf("memory: %d\n", $stat["current"] - $stat['begin']); });