Ejemplo n.º 1
0
 private function getHeaders()
 {
     $headers = RemoteHeaderCreator::getHeaders();
     $headerArr = array('Meilishuo:' . $headers['Meilishuo']);
     if (!empty($headers['X-REF'])) {
         $headerArr[] = 'XREF:' . $headers['X-REF'];
     }
     //transfer the logid http header between rpc
     if ($logid = \Libs\log\LevelLogWriter::logId()) {
         $headerArr[] = 'LOGID:' . $logid;
     }
     return $headerArr;
 }
Ejemplo n.º 2
0
 protected function do_publish_message($topic, $message, $partition_key = 0, $current_retry_time = 0)
 {
     $stime = microtime(true);
     $rtn = $offset = $partition = false;
     $post_body = http_build_query($message);
     $headers = array();
     $headers[] = 'X-Kmq-Topic: ' . $topic;
     $headers[] = 'X-Kmq-Partition-Key: ' . $partition_key;
     $headers[] = 'X-Kmq-Logid: ' . \Libs\Log\LevelLogWriter::logId();
     $endpoint = $this->getEndpoint();
     $ch = curl_init($endpoint);
     curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
     if (defined('CURLOPT_TIMEOUT_MS')) {
         curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout_ms);
     } else {
         // hack 没有毫秒超时的情况,如果当前超时转为秒大于1,则用该时间,否则用1,不按此处理会导致超时设置无效
         curl_setopt($ch, CURLOPT_TIMEOUT, max(1, intval($this->timeout_ms / 1000)));
     }
     if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connect_timeout_ms);
     } else {
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, max(1, intval($this->connect_timeout_ms / 1000)));
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
     $proxy_result = curl_exec($ch);
     $this->rpclog($ch);
     if (!$proxy_result) {
         $rtn = false;
         $catched_exception = "request error : " . curl_error($ch);
     } else {
         $res = json_decode($proxy_result, true);
         if (!is_array($res)) {
             $rtn = false;
             $catched_exception = "json_decode failed : #{$proxy_result}#";
         } else {
             if ($res['errno'] != 0) {
                 $rtn = false;
                 $catched_exception = "proxy processed failed , errno:" . $res['errno'] . " , errmsg: " . $res['errmsg'];
             } else {
                 $rtn = $offset = $res['data']['Offset'];
                 $partition = $res['data']['Partition'];
             }
         }
     }
     $logData = array();
     $logData['endpoint'] = $endpoint;
     $logData['topic'] = $topic;
     $logData['partition_key'] = $partition_key;
     $logData['message'] = json_encode($message);
     $logData['partition'] = $partition;
     $logData['offset'] = $offset;
     $logData['exception'] = isset($catched_exception) ? json_encode($catched_exception) : '';
     $logData['timecost'] = number_format((microtime(true) - $stime) * 1000, 2);
     $logData['current_retry_time'] = $current_retry_time;
     if (!$logData['exception']) {
         $logData['loglevel'] = 'INFO';
     } else {
         if ($logData['current_retry_time'] >= $this->retry_times - 1) {
             $logData['loglevel'] = 'FATAL';
         } else {
             $logData['loglevel'] = 'WARNING';
         }
     }
     if ($logData['logLevel'] == 'FATAL' || $logData['loglevel'] == 'WARNING') {
         \Libs\Log\LevelLogWriter::selfLog("mqproxy", "publish_message", $logData);
     }
     $retry_curl_errno = array(CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_CONNECT);
     if (in_array(curl_errno($ch), $retry_curl_errno)) {
         throw new \Frame\Exception\RetryException(curl_error($ch));
     }
     return $rtn;
 }