Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pecl-event 1.5.0 compatibility

  • Loading branch information...
commit 111b79c54783603bef78ae44e6828d0067c01e3b 1 parent 95146a5
@kakserpom authored
View
2  VERSION
@@ -1 +1 @@
-0.9.0
+0.9.2
View
10 lib/BoundSocket.php
@@ -43,7 +43,7 @@
* Listener mode?
* @var boolean
*/
- protected $listenerMode = false;
+ protected $listenerMode = true;
/**
* Context
@@ -227,6 +227,12 @@ public function setFd($fd) {
$this->fd = $fd;
$this->pid = posix_getpid();
}
+
+ /**
+ * Called when socket is bound
+ * @return boolean Success
+ */
+ protected function onBound() {}
/**
* Enable socket events
@@ -242,6 +248,7 @@ public function enable() {
$this->enabled = true;
if ($this->listenerMode) {
if ($this->ev === null) {
+ Daemon::$process->log('new EventListener('.$this->fd.')');
$this->ev = new EventListener(
Daemon::$process->eventBase,
[$this, 'onListenerAcceptEv'],
@@ -253,6 +260,7 @@ public function enable() {
if ($this->ev and is_callable([$this->ev, 'getSocketName'])) {
$this->ev->getSocketName($this->locHost, $this->locPort);
}
+ $this->onBound();
} else {
$this->ev->enable();
}
View
6 lib/BoundTCPSocket.php
@@ -21,12 +21,6 @@ class BoundTCPSocket extends BoundSocket {
protected $port;
/**
- * Listener mode?
- * @var boolean
- */
- protected $listenerMode = true;
-
- /**
* Default port
* @var integer
*/
View
78 lib/BoundUNIXSocket.php
@@ -33,6 +33,48 @@ class BoundUNIXSocket extends BoundSocket {
public function __toString() {
return $this->path;
}
+
+ /**
+ * Listener mode?
+ * @var boolean
+ */
+ protected $listenerMode = false;
+
+ /**
+ * Called when socket is bound
+ * @return boolean Success
+ */
+ protected function onBound() {
+ touch($this->path);
+ chmod($this->path, 0770);
+ if ($this->group === null && !empty($this->uri['pass'])) {
+ $this->group = $this->uri['pass'];
+ }
+ if ($this->group === null && isset(Daemon::$config->group->value)) {
+ $this->group = Daemon::$config->group->value;
+ }
+ if ($this->group !== null) {
+ if (!@chgrp($this->path, $this->group)) {
+ unlink($this->path);
+ Daemon::log('Couldn\'t change group of the socket \'' . $this->path . '\' to \'' . $this->group . '\'.');
+ return false;
+ }
+ }
+ if ($this->user === null && !empty($this->uri['user'])) {
+ $this->user = $this->uri['user'];
+ }
+ if ($this->user === null && isset(Daemon::$config->user->value)) {
+ $this->user = Daemon::$config->user->value;
+ }
+ if ($this->user !== null) {
+ if (!@chown($this->path, $this->user)) {
+ unlink($this->path);
+ Daemon::log('Couldn\'t change owner of the socket \'' . $this->path . '\' to \'' . $this->user . '\'.');
+ return false;
+ }
+ }
+ }
+
/**
* Bind socket
* @return boolean Success.
@@ -50,11 +92,15 @@ public function bindSocket() {
Daemon::$process->log('Unix-socket \'' . $this->path . '\' must has \'.sock\' extension.');
return;
}
-
+
if (file_exists($this->path)) {
unlink($this->path);
}
+ if ($this->listenerMode) {
+ $this->setFd('unix:' . $this->path);
+ return true;
+ }
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if (!$sock) {
$errno = socket_last_error();
@@ -71,38 +117,12 @@ public function bindSocket() {
Daemon::$process->log(get_class($this) . ': Couldn\'t bind Unix-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
return;
}
+ socket_set_nonblock($sock);
+ $this->onBound();
if (!socket_listen($sock, SOMAXCONN)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this) . ': Couldn\'t listen UNIX-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
}
- socket_set_nonblock($sock);
- chmod($this->uri['path'], 0770);
- if ($this->group === null && !empty($this->uri['pass'])) {
- $this->group = $this->uri['pass'];
- }
- if ($this->group === null && isset(Daemon::$config->group->value)) {
- $this->group = Daemon::$config->group->value;
- }
- if ($this->group !== null) {
- if (!@chgrp($this->path, $this->group)) {
- unlink($this->path);
- Daemon::log('Couldn\'t change group of the socket \'' . $this->path . '\' to \'' . $this->group . '\'.');
- return false;
- }
- }
- if ($this->user === null && !empty($this->uri['user'])) {
- $this->user = $this->uri['user'];
- }
- if ($this->user === null && isset(Daemon::$config->user->value)) {
- $this->user = Daemon::$config->user->value;
- }
- if ($this->user !== null) {
- if (!@chown($this->uri['path'], $this->user)) {
- unlink($this->uri['path']);
- Daemon::log('Couldn\'t change owner of the socket \'' . $this->path . '\' to \'' . $this->user . '\'.');
- return false;
- }
- }
$this->setFd($sock);
return true;
}
View
24 lib/Connection.php
@@ -474,15 +474,22 @@ public function connect($url, $cb = null) {
*/
public function connectUnix($path) {
$this->type = 'unix';
- $fd = socket_create(AF_UNIX, SOCK_STREAM, 0);
- if (!$fd) {
- return false;
+
+ if (!$this->bevConnectEnabled) {
+ $fd = socket_create(AF_UNIX, SOCK_STREAM, 0);
+ if (!$fd) {
+ return false;
+ }
+ socket_set_nonblock($fd);
+ socket_set_option($fd, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
+ socket_set_option($fd, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
+ @socket_connect($fd, $path, 0);
+ $this->setFd($fd);
+ return true;
}
- socket_set_nonblock($fd);
- socket_set_option($fd, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
- socket_set_option($fd, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
- @socket_connect($fd, $path, 0);
- $this->setFd($fd);
+ $this->bevConnect = true;
+ $this->addr = 'unix:' . $path;
+ $this->setFd(null);
return true;
}
@@ -516,6 +523,7 @@ public function connectRaw($host) {
if ($this->host === null) {
$this->host = $this->hostReal;
}
+ $this->addr = $this->hostReal . ':raw';
$fd = socket_create(AF_INET, SOCK_RAW, 1);
if (!$fd) {
return false;
View
2  lib/Daemon_Bootstrap.php
@@ -164,7 +164,7 @@ public static function init($configFile = null) {
$error = true;
}
- $eventVer = '1.4.0';
+ $eventVer = '1.5.0';
if (!Daemon::loadModuleIfAbsent('event', $eventVer .'-beta')) {
Daemon::log('[EMERG] event extension >= ' . $eventVer . ' not found (or OUTDATED). You have to install it. `pecl install http://pecl.php.net/get/event-' . $eventVer . '.tgz`');
$error = true;
View
14 lib/Daemon_WorkerThread.php
@@ -183,7 +183,7 @@ class_exists('Timer');
Timer::add(function($event) {
- if (!Daemon::$runworkerMode) {
+ if (!Daemon::$runworkerMode || 1) {
$this->IPCManager->ensureConnection();
}
@@ -694,8 +694,16 @@ protected function sigusr2() {
$this->log('caught SIGUSR2 (graceful shutdown for update).');
}
- $this->reload = TRUE;
- $this->reloadTime = microtime(TRUE) + $this->reloadDelay;
+ $this->gracefulRestart();
+ }
+
+ /**
+ * Graceful restart
+ * @return void
+ */
+ public function gracefulRestart() {
+ $this->reload = true;
+ $this->reloadTime = microtime(true) + $this->reloadDelay;
$this->setState($this->state);
}
View
10 lib/IOStream.php
@@ -292,8 +292,12 @@ public function setFd($fd, $bev = null) {
$this->bev->setTimeouts($this->timeout, $this->timeout);
}
if ($this->bevConnect && ($this->fd === null)) {
- $this->bev->connect($this->addr, false);
- //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port, EventUtil::AF_UNSPEC);
+ //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port);
+ $this->bev->connect($this->addr);
+ }
+ if (!$this->bev) {
+ $this->finish();
+ return false;
}
if (!$this->bev->enable(Event::READ | Event::WRITE | Event::TIMEOUT | Event::PERSIST)) {
$this->finish();
@@ -669,7 +673,7 @@ public function unsetFd() {
}
protected function log($m) {
- Daemon::log(get_class().': '.$m);
+ Daemon::log(get_class($this).': '.$m);
}
/**
View
9 lib/IPCManager.php
@@ -159,6 +159,7 @@ protected function onPacket($p) {
if (!is_array($p)) {
return;
}
+ //Daemon::log(Debug::dump($p));;
if ($p['op'] === 'start') {
$this->workerId = $p['workerId'];
$this->pool->workers[$this->workerId] = $this;
@@ -240,11 +241,11 @@ class IPCManagerWorkerConnection extends Connection {
const STATE_CONTENT = 1;
protected $packetLength;
public function onReady() {
- $this->sendPacket(array(
+ $this->sendPacket([
'op' => 'start',
'pid' => Daemon::$process->getPid(),
- 'workerId' => Daemon::$process->getId())
- );
+ 'workerId' => Daemon::$process->getId()
+ ]);
parent::onReady();
}
protected function onPacket($p) {
@@ -259,7 +260,7 @@ protected function onPacket($p) {
}
elseif ($p['op'] === 'importFile') {
if (!Daemon::$config->autoreimport->value) {
- Daemon::$process->sigusr2(); // graceful restart
+ Daemon::$process->gracefulRestart();
return;
}
$path = $p['path'];
Please sign in to comment.
Something went wrong with that request. Please try again.