stanley-king 8 lat temu
rodzic
commit
db1ddcf88d
4 zmienionych plików z 61 dodań i 49 usunięć
  1. 20 27
      centra_srv.php
  2. 37 17
      helper/search/server.php
  3. 1 2
      mac_start.sh
  4. 3 3
      test/TestSearch.php

+ 20 - 27
centra_srv.php

@@ -32,7 +32,6 @@ function search_work($sockfd)
     $gMessageStates = new MsgStates();
     $gMessageLock = Mutex::create();
 
-    echo "start searcher";
     Base::run_util();
 
     StatesHelper::init();
@@ -42,48 +41,43 @@ function search_work($sockfd)
     if(StatesHelper::fetch_state('init')) {
         Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
         search\searcher::instance()->init();
-        \search\area_library::instance();
+        search\area_library::instance();
     }
 
     search\CentraHelper::instance()->init(new search\processor());
-    echo "start ending";
-
     search\CentraHelper::instance()->run_loop($sockfd);
-    echo "end run_loop";
 }
 
 function fork_subprocess($count,$listen_fd)
 {
-    if (($child = pcntl_fork()) === 0)
+    if (($pid = pcntl_fork()) === 0)
     {
-//        ob_end_clean(); // Discard the output buffer and close
-//        fclose(STDIN);  // Close all of the standard
-//        fclose(STDOUT); // file descriptors as we
-//        fclose(STDERR); // are running as a daemon.
+        //ob_end_clean(); // Discard the output buffer and close
+        fclose(STDIN);  // Close all of the standard
+        fclose(STDOUT); // file descriptors as we
+        fclose(STDERR); // are running as a daemon.
 
-        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
         search_work($listen_fd);
         exit();
     }
-    elseif($child === -1)
+    elseif($pid === -1)
     {
-        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
         die('could not fork');
     }
     else
     {
-
-
-        echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
-        $ret = pcntl_waitpid($child,$status,WNOHANG);
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
+        $ret = pcntl_waitpid($pid,$status,WNOHANG);
         if($ret == 0) {
-            echo "spawn-fcgi: successful ret == 0 PID: {$child}\n" ;
+            Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
         }
         elseif($ret == -1) {
-            echo "spawn-fcgi: ret == 0  PID: {$child}\n" ;
+            Log::record("spawn-fcgi: ret == 0  PID: {$pid}",Log::DEBUG);
         }
         else {
-            echo "spawn-fcgi: ret == 0 child exited PID: {$child}.\n";
+            Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
         }
     }
 }
@@ -99,16 +93,15 @@ function remote_addr()
 }
 
 $listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
-if($listen_fd != false)
-{
-    $count = 1;
-    while ($count-- > 0) {
-        fork_subprocess($count,$listen_fd);
-    }
-} else {
+if($listen_fd == false) {
     echo "无法创建socket,请退出之前进程.\n";
 }
 
+$count = 5;
+while ($count-- > 0) {
+    fork_subprocess($count,$listen_fd);
+}
+
 
 
 

+ 37 - 17
helper/search/server.php

@@ -53,46 +53,54 @@ class CentraHelper
 
     public function run_loop($sockfd)
     {
+        //$this->mListenSocket = stream_socket_server ($this->remote_addr(), $errno, $errstr);
         $this->mListenSocket = $sockfd;
-        echo "stream_set_blocking \n";
+        Log::record("stream_set_blocking",Log::DEBUG);
         if(stream_set_blocking($this->mListenSocket, 0) == false) {
-            echo "stream_set_blocking error \n";
+            Log::record("stream_set_blocking error",Log::DEBUG);
         }
         $this->mEvbase = event_base_new();
         $this->mEv = event_new();
 
-        echo "event_set\n";
+        Log::record("event_set",Log::DEBUG);
         if(event_set($this->mEv, $this->mListenSocket, EV_READ | EV_PERSIST, 'ev_accept', $this->mEvbase) == false) {
-            echo "event_set error EV_READ | EV_PERSIST\n";
+            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
         }
 
-        echo "event_base_set\n";
+        Log::record("event_base_set",Log::DEBUG);
         if(event_base_set($this->mEv, $this->mEvbase) == false) {
-            echo "event_set error EV_READ | EV_PERSIST\n";
+            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
         }
 
-        echo "event_add\n";
+        Log::record("event_add",Log::DEBUG);
         if(event_add($this->mEv) == false) {
-            echo "event_add error EV_READ | EV_PERSIST\n";
+            Log::record("event_add error EV_READ | EV_PERSIST",Log::DEBUG);
         }
 
         $ret = event_base_loop($this->mEvbase);
-        echo "event_base_loop ret={$ret}\n";
+        Log::record("event_base_loop ret={$ret}",Log::DEBUG);
     }
 
     public function ev_accept($socket, $flag, $base)
     {
-        Log::record("ev_accept socket_fd={$socket}",Log::DEBUG);
+        $pid = posix_getpid();
+        Log::record("ev_accept pid={$pid} socket_fd={$socket}",Log::DEBUG);
 
         static $bufid = 1;
 
         $stream = stream_socket_accept($socket);
-        if($stream == false) return;
+        if($stream == false) {
+            Log::record("stream_socket_accept return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
+            return;
+        }
+        Log::record("stream_socket_accept pid={$pid} ",Log::DEBUG);
 
         stream_set_blocking($stream, 0);
         $buffer = event_buffer_new($stream, 'ev_read', NULL, 'ev_error', $bufid);
         if($buffer == false) {
             fclose($stream);
+            Log::record("event_buffer_new return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
+
             return;
         }
 
@@ -112,6 +120,9 @@ class CentraHelper
 
     public function ev_read($buffer, $bufid)
     {
+        $pid = posix_getpid();
+        Log::record("ev_read pid={$pid}",Log::DEBUG);
+
         $content = &$this->mContents[$bufid];
         while ($read = event_buffer_read($buffer, 256)) {
             $content .= $read;
@@ -129,11 +140,18 @@ class CentraHelper
                 if($left >= self::body_header_len + $len)
                 {
                     $body = substr($content,$start + self::body_header_len,$len);
-                    if($this->mProcessor != null) {
+                    if($this->mProcessor != null)
+                    {
                         $data = $this->mProcessor->handle_input($body);
                         $header = sprintf("%010d",strlen($data));
                         $data = $header . $data;
-                        event_buffer_write($buffer,$data,strlen($data));
+                        $ret = event_buffer_write($buffer,$data,strlen($data));
+                        if($ret == false) {
+                            Log::record("event_buffer_write error pid={$pid}",Log::DEBUG);
+                            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
+                            fclose($this->mStreams[$bufid]);
+                            unset($this->mStreams[$bufid]);
+                        }
                     }
                     $start += self::body_header_len + $len;
                     $left = $left - self::body_header_len - $len;
@@ -150,7 +168,6 @@ class CentraHelper
         if($start > 0) {
             $content = substr($content,$start);
         }
-        ob_clean();
     }
 
     public function ev_error($buffer, $error, $bufid)
@@ -160,10 +177,13 @@ class CentraHelper
 
         event_buffer_disable($buffer, EV_READ | EV_WRITE);
         event_buffer_free($buffer);
-        stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
-        fclose($this->mStreams[$bufid]);
 
-        unset($this->mStreams[$bufid]);
+        if(array_key_exists($bufid,$this->mStreams)) {
+            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
+            fclose($this->mStreams[$bufid]);
+            unset($this->mStreams[$bufid]);
+        }
+
         unset($this->mBuffers[$bufid]);
         unset($this->mContents[$bufid]);
     }

+ 1 - 2
mac_start.sh

@@ -3,5 +3,4 @@ sudo /usr/local/mysql/support-files/mysql.server restart
 sudo nginx
 sudo php-fpm
 redis-server /etc/redis/6379.conf
-redis-server /etc/redis/6380.conf
-php ./centra_srv.php
+redis-server /etc/redis/6380.conf

+ 3 - 3
test/TestSearch.php

@@ -53,9 +53,9 @@ class TestSearch extends PHPUnit_Framework_TestCase
 
     public function testRequest()
     {
-        for($i = 0; $i < 10000; ++$i) {
-            $result = search\tcp_client::instance()->get_result('眼影');
-            Log::record("testRequest {$i}",Log::DEBUG);
+        for($i = 0; $i < 10; ++$i) {
+            $result = search\tcp_client::instance()->get_result(array('keyword' => '相宜本草'));
+            //Log::record("testRequest {$i}",Log::DEBUG);
         }
     }