Exemple #1
5
 function enableForConsumer($consumerId, $interval, $probeKey)
 {
     $_this = $this;
     $this->enabled = true;
     if (!$this->instant) {
         // register consumer
         $newConsumerId = !isset($this->consumerTimers[$consumerId]);
         if ($interval > 0 && $newConsumerId) {
             $this->consumerTimers[$consumerId] = setInterval(function () use($_this, $consumerId) {
                 $_this->sample($consumerId);
             }, $interval);
         }
         // enable with delayed disabling
         if (!empty($this->disableDelay)) {
             clearTimeout($this->disableDelay);
         }
         $this->disableDelay = setTimeout(function () use($_this) {
             //echo "\ndisabled " . $_this->name;
             $_this->enabled = false;
             foreach ($_this->consumerTimers as $consumerId => $timer) {
                 clearInterval($timer);
             }
             $_this->consumerTimers = array();
         }, PROBE_DISABLE_DELAY);
     }
 }
 /**
  * Called when the worker is ready to go.
  * @return void
  */
 public function onReady()
 {
     $appInstance = $this;
     // a reference to this application instance for ExampleWebSocketRoute
     \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('ExamplePubSub', function ($client) use($appInstance) {
         return new ExamplePubSubWebSocketRoute($client, $appInstance);
     });
     $this->sql = \PHPDaemon\Clients\MySQL\Pool::getInstance();
     $this->pubsub = new \PHPDaemon\PubSub\PubSub();
     $this->pubsub->addEvent('usersNum', \PHPDaemon\PubSub\PubSubEvent::init()->onActivation(function ($pubsub) use($appInstance) {
         \PHPDaemon\Core\Daemon::log('onActivation');
         if (isset($pubsub->event)) {
             \PHPDaemon\Core\Timer::setTimeout($pubsub->event, 0);
             return;
         }
         $pubsub->event = setTimeout(function ($timer) use($pubsub, $appInstance) {
             $appInstance->sql->getConnection(function ($sql) use($pubsub) {
                 if (!$sql->connected) {
                     return;
                 }
                 $sql->query('SELECT COUNT(*) `num` FROM `dle_users`', function ($sql, $success) use($pubsub) {
                     $pubsub->pub(sizeof($sql->resultRows) ? $sql->resultRows[0]['num'] : 'null');
                 });
             });
             $timer->timeout(5000000.0);
             // 5 seconds
         }, 0);
     })->onDeactivation(function ($pubsub) {
         if (isset($pubsub->event)) {
             \PHPDaemon\Core\Timer::cancelTimeout($pubsub->event);
         }
     }));
 }
Exemple #3
0
 /**
  * Called when the worker is ready to go
  * @return void
  */
 public function onReady()
 {
     $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance();
     $this->redis->multi(function ($multi) {
         // "OK"
         D('start multi: ' . $multi->result);
         $multi->set('test1', 'value1', function ($redis) use($multi) {
             // "QUEUED"
             D('in multi 1: ' . $redis->result);
             $this->redis->set('test1', 'value1-new', function ($redis) {
                 // "OK", not "QUEUED"
                 D('out multi 1: ' . $redis->result);
             });
             setTimeout(function ($timer) use($multi) {
                 // "QUEUED"
                 $multi->set('test2', 'value2', function ($redis) use($multi) {
                     D('in multi 2: ' . $redis->result);
                     $multi->exec(function ($redis) {
                         D('exec');
                         D($redis->result);
                     });
                 });
                 $timer->free();
             }, 200000.0);
         });
     });
 }
Exemple #4
0
 /**
  * Called when the connection is handshaked (at low-level), and peer is ready to recv. data
  * @return void
  */
 public function onReady()
 {
     $conn = $this;
     $this->keepaliveTimer = setTimeout(function ($timer) use($conn) {
         $conn->ping();
     }, 10000000.0);
 }
 public function onReady()
 {
     $appInstance = $this;
     setTimeout(function ($event) use($appInstance) {
         $appInstance->broadcastCall('hello', [\PHPDaemon\Core\Daemon::$process->getPid()]);
         $event->finish();
     }, 2000000.0);
 }
Exemple #6
0
 /**
  * Called when the worker is ready to go.
  * @return void
  */
 public function onReady()
 {
     if ($this->config->enable->value) {
         $this->timer = setTimeout(function ($timer) {
             $this->touchCursor();
         }, 300000.0);
     }
 }
Exemple #7
0
 /**
  * Called when the connection is handshaked (at low-level), and peer is ready to recv. data
  * @return void
  */
 public function onReady()
 {
     $this->createXMLStream();
     $this->startXMLStream();
     $this->keepaliveTimer = setTimeout(function ($timer) {
         $this->ping();
     }, 1000000.0 * 30);
 }
Exemple #8
0
 /**
  * Called when the worker is ready to go.
  * @return void
  */
 public function onReady()
 {
     if ($this->isEnabled()) {
         $this->updateTimer = setTimeout(function ($timer) {
             $this->updateAllServers();
             $timer->timeout(2000000.0);
         }, 1);
     }
 }
Exemple #9
0
 /**
  * Constructor
  * @return void
  */
 public function init()
 {
     $this->sessId = $this->attrs->sessId;
     $this->serverId = $this->attrs->serverId;
     $this->path = $this->attrs->path;
     // @TODO: revert timeout after request
     $this->upstream->setTimeouts($this->appInstance->config->networktimeoutread->value, $this->appInstance->config->networktimeoutwrite->value);
     $this->opts = $this->appInstance->getRouteOptions($this->attrs->path);
     $this->CORS();
     if ($this->isFinished()) {
         return;
     }
     if ($this->opts['cookie_needed'] && !$this instanceof Info) {
         if (isset($_COOKIE['JSESSIONID'])) {
             $val = $_COOKIE['JSESSIONID'];
             if (!is_string($val) || $val === '') {
                 $val = 'dummy';
             }
         } else {
             $val = 'dummy';
         }
         $this->setcookie('JSESSIONID', $val, 0, '/');
     }
     $this->contentType($this->contentType);
     if (!$this->cacheable) {
         $this->noncache();
         $this->header('X-Accel-Buffering: no');
     }
     if ($this->callbackParamEnabled) {
         if (!isset($_GET['c']) || !is_string($_GET['c']) || preg_match('~[^_\\.a-zA-Z0-9]~', $_GET['c'])) {
             $this->header('500 Internal Server Error');
             $this->out('"callback" parameter required');
             $this->finish();
             return;
         }
     }
     if (($f = $this->appInstance->config->heartbeatinterval->value) > 0) {
         $this->heartbeatTimer = setTimeout(function ($timer) {
             if (in_array('one-by-one', $this->pollMode)) {
                 $this->heartbeatOnFinish = true;
                 $this->stop();
                 return;
             }
             $this->sendFrame('h');
             if ($this->gcEnabled) {
                 $this->gcCheck();
             }
         }, $f * 1000000.0);
     }
     $this->afterHeaders();
     if ($this->poll) {
         $this->acquire(function () {
             $this->poll();
         });
     }
 }
Exemple #10
0
 /**
  * Manager constructor.
  * @param $path
  */
 public function __construct($path, $threadsNum)
 {
     $this->threadsNum = $threadsNum;
     $this->path = $path;
     $this->queue = new StackCallbacks();
     $this->reloadCheckTimer = setTimeout(function ($ev) {
         FileSystem::stat($this->path, function ($path, $stat) {
             if (!$stat) {
                 $this->stop();
                 $this->free();
             }
             if ($this->reloadCheckLastModified >= $stat['mtime']) {
                 return;
             }
             $this->reloadCheckLastModified = $stat['mtime'];
             $this->stop();
             $this->start();
         });
         $ev->timeout(static::RELOAD_CHECK_INTERVAL);
     }, 1);
 }
Exemple #11
0
 /**
  * __construct
  * @param Application $appInstance [@todo description]
  * @param string $id [@todo description]
  * @param array $server [@todo description]
  * @return void
  */
 public function __construct($appInstance, $id, $server)
 {
     $this->onWrite = new StackCallbacks();
     $this->id = $id;
     $this->appInstance = $appInstance;
     $this->server = $server;
     if (isset($this->server['HTTP_COOKIE'])) {
         Generic::parseStr(strtr($this->server['HTTP_COOKIE'], Generic::$hvaltr), $this->cookie);
     }
     if (isset($this->server['QUERY_STRING'])) {
         Generic::parseStr($this->server['QUERY_STRING'], $this->get);
     }
     $this->addr = $server['REMOTE_ADDR'];
     $this->finishTimer = setTimeout(function ($timer) {
         $this->finish();
     }, $this->timeout * 1000000.0);
     $this->appInstance->subscribe('c2s:' . $this->id, [$this, 'c2s']);
     $this->appInstance->subscribe('poll:' . $this->id, [$this, 'poll'], function ($redis) {
         $this->appInstance->publish('state:' . $this->id, 'started', function ($redis) {
             // @TODO: remove this callback
         });
     });
 }
Exemple #12
0
 /**
  * End request
  * @return void
  */
 public function endRequest($req, $appStatus, $protoStatus)
 {
     if ($protoStatus === -1) {
         $this->close();
     } else {
         if ($req->attrs->chunked) {
             $this->write("0\r\n\r\n");
         }
         if (isset($req->keepalive) && $req->keepalive && $this->pool->config->keepalive->value) {
             $this->keepaliveTimer = setTimeout(function ($timer) {
                 $this->finish();
             }, $this->pool->config->keepalive->value);
         } else {
             $this->finish();
         }
     }
     $this->freeRequest($req);
 }
Exemple #13
0
            setTimeout($Redis, $arr['key'], $arr['expire']);
            break;
        case $Redis::REDIS_LIST:
            echo "list\n";
            foreach ($arr['val'] as $v) {
                $Redis->rPush($arr['key'], $v);
            }
            setTimeout($Redis, $arr['key'], $arr['expire']);
            break;
        case $Redis::REDIS_SET:
            echo "set\n";
            foreach ($arr['val'] as $v) {
                $Redis->sAdd($arr['key'], $v);
            }
            setTimeout($Redis, $arr['key'], $arr['expire']);
            break;
        case $Redis::REDIS_ZSET:
            echo "zset\n";
            foreach ($arr['val'] as $v => $score) {
                $Redis->zAdd($arr['key'], $score, $v);
            }
            setTimeout($Redis, $arr['key'], $arr['expire']);
            break;
        default:
            //echo "unknown\n";
            continue;
            break;
    }
}
$Redis->close();
fclose($file);
Exemple #14
0
 public function onReady()
 {
     if ($this->isEnabled()) {
         \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('Chat', array($this, 'onHandshake'));
         $app = $this;
         $this->timer = setTimeout(function ($timer) use($app) {
             foreach ($app->tags as $tag) {
                 $tag->touch();
             }
             $timer->timeout();
         }, 300000.0);
     }
 }
 function testTick()
 {
     $check = 0;
     setTimeout(function () use(&$check) {
         $check++;
     }, 1);
     nextTick(function () use(&$check) {
         $check++;
     });
     tick();
     $this->assertEquals(1, $check);
 }
 /**
  * @TODO DESCR
  * @return void
  */
 public function onHandshake()
 {
     $this->realRoute->client->sendFrameReal('o');
     if (($f = $this->sockjs->config->heartbeatinterval->value) > 0) {
         $this->heartbeatTimer = setTimeout(function ($timer) {
             $this->realRoute->client->sendFrameReal('h');
             $timer->timeout();
         }, $f * 1000000.0);
         $this->realRoute->onHandshake();
     }
 }
Exemple #17
0
 /**
  * Called when we got UDP packet
  * @param resource $stream Descriptor
  * @param integer $events  Events
  * @param mixed $arg       Attached variable
  * @return boolean Success.
  */
 public function onReadUdp($stream = null, $events = 0, $arg = null)
 {
     if (Daemon::$config->logevents->value) {
         Daemon::$process->log(get_class($this) . '::' . __METHOD__ . ' invoked.');
     }
     if (Daemon::$process->reload) {
         return false;
     }
     if ($this->pool->maxConcurrency) {
         if ($this->pool->count() >= $this->pool->maxConcurrency) {
             $this->overload = true;
             return false;
         }
     }
     $host = null;
     do {
         $l = @socket_recvfrom($this->fd, $buf, 10240, MSG_DONTWAIT, $host, $port);
         if (!$l) {
             break;
         }
         $key = '[' . $host . ']:' . $port;
         if (!isset($this->portsMap[$key])) {
             if ($this->pool->allowedClients !== null) {
                 if (!self::netMatch($conn->pool->allowedClients, $host)) {
                     Daemon::log('Connection is not allowed (' . $host . ')');
                 }
                 continue;
             }
             $class = $this->pool->connectionClass;
             $conn = new $class(null, $this->pool);
             $conn->setDgram(true);
             $conn->onWriteEv(null);
             $conn->setPeername($host, $port);
             $conn->setParentSocket($this);
             $this->portsMap[$key] = $conn;
             $conn->timeoutRef = setTimeout(function ($timer) use($conn) {
                 $conn->finish();
                 $timer->finish();
             }, $conn->timeout * 1000000.0);
             $conn->onUdpPacket($buf);
         } else {
             $conn = $this->portsMap[$key];
             $conn->onUdpPacket($buf);
             Timer::setTimeout($conn->timeoutRef);
         }
     } while (true);
     return $host !== null;
 }
Exemple #18
0
<?php

$http = new Event\HTTP();
$srv = $http->createServer(function ($req, $res) {
    $res->write("Hello World immediately\n");
    setTimeout(function () use(&$res) {
        $res->end("Goodbye world 5 seconds later\n");
    }, 5000);
});
$srv->listen(8080, '127.0.0.1');