コード例 #1
0
ファイル: Worker.php プロジェクト: hioop/statistics
 /**
  * 接收数据
  * @param \swoole_server $serv
  * @param unknown $fd
  * @param unknown $from_id
  * @param unknown $data
  */
 public function onReceive(\swoole_server $serv, $fd, $from_id, $data)
 {
     $data = self::decode($data);
     $connInfo = $serv->connection_info($fd, $from_id);
     if ($connInfo['server_port'] == $this->handleWorkerPort) {
         $module = $data['module'];
         $interface = $data['interface'];
         $cost_time = $data['cost_time'];
         $success = $data['success'];
         $time = $data['time'];
         $code = $data['code'];
         $msg = str_replace("\n", "<br>", $data['msg']);
         $ip = $serv->connection_info($fd)['remote_ip'];
         // 模块接口统计
         $this->collectStatistics($module, $interface, $cost_time, $success, $ip, $code, $msg);
         // 全局统计
         $this->collectStatistics('AllData', 'Statistics', $cost_time, $success, $ip, $code, $msg);
         // 失败记录日志
         if (!$success) {
             $redis = $this->getRedis();
             $logBuffer = $redis->get($this->logBufferKey);
             $logBuffer .= date('Y-m-d H:i:s', $time) . "\t{$ip}\t{$module}::{$interface}\tcode:{$code}\tmsg:{$msg}\n";
             $redis->set($this->logBufferKey, $logBuffer);
             if (strlen($logBuffer) >= $this->max_log_buffer_size) {
                 $this->writeLogToDisk();
             }
         }
     } else {
         if ($connInfo['server_port'] == $this->handleProviderPort) {
             $provider = \Bootstrap\Provider::getInstance();
             $provider->message($serv, $fd, $from_id, $data);
         } else {
             if ($connInfo['server_port'] == $this->udpFinderport) {
                 if (empty($data)) {
                     return false;
                 }
                 // 无法解析的包
                 if (empty($data['cmd']) || $data['cmd'] != 'REPORT_IP') {
                     return false;
                 }
                 return $serv->send($fd, json_encode(array('result' => 'ok')));
             } else {
                 echo '端口错误' . PHP_EOL;
             }
         }
     }
 }
コード例 #2
0
ファイル: Worker.php プロジェクト: smalleyes/statistics
 /**
  * task任务
  * @param \swoole_server $serv
  * @param unknown $task_id
  * @param unknown $from_id
  * @param unknown $data
  * @return void|multitype:string
  */
 public function onTask(\swoole_server $serv, $task_id, $from_id, $data)
 {
     list($fd, $req) = $data;
     $provider = \Bootstrap\Provider::getInstance();
     $provider->message($serv, $fd, $from_id, $req);
 }