开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > WEB应用开发 > RPC开发框架 &&
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
22 Star 46 Fork 13

thinkpc/Dora-RPC

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (5)
标签 (6)
master
platform-version
dora-console
hotfix-server-discovery
pr/1
0.4.15_beta
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (5)
标签 (6)
master
platform-version
dora-console
hotfix-server-discovery
pr/1
0.4.15_beta
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (5)
标签 (6)
master
platform-version
dora-console
hotfix-server-discovery
pr/1
0.4.15_beta
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
Dora-RPC
/
src
/
Client.php
Dora-RPC
/
src
/
Client.php
Client.php 19.95 KB
一键复制 编辑 原始数据 按行查看 历史
徐长龙 提交于 2017年01月15日 16:14 +08:00 . chang the guid outside
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
<?php
namespace DoraRPC;
/**
* Class Client
* https://github.com/xcl3721/Dora-RPC
* by 蓝天 http://weibo.com/thinkpc
*/
class Client
{
//client obj pool
private static $client = array();
//for the async task list
private static $asynclist = array();
//for the async task result
private static $asynresult = array();
//client config array
private $serverConfig = array();
//when connect fail will block error config
private $serverConfigBlock = array();
//this request guid
private $guid;
//1 random from specify group,2 specify by ip port
//1 随机从指定group名称内选择客户端,2 指定ip进行连接
private $connectMode = 0;
//是否使用上一次已连接connectclient
//用于减少单机长链接数
//private $connectReuse = true;
//current connect ip port
private $connectIp = "";
private $connectPort = 0;
//config group name
private $connectGroup = "";
//current using client obj key on static client array
private $currentClientKey = "";
public function __construct($serverConfig)
{
if (count($serverConfig) == 0) {
echo "cant found config on the Dora RPC..";
throw new \Exception("please set the config param on init Dora RPC", -1);
}
$this->serverConfig = $serverConfig;
}
//$param = array("type"=>1,"group"=>"group1");
//$param = array("type"=>2,"ip"=>"127.0.0.1","port"=>9567);
/**
* 更换连接模式,用于指定ip请求和普通请求切换
* @param $param
* @throws \Exception unknow mode parameter
*/
public function changeMode($param)
{
switch ($param["type"]) {
case 1:
if ($param["group"] == "") {
throw new \Exception("change mode parameter is wrong", -1);
}
$this->connectMode = 1;
$this->connectGroup = $param["group"];
$this->connectIp = "";
$this->connectPort = "";
$this->currentClientKey = "";
break;
case 2:
if ($param["ip"] == "" || $param["port"] == "") {
throw new \Exception("change mode parameter is wrong", -1);
}
$this->connectMode = 2;
$this->connectGroup = "default";
$this->connectIp = $param["ip"];
$this->connectPort = $param["port"];
$this->currentClientKey = "";
break;
default:
throw new \Exception("change mode parameter is wrong", -1);
break;
}
}
/**
* 返回当前连接模式及相关信息
* @return array
* @throws \Exception unknow mode
*/
public function getConnectMode()
{
switch ($this->connectMode) {
case 1:
return array("type" => 1, "group" => $this->connectGroup, "ip" => $this->connectIp, "port" => $this->connectPort);
break;
case 2:
return array("type" => 2, "group" => "", "ip" => $this->connectIp, "port" => $this->connectPort);
break;
default:
throw new \Exception("current connect mode is unknow", -1);
break;
}
}
//random get config key
private function getConfigObjKey()
{
if (!isset($this->serverConfig[$this->connectGroup])) {
throw new \Exception("there is no one server can connect", 100010);
}
// if there is no config can use clean up the block list
if (isset($this->serverConfigBlock[$this->connectGroup]) &&
count($this->serverConfig[$this->connectGroup]) <= count($this->serverConfigBlock[$this->connectGroup])
) {
//clean up the block list
$this->serverConfigBlock[$this->connectGroup] = array();
}
//if not specified the ip and port random get one
do {
//get one config by random
$key = array_rand($this->serverConfig[$this->connectGroup]);
//if not on the block list.
if (!isset($this->serverConfigBlock[$this->connectGroup][$key])) {
return $key;
}
} while (count($this->serverConfig[$this->connectGroup]) > count($this->serverConfigBlock));
throw new \Exception("there is no one server can connect", 100010);
}
//get current client
private function getClientObj()
{
//config obj key
$key = "";
//if not spc will random
switch ($this->connectMode) {
case 1:
$key = $this->getConfigObjKey();
$clientKey = $this->serverConfig[$this->connectGroup][$key]["ip"] . "_" . $this->serverConfig[$this->connectGroup][$key]["port"];
//set the current client key
$this->currentClientKey = $clientKey;
$connectHost = $this->serverConfig[$this->connectGroup][$key]["ip"];
$connectPort = $this->serverConfig[$this->connectGroup][$key]["port"];
break;
case 2:
//using spec
$clientKey = trim($this->connectIp) . "_" . trim($this->connectPort);
//set the current client key
$this->currentClientKey = $clientKey;
$connectHost = $this->connectIp;
$connectPort = $this->connectPort;
break;
default:
throw new \Exception("current connect mode is unknow", -1);
break;
}
if (!isset(self::$client[$clientKey])) {
$client = new \swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
$client->set(array(
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 4,
'package_max_length' => 1024 * 1024 * 2,
'open_tcp_nodelay' => 1,
'socket_buffer_size' => 1024 * 1024 * 4,
));
if (!$client->connect($connectHost, $connectPort, DoraConst::SW_RECIVE_TIMEOUT)) {
//connect fail
$errorCode = $client->errCode;
if ($errorCode == 0) {
$msg = "connect fail.check host dns.";
$errorCode = -1;
} else {
$msg = \socket_strerror($errorCode);
}
if ($key !== "") {
//put the fail connect config to block list
$this->serverConfigBlock[$this->connectGroup][$key] = 1;
}
throw new \Exception($msg . " " . $clientKey, $errorCode);
}
self::$client[$clientKey] = $client;
}
//success
return self::$client[$clientKey];
}
/**
* 获取应用服务器信息 get the backend service stat
* @param string $ip
* @param string $port
* @return array
*/
public function getStat($ip = "", $port = "")
{
$beformode = $this->getConnectMode();
if ($ip != "" && $port != "") {
$mode = array("type" => 2, "ip" => $ip, "port" => $port);
$this->changeMode($mode);
}
$this->guid = $this->generateGuid();
$packet = array(
'api' => array(
"cmd" => array(
'name' => "getStat",
'param' => array(),
),
),
'type' => DoraConst::SW_CONTROL_CMD,
'guid' => $this->guid,
);
$sendData = Packet::packEncode($packet);
$result = $this->doRequest($sendData, DoraConst::SW_MODE_WAITRESULT_SINGLE);
if ($this->guid != $result["guid"]) {
return Packet::packFormat($this->guid, "guid wront please retry..", 100100, $result["data"]);
}
//revert befor connect mode
if ($ip != "" && $port != "") {
//revert befor mode
$this->changeMode($beformode);
}
return $result["data"];
}
/**
* reload 指定服务器的代码
* @param string $ip
* @param string $port
* @return array
*/
public function reloadServerTask($ip = "", $port = "")
{
$beformode = $this->getConnectMode();
if ($ip != "" && $port != "") {
$mode = array("type" => 2, "ip" => $ip, "port" => $port);
$this->changeMode($mode);
}
$this->guid = $this->generateGuid();
$packet = array(
'api' => array(
"cmd" => array(
'name' => "reloadTask",
'param' => array(),
),
),
'type' => DoraConst::SW_CONTROL_CMD,
'guid' => $this->guid,
);
$sendData = Packet::packEncode($packet);
$result = $this->doRequest($sendData, DoraConst::SW_MODE_WAITRESULT_SINGLE);
if ($this->guid != $result["guid"]) {
return Packet::packFormat($this->guid, "guid wront please retry..", 100100, $result["data"]);
}
//revert befor connect mode
if ($ip != "" && $port != "") {
//revert befor mode
$this->changeMode($beformode);
}
return $result["data"];
}
/*
* mode 参数更改说明,以前版本只是sync参数不是mode
* sync :
* true 代表是否阻塞等待结果,
* false 下发任务成功后就返回不等待结果,用于对接口返回没有影响的操作提速
* 改版后----
* mode :
* 0 代表阻塞等待任务执行完毕拿到结果 ;
* 1 代表下发任务成功后就返回不等待结果 ;
* 2 代表下发任务成功后直接返回guid 然后稍晚通过调用阻塞接收函数拿到所有结果
*/
/**
* 单api请求
* @param string $name api地址
* @param array $param 参数
* @param int $mode
* @param int $retry 通讯错误时重试次数
* @param string $ip 要连得ip地址,如果不指定从现有配置随机个
* @param string $port 要连得port地址,如果不指定从现有配置找一个
* @return mixed 返回单个请求结果
* @throws \Exception unknow mode type
*/
public function singleAPI($name, $param, $mode = DoraConst::SW_MODE_WAITRESULT, $retry = 0, $ip = "", $port = "")
{
//get guid
$this->guid = $this->generateGuid();
$packet = array(
'api' => array(
"one" => array(
'name' => $name,
'param' => $param,
)
),
'guid' => $this->guid,
);
switch ($mode) {
case DoraConst::SW_MODE_WAITRESULT:
$packet["type"] = DoraConst::SW_MODE_WAITRESULT_SINGLE;
break;
case DoraConst::SW_MODE_NORESULT:
$packet["type"] = DoraConst::SW_MODE_NORESULT_SINGLE;
break;
case DoraConst::SW_MODE_ASYNCRESULT:
$packet["type"] = DoraConst::SW_MODE_ASYNCRESULT_SINGLE;
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($this->guid, "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 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(
'api' => $params,
'guid' => $this->guid,
);
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($this->guid, "guid wront please retry..", 100100, $result["data"]);
}
return $result;
}
private function doRequest($sendData, $type)
{
//get client obj
try {
$client = $this->getClientObj();
} catch (\Exception $e) {
$data = Packet::packFormat($this->guid, $e->getMessage(), $e->getCode());
return $data;
}
$ret = $client->send($sendData);
//ok fail
if (!$ret) {
$errorcode = $client->errCode;
//destroy error client obj to make reconncet
self::$client[$this->currentClientKey]->close(true);
unset(self::$client[$this->currentClientKey]);
// mark the current connection cannot be used, try another channel
$this->serverConfigBlock[$this->connectGroup][$this->currentClientKey] = 1;
if ($errorcode == 0) {
$msg = "connect fail.check host dns.";
$errorcode = -1;
$packet = Packet::packFormat($this->guid, $msg, $errorcode);
} else {
$msg = \socket_strerror($errorcode);
$packet = Packet::packFormat($this->guid, $msg, $errorcode);
}
return $packet;
}
//if the type is async result will record the guid and client handle
if ($type == DoraConst::SW_MODE_ASYNCRESULT_MULTI || $type == DoraConst::SW_MODE_ASYNCRESULT_SINGLE) {
self::$asynclist[$this->guid] = $client;
}
//recive the response
$data = $this->waitResult($client);
$data["guid"] = $this->guid;
return $data;
}
//for the loop find the right result
//save the async result to the asyncresult static var
//return the right guid request
private function waitResult($client)
{
while (1) {
$result = $client->recv();
if ($result !== false && $result != "") {
$data = Packet::packDecode($result);
//if the async result first deploy success will
if ($data["data"]["guid"] != $this->guid) {
// this data was not we want
//it's may the async result
//when the guid on the asynclist and have isresult =1 on data is async result
//when the guid on the asynclist not have isresult field ond data is first success deploy msg
if (isset(self::$asynclist[$data["data"]["guid"]]) && isset($data["data"]["isresult"]) && $data["data"]["isresult"] == 1) {
//ok recive an async result
//remove the guid on the asynclist
unset(self::$asynclist[$data["data"]["guid"]]);
//add result to async result
self::$asynresult[$data["data"]["guid"]] = $data["data"];
self::$asynresult[$data["data"]["guid"]]["fromwait"] = 1;
} else {
//not in the asynclist drop this packet
continue;
}
} else {
//founded right data
return $data;
}
} else {
//time out
$packet = Packet::packFormat($this->guid, "the recive wrong or timeout", 100009);
return $packet;
}
}
}
public function getAsyncData()
{
//wait all the async result
//when timeout all the error will return
//这里有个坑,我不知道具体哪个client需要recive
while (1) {
if (count(self::$asynclist) > 0) {
foreach (self::$asynclist as $k => $client) {
if ($client->isConnected()) {
$data = $client->recv();
if ($data !== false && $data != "") {
$data = Packet::packDecode($data);
if (isset(self::$asynclist[$data["data"]["guid"]]) && isset($data["data"]["isresult"]) && $data["data"]["isresult"] == 1) {
//ok recive an async result
//remove the guid on the asynclist
unset(self::$asynclist[$data["data"]["guid"]]);
//add result to async result
self::$asynresult[$data["data"]["guid"]] = $data["data"];
self::$asynresult[$data["data"]["guid"]]["fromwait"] = 0;
continue;
} else {
//not in the asynclist drop this packet
continue;
}
} else {
//remove the result
unset(self::$asynclist[$k]);
self::$asynresult[$k] = Packet::packFormat($this->guid, "the recive wrong or timeout", 100009);
continue;
}
} else {
//remove the result
unset(self::$asynclist[$k]);
self::$asynresult[$k] = Packet::packFormat($this->guid, "Get Async Result Fail: Client Closed.", 100012);
continue;
}
} // foreach the list
} else {
break;
}
}//while
$result = self::$asynresult;
self::$asynresult = array();
return Packet::packFormat($this->guid, "OK", 0, $result);
}
//clean up the async list and result
public function clearAsyncData()
{
self::$asynresult = array();
self::$asynclist = array();
}
private function generateGuid()
{
//to make sure the guid is unique for the async result
while (1) {
$guid = md5(microtime(true) . mt_rand(1, 1000000) . mt_rand(1, 1000000));
//prevent the guid on the async list
if (!isset(self::$asynclist[$guid])) {
return $guid;
}
}
}
public function __destruct()
{
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Dora RPC 是一款基于Swoole定长包头通讯协议的最精简的RPC, 用于复杂项目前后端分离,分离后项目都通过API工作可更好的跟踪、升级、维护及管理。
取消

发行版

暂无发行版

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/thinkpc/Dora-RPC.git
git@gitee.com:thinkpc/Dora-RPC.git
thinkpc
Dora-RPC
Dora-RPC
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /