/** * 并发请求api,使用方法如 * $params = array( * "api_1117"=>array("name"=>"apiname1",“param”=>array("id"=>1117)), * "api_2"=>array("name"=>"apiname2","param"=>array("id"=>2)), * ) * @param array $params 提交参数 请指定key好方便区分对应结果,注意考虑到硬件资源有限并发请求不要超过50个 * @param int $mode * @param int $retry 通讯错误时重试次数 * @param string $ip 要连得ip地址,如果不指定从现有配置随机个 * @param string $port 要连得port地址,如果不指定从现有配置找一个 * @return mixed 返回指定key结果 * @throws \Exception unknow mode type */ public function multiAPI($params, $mode = DoraConst::SW_MODE_WAITRESULT, $retry = 0, $ip = "", $port = "") { //get guid $this->guid = $this->generateGuid(); $packet = array('guid' => $this->guid, 'api' => $params); switch ($mode) { case DoraConst::SW_MODE_WAITRESULT: $packet["type"] = DoraConst::SW_MODE_WAITRESULT_MULTI; break; case DoraConst::SW_MODE_NORESULT: $packet["type"] = DoraConst::SW_MODE_NORESULT_MULTI; break; case DoraConst::SW_MODE_ASYNCRESULT: $packet["type"] = DoraConst::SW_MODE_ASYNCRESULT_MULTI; break; default: throw new \Exception("unknow mode have been set", 100099); break; } $sendData = Packet::packEncode($packet); $result = $this->doRequest($sendData, $packet["type"]); //retry when the send fail while ((!isset($result["code"]) || $result["code"] != 0) && $retry > 0) { $result = $this->doRequest($sendData, $packet["type"]); $retry--; } if ($this->guid != $result["guid"]) { return Packet::packFormat("guid wront please retry..", 100100, $result["data"]); } return $result; }
/** * 并发请求api,使用方法如 * $params = array( * "api_1117"=>array("name"=>"apiname1",“param”=>array("id"=>1117)), * "api_2"=>array("name"=>"apiname2","param"=>array("id"=>2)), * ) * @param array $params 提交参数 请指定key好方便区分对应结果,注意考虑到硬件资源有限并发请求不要超过50个 * @param bool $sync 阻塞等待所有结果 * @param int $retry 通讯错误时重试次数 * @param string $group 服务分组 * @param string $ip 要连得ip地址,如果不指定从现有配置随机个 * @param string $port 要连得port地址,如果不指定从现有配置找一个 * @return mixed 返回指定key结果 */ public function multiAPI($params, $group = "group1", $sync = true, $retry = 0, $ip = "", $port = "") { $guid = md5(uniqid() . microtime(true) . rand(1, 1000000)); $Packet = array('guid' => $guid, 'api' => $params); if ($sync) { $Packet["type"] = DoraConst::SW_SYNC_MULTI; } else { $Packet["type"] = DoraConst::SW_ASYNC_MULTI; } $sendData = Packet::packEncode($Packet); $result = $this->doRequest($sendData, $group, $ip, $port); //retry when the send fail while ((!isset($result["code"]) || $result["code"] != 0) && $retry > 0) { $result = $this->doRequest($sendData, $group, $ip, $port); $retry--; } if ($result["code"] == "0" && $guid != $result["data"]["guid"]) { return Packet::packFormat("guid wrong please retry..", 100008, $result); } return $result; }
public final function onHttpFinished($serv, $task_id, $data, $response) { $fd = $data["fd"]; $guid = $data["guid"]; //if the guid not exists .it's mean the api no need return result if (!isset($this->taskInfo[$fd][$guid])) { return true; } //get the api key $key = $this->taskInfo[$fd][$guid]["taskkey"][$task_id]; //save the result $this->taskInfo[$fd][$guid]["result"][$key] = $data["result"]; //remove the used taskid unset($this->taskInfo[$fd][$guid]["taskkey"][$task_id]); switch ($data["type"]) { case DoraConst::SW_MODE_WAITRESULT_MULTI: //all task finished if (count($this->taskInfo[$fd][$guid]["taskkey"]) == 0) { $packet = Packet::packFormat("OK", 0, $this->taskInfo[$fd][$guid]["result"]); $packet["guid"] = $guid; $packet = Packet::packEncode($packet, $data["protocol"]); unset($this->taskInfo[$fd][$guid]); $response->end($packet); return true; } else { //multi call task //not finished //waiting other result return true; } break; default: return true; break; } }
public final function onFinish($serv, $task_id, $data) { //fixed the result more than 8k timeout bug if (strpos($data, '$$$$$$$$') === 0) { $tmp_path = substr($data, 8); $data = file_get_contents($tmp_path); unlink($tmp_path); } $data = unserialize($data); $fd = $data["fd"]; if (!isset($this->taskInfo[$fd]) || !$data["result"]) { unset($this->taskInfo[$fd]); return true; } $key = $this->taskInfo[$fd]["task"][$task_id]; $this->taskInfo[$fd]["result"][$key] = $data["result"]; unset($this->taskInfo[$fd]["task"][$task_id]); switch ($data["type"]) { case DoraConst::SW_SYNC_SINGLE: $Packet = Packet::packFormat("OK", 0, $data["result"]); $Packet["guid"] = $this->taskInfo[$fd]["guid"]; $Packet = Packet::packEncode($Packet); //sys_get_temp_dir $serv->send($fd, $Packet); unset($this->taskInfo[$fd]); return true; break; case DoraConst::SW_SYNC_MULTI: if (count($this->taskInfo[$fd]["task"]) == 0) { $Packet = Packet::packFormat("OK", 0, $this->taskInfo[$fd]["result"]); $Packet["guid"] = $this->taskInfo[$fd]["guid"]; $Packet = Packet::packEncode($Packet); $serv->send($fd, $Packet); unset($this->taskInfo[$fd]); return true; } else { return true; } break; default: unset($this->taskInfo[$fd]); return true; break; } }