Переглянути джерело

Merge pull request #491 from joanhey/perf6

More performance changes
walkor 6 роки тому
батько
коміт
d1993208a2

+ 2 - 2
Connection/AsyncTcpConnection.php

@@ -190,7 +190,7 @@ class AsyncTcpConnection extends TcpConnection
         }
         // If failed attempt to emit onError callback.
         if (!$this->_socket || !\is_resource($this->_socket)) {
-            $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
+            $this->emitError(\WORKERMAN_CONNECT_FAIL, $errstr);
             if ($this->_status === self::STATUS_CLOSING) {
                 $this->destroy();
             }
@@ -360,7 +360,7 @@ class AsyncTcpConnection extends TcpConnection
             }
         } else {
             // Connection failed.
-            $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(\microtime(true) - $this->_connectStartTime, 4) . ' seconds');
+            $this->emitError(\WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(\microtime(true) - $this->_connectStartTime, 4) . ' seconds');
             if ($this->_status === self::STATUS_CLOSING) {
                 $this->destroy();
             }

+ 2 - 2
Connection/TcpConnection.php

@@ -385,7 +385,7 @@ class TcpConnection extends ConnectionInterface
                     self::$statistics['send_fail']++;
                     if ($this->onError) {
                         try {
-                            \call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
+                            \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'client closed');
                         } catch (\Exception $e) {
                             Worker::log($e);
                             exit(250);
@@ -899,7 +899,7 @@ class TcpConnection extends ConnectionInterface
         if ($this->maxSendBufferSize <= \strlen($this->_sendBuffer)) {
             if ($this->onError) {
                 try {
-                    \call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    \call_user_func($this->onError, $this, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);

+ 1 - 1
Events/Select.php

@@ -274,7 +274,7 @@ class Select implements EventInterface
 
             // Waiting read/write/signal/timeout events.
             \set_error_handler(function(){});
-            $ret = stream_select($read, $write, $except, 0, $this->_selectTimeout);
+            $ret = \stream_select($read, $write, $except, 0, $this->_selectTimeout);
             \restore_error_handler();
 
 

+ 7 - 7
Lib/Constants.php

@@ -8,8 +8,9 @@
  *
  * @author    walkor<walkor@workerman.net>
  * @copyright walkor<walkor@workerman.net>
- * @link      http://www.workerman.net/
  * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ *
+ * @link      http://www.workerman.net/
  */
 
 // Display errors.
@@ -23,17 +24,16 @@ if (function_exists('opcache_reset')) {
 }
 
 // For onError callback.
-define('WORKERMAN_CONNECT_FAIL', 1);
+const WORKERMAN_CONNECT_FAIL = 1;
 // For onError callback.
-define('WORKERMAN_SEND_FAIL', 2);
+const WORKERMAN_SEND_FAIL = 2;
 
 // Define OS Type
-define('OS_TYPE_LINUX', 'linux');
-define('OS_TYPE_WINDOWS', 'windows');
+const OS_TYPE_LINUX   = 'linux';
+const OS_TYPE_WINDOWS = 'windows';
 
 // Compatible with php7
-if(!class_exists('Error'))
-{
+if ( ! class_exists('Error')) {
     class Error extends Exception
     {
     }

+ 1 - 1
Protocols/Websocket.php

@@ -261,7 +261,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
                 if ($connection->onError) {
                     try {
-                        \call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                        \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);

+ 1 - 1
Protocols/Ws.php

@@ -262,7 +262,7 @@ class Ws
             if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
                 if ($connection->onError) {
                     try {
-                        \call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                        \call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);

+ 16 - 16
Worker.php

@@ -434,7 +434,7 @@ class Worker
      *
      * @var string
      */
-    protected static $_OS = OS_TYPE_LINUX;
+    protected static $_OS = \OS_TYPE_LINUX;
 
     /**
      * Processes for windows.
@@ -553,7 +553,7 @@ class Worker
             exit("Only run in command line mode \n");
         }
         if (\DIRECTORY_SEPARATOR === '\\') {
-            self::$_OS = OS_TYPE_WINDOWS;
+            self::$_OS = \OS_TYPE_WINDOWS;
         }
     }
 
@@ -639,7 +639,7 @@ class Worker
      */
     protected static function initWorkers()
     {
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         foreach (static::$_workers as $worker) {
@@ -744,7 +744,7 @@ class Worker
         if (\in_array('-q', $argv)) {
             return;
         }
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
             static::safeEcho('Workerman version:'. static::VERSION. '          PHP version:'. \PHP_VERSION. "\r\n");
             static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
@@ -841,7 +841,7 @@ class Worker
      */
     protected static function parseCommand()
     {
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         global $argv;
@@ -1079,7 +1079,7 @@ class Worker
      */
     protected static function installSignal()
     {
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         $signalHandler = '\Workerman\Worker::signalHandler';
@@ -1106,7 +1106,7 @@ class Worker
      */
     protected static function reinstallSignal()
     {
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         $signalHandler = '\Workerman\Worker::signalHandler';
@@ -1183,7 +1183,7 @@ class Worker
      */
     protected static function daemonize()
     {
-        if (!static::$daemonize || static::$_OS !== OS_TYPE_LINUX) {
+        if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         \umask(0);
@@ -1212,7 +1212,7 @@ class Worker
      */
     public static function resetStd()
     {
-        if (!static::$daemonize || static::$_OS !== OS_TYPE_LINUX) {
+        if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
         global $STDOUT, $STDERR;
@@ -1243,7 +1243,7 @@ class Worker
      */
     protected static function saveMasterPid()
     {
-        if (static::$_OS !== OS_TYPE_LINUX) {
+        if (static::$_OS !== \OS_TYPE_LINUX) {
             return;
         }
 
@@ -1321,7 +1321,7 @@ class Worker
      */
     protected static function forkWorkers()
     {
-        if (static::$_OS === OS_TYPE_LINUX) {
+        if (static::$_OS === \OS_TYPE_LINUX) {
             static::forkWorkersForLinux();
         } else {
             static::forkWorkersForWindows();
@@ -1600,7 +1600,7 @@ class Worker
      */
     protected static function monitorWorkers()
     {
-        if (static::$_OS === OS_TYPE_LINUX) {
+        if (static::$_OS === \OS_TYPE_LINUX) {
             static::monitorWorkersForLinux();
         } else {
             static::monitorWorkersForWindows();
@@ -2041,7 +2041,7 @@ class Worker
     public static function checkErrors()
     {
         if (static::STATUS_SHUTDOWN !== static::$_status) {
-            $error_msg = static::$_OS === OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
+            $error_msg = static::$_OS === \OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
             $errors    = error_get_last();
             if ($errors && ($errors['type'] === \E_ERROR ||
                     $errors['type'] === \E_PARSE ||
@@ -2083,7 +2083,7 @@ class Worker
             static::safeEcho($msg);
         }
         \file_put_contents((string)static::$logFile, \date('Y-m-d H:i:s') . ' ' . 'pid:'
-            . (static::$_OS === OS_TYPE_LINUX ? \posix_getpid() : 1) . ' ' . $msg, \FILE_APPEND | \LOCK_EX);
+            . (static::$_OS === \OS_TYPE_LINUX ? \posix_getpid() : 1) . ' ' . $msg, \FILE_APPEND | \LOCK_EX);
     }
 
     /**
@@ -2134,7 +2134,7 @@ class Worker
             static::$_outputDecorated = false;
         } else {
             static::$_outputDecorated =
-                static::$_OS === OS_TYPE_LINUX &&
+                static::$_OS === \OS_TYPE_LINUX &&
                 \function_exists('posix_isatty') &&
                 \posix_isatty($stream);
         }
@@ -2160,7 +2160,7 @@ class Worker
         Autoloader::setRootPath($this->_autoloadRootPath);
 
         // Turn reusePort on.
-        if (static::$_OS === OS_TYPE_LINUX  // if linux
+        if (static::$_OS === \OS_TYPE_LINUX  // if linux
                             && \version_compare(\PHP_VERSION,'7.0.0', 'ge') // if php >= 7.0.0
                             && \strtolower(\php_uname('s')) !== 'darwin') { // if not Mac OS