Skip to content

Commit

Permalink
新增请求UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Dec 23, 2020
1 parent e707557 commit f7e9efd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function exec(float $timeout = 3.0)
$pack->setModule($module);
$pack->setAction($action);
$pack->setArg($requestContext->getArg());
$pack->setRequestUUID($requestContext->getRequestUUID());
$client = new TcpClient($this->config->getMaxPackageSize(),$timeout);
if(!$client->connect($node)){
$res->setStatus(Response::STATUS_CONNECT_TIMEOUT);
Expand All @@ -101,7 +102,7 @@ function exec(float $timeout = 3.0)
$left = $timeout;
while ((time() < $start + $timeout) && $all > 0){
$t = microtime(true);
$ret = $channel->pop($left + 1);
$ret = $channel->pop($left + 0.01);
if($ret){
$all--;
$this->execCallback($ret['response'],$ret['context']);
Expand Down
15 changes: 15 additions & 0 deletions src/Client/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use EasySwoole\Rpc\Server\ServiceNode;
use EasySwoole\Utility\Random;

class RequestContext
{
Expand All @@ -20,6 +21,20 @@ class RequestContext
private $onFail;
/** @var ServiceNode|null */
private $serviceNode;
/** @var string */
private $requestUUID;

public function __construct()
{
$this->requestUUID = Random::makeUUIDV4();
}
/**
* @return string
*/
public function getRequestUUID(): string
{
return $this->requestUUID;
}

/**
* @return string
Expand Down
18 changes: 18 additions & 0 deletions src/Protocol/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Request extends SplBean
protected $action;
/** @var mixed */
protected $arg;
/** @var string */
protected $requestUUID;

/**
* @return string|null
Expand Down Expand Up @@ -80,4 +82,20 @@ public function setArg($arg): void
{
$this->arg = $arg;
}

/**
* @return string
*/
public function getRequestUUID(): string
{
return $this->requestUUID;
}

/**
* @param string $requestUUID
*/
public function setRequestUUID(string $requestUUID): void
{
$this->requestUUID = $requestUUID;
}
}
10 changes: 10 additions & 0 deletions src/Protocol/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use EasySwoole\Spl\SplBean;
use EasySwoole\Utility\Random;

class Response extends SplBean
{
Expand All @@ -26,6 +27,8 @@ class Response extends SplBean
protected $result;
/** @var mixed $msg */
protected $msg;
/** @var string */
protected $responseUUID;

public static function status2msg(int $status)
{
Expand Down Expand Up @@ -122,4 +125,11 @@ public function setResult($result): self
$this->result = $result;
return $this;
}

protected function initialize(): void
{
if(empty($this->responseUUID)){
$this->responseUUID = Random::makeUUIDV4();
}
}
}

0 comments on commit f7e9efd

Please sign in to comment.