From 7ccfa35e3b4ad2f4b12d5710bad8c76a262a5d91 Mon Sep 17 00:00:00 2001 From: zhanglei28 Date: Wed, 30 Oct 2024 18:11:25 +0800 Subject: [PATCH] fix client call with multi params --- .gitignore | 4 +++ phpunit.xml | 16 ++++++------ src/Motan/Client.php | 21 ++++++++++------ src/Motan/Endpointer.php | 39 ++++++++++++++++-------------- src/Motan/Transport/Connection.php | 10 ++++---- tests/Motan/ClientTest.php | 12 ++++++--- 6 files changed, 59 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 8de10f1..71c6ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ agent.pid # debugger .gdb* /tests/MockServer/log.txt + +composer.phar +composer.lock +.phpunit.cache/ \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index ebe8738..3f78f1d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,13 +1,13 @@ diff --git a/src/Motan/Client.php b/src/Motan/Client.php index 9d7fde7..5ff6f74 100644 --- a/src/Motan/Client.php +++ b/src/Motan/Client.php @@ -47,9 +47,9 @@ public function __construct(URL $url_obj = NULL) try { $mesh_isalive = $connection->buildConnection($agent_addr); } catch (\Exception $e) { - error_log("weibo-mesh isn't alive " . $e->getMessage() ); + error_log("weibo-mesh isn't alive " . $e->getMessage()); } - if ($mesh_isalive){ + if ($mesh_isalive) { $this->_url_obj->setEndpoint(Constants::ENDPOINT_AGENT); $this->_endpoint = new Agent($this->_url_obj); $this->_endpoint->setConnectionObj($connection); @@ -78,7 +78,8 @@ public function setConnectionTimeOut($time_out) $this->_endpoint->setConnectionTimeOut($time_out); } - public function getEndPoint() { + public function getEndPoint() + { return $this->_endpoint; } @@ -132,8 +133,11 @@ public function __call($name, $arguments) { $request_id = $request_args = $request_header = NULL; isset($arguments[0]) && !empty($arguments[0]) && $request_args = $arguments[0]; + if (!is_array($request_args)) { // single parameter + $request_args = array($request_args); + } $request = new \Motan\Request($this->_url_obj->getService(), - $name, ...[$request_args]); + $name, ...$request_args); $request->addHeaders($this->_url_obj->getHeaders()); isset($arguments[1]) && !empty($arguments[1]) && $request->addHeaders($arguments[1]); isset($arguments[2]) && !empty($arguments[2]) && $request->setRequestId($arguments[2]); @@ -164,12 +168,13 @@ public function __call($name, $arguments) /** * multiCall calls a method on multiple backend. * @param URL[] $url_objs array of backend URL object. - * @param string $method RPC method name. - * @param mixed ...$args arguments will pass to method. + * @param string $method RPC method name. + * @param mixed ...$args arguments will pass to method. * @return array arary of called result, index 0 is the first response of $url_objs. * @throws \Exception, you should try to catch it. */ - public function multiCall(array $url_objs,string $method, ...$args) { + public function multiCall(array $url_objs, string $method, ...$args) + { if (empty($url_objs)) { return []; } @@ -177,7 +182,7 @@ public function multiCall(array $url_objs,string $method, ...$args) { $request_objs = []; foreach ($url_objs as $url_obj) { $request = new \Motan\Request($url_obj->getService(), - $method, ...$args); + $method, ...$args); $request->addHeaders($url_obj->getHeaders()); $request->setGroup($url_obj->getGroup()); $request_objs[] = $request; diff --git a/src/Motan/Endpointer.php b/src/Motan/Endpointer.php index 3878c96..d998586 100644 --- a/src/Motan/Endpointer.php +++ b/src/Motan/Endpointer.php @@ -106,7 +106,8 @@ protected function _buildConnection() return $this->_connection = $this->_connection_obj->getConnection(); } - public function setConnectionObj(Connection $conn_obj) { + public function setConnectionObj(Connection $conn_obj) + { $this->_connection_obj = $conn_obj; $this->_connection = $this->_connection_obj->getConnection(); } @@ -114,7 +115,7 @@ public function setConnectionObj(Connection $conn_obj) { public function doUpload(\Motan\Request $request) { $this->_buildConnection(); - if( !$this->_connection) { + if (!$this->_connection) { throw new \Exception("Connection has gone away!"); } @@ -151,7 +152,7 @@ public function doUpload(\Motan\Request $request) $sent = @fwrite($this->_connection, $buffer, $length); if ($sent === false) { $stream_meta = stream_get_meta_data($this->_connection); - if($stream_meta['timed_out'] == TRUE) { + if ($stream_meta['timed_out'] == TRUE) { throw new \Exception('Write to remote timeout.'); } else { throw new \Exception('Unknow error when write to remote. Stream detail:' . var_export($stream_meta, TRUE)); @@ -175,7 +176,7 @@ public function doUpload(\Motan\Request $request) $sent += @fwrite($this->_connection, $buff); } if ($sent != $file_size) { - throw new \Exception("upload fail, need to upload:${file_size}, but only uploaded:${sent}" . var_export(stream_get_meta_data($this->_connection), TRUE)); + throw new \Exception("upload fail, need to upload:{$file_size}, but only uploaded:{$sent}" . var_export(stream_get_meta_data($this->_connection), TRUE)); } @fclose($in); @@ -219,13 +220,13 @@ protected function _doHTTPCall(\Motan\Request $request) $status_code = \curl_getinfo($ch, CURLINFO_HTTP_CODE); $exception = NULL; if ($status_code == 0) { - $exception = new \Exception("bad request to httpcalling error, url is ${url}"); + $exception = new \Exception("bad request to httpcalling error, url is {$url}"); } if ($status_code >= 400) { - $exception = new \Exception("back to httpcalling error, url is ${url}"); + $exception = new \Exception("back to httpcalling error, url is {$url}"); } $request_id = $request->getRequestId(); - $raw_header = \Motan\Protocol\Motan::buildResponseHeader($request_id,SERIALIZE_SIMPLE, $status_code); + $raw_header = \Motan\Protocol\Motan::buildResponseHeader($request_id, SERIALIZE_SIMPLE, $status_code); $metadata['M_p'] = $request->getService(); $metadata['M_m'] = $request->getMethod(); $metadata['M_g'] = $request->getGroup(); @@ -283,11 +284,11 @@ protected function _doSend(\Motan\Request $request) { // aquires seialization type from request itself. // notice: $request_seria is a string typed flag. - $request_seria=$request->getSerialization(); + $request_seria = $request->getSerialization(); // create seialization object from $request_seria. - $serialization=empty($request_seria)?$this->_url_obj->getSerialization():$request_seria; + $serialization = empty($request_seria) ? $this->_url_obj->getSerialization() : $request_seria; // if create fail, using $this->_serializer as default serializer. - $serializer=empty($request_seria)?$this->_serializer:$request->getSerializer(); + $serializer = empty($request_seria) ? $this->_serializer : $request->getSerializer(); if ($this->_url_obj->getUrlType() == Constants::REQ_URL_TYPE_RESTY || FALSE !== strpos($request->getMethod(), '/')) { @@ -305,10 +306,10 @@ protected function _doSend(\Motan\Request $request) throw new \Exception("Couldn't get correct group."); } } - if( !$this->_connection) { + if (!$this->_connection) { throw new \Exception("Connection has gone away!"); } - if(!is_null($request->getRequestArgs())){ + if (!is_null($request->getRequestArgs())) { $req_body = $serializer->serializeMulti(...$request->getRequestArgs()); } @@ -341,22 +342,24 @@ protected function _doRecv($resp_obj = NULL) return $this->_parseRespMsg($resp_msg, $resp_obj); } - protected function _doRecvRespMsg() { + protected function _doRecvRespMsg() + { return $this->_connection_obj->read(); } - protected function _parseRespMsg($resp_msg, $resp_obj = NULL) { + protected function _parseRespMsg($resp_msg, $resp_obj = NULL) + { $resp_body = $resp_msg->getBody(); if ($resp_msg->getHeader()->isGzip()) { $resp_body = zlib_decode($resp_body); } // aquires seialization type from response header. - $resp_seria=$resp_msg->getHeader()->getSerialize(); + $resp_seria = $resp_msg->getHeader()->getSerialize(); // create seialization object from $resp_seria. // notice: $resp_seria is a int typed flag. - $serializer=Utils::getSerializer($resp_seria); + $serializer = Utils::getSerializer($resp_seria); // if create fail, using $this->_serializer as default serializer. - empty($serializer)&&$serializer=$this->_serializer; + empty($serializer) && $serializer = $this->_serializer; $res = $exception = NULL; $res = $serializer->deserialize($resp_obj, $resp_body); @@ -429,7 +432,7 @@ public function doMultiCall($request_objs) $respMsg = $this->_doRecvRespMsg(); $requestId = $respMsg->getHeader()->getRequestId(); $resp_obj = isset($requests[$requestId]) ? $requests[$requestId]->getRespSerializerObj() : null; - $resp= $this->_parseRespMsg($respMsg, $resp_obj); + $resp = $this->_parseRespMsg($respMsg, $resp_obj); } catch (\Exception $e) { array_push($multi_exceptions, $e); continue; diff --git a/src/Motan/Transport/Connection.php b/src/Motan/Transport/Connection.php index 03cb156..51d5b8b 100644 --- a/src/Motan/Transport/Connection.php +++ b/src/Motan/Transport/Connection.php @@ -21,11 +21,11 @@ /** * TCP Connection for PHP 5.6+ - * + * *
  * TCP 连接
  * 
- * + * * @author idevz * @version V1.0 [created at: 2016-11-18] */ @@ -74,14 +74,14 @@ private function _initConnection() } } if (!$connection) { - throw new \Exception("Connect to $this->_connection_addr fail, err_code:${err_code},err_msg:${err_msg} "); + throw new \Exception("Connect to $this->_connection_addr fail, err_code:{$err_code},err_msg:{$err_msg} "); } $this->_connection = $connection; $this->_setStreamOpt(); return true; } - private function _setStreamOpt() + private function _setStreamOpt() { if (!is_resource($this->_connection)) { return false; @@ -102,7 +102,7 @@ public function write($buffer) $sent = @fwrite($this->_connection, $buffer, $length); if ($sent === false) { $stream_meta = stream_get_meta_data($this->_connection); - if($stream_meta['timed_out'] == TRUE) { + if ($stream_meta['timed_out'] == TRUE) { throw new \Exception('Write to remote timeout.'); } else { throw new \Exception('Unknow error when write to remote. Stream detail:' . var_export($stream_meta, TRUE)); diff --git a/tests/Motan/ClientTest.php b/tests/Motan/ClientTest.php index d5cf660..ef62b75 100644 --- a/tests/Motan/ClientTest.php +++ b/tests/Motan/ClientTest.php @@ -1,4 +1,5 @@ setConnectionTimeOut(50000); @@ -31,7 +32,7 @@ protected function setUp() : void * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ - protected function tearDown() : void + protected function tearDown(): void { } @@ -77,7 +78,7 @@ public function testGetResponseMetadata() */ public function testGetResponseException() { - $this->object->doCall('HelloX', 222, 123, 124, ['string','arr']); + $this->object->doCall('HelloX', 222, 123, 124, ['string', 'arr']); $rs = $this->object->getResponseException(); $this->assertEquals('{"errcode":500,"errmsg":"method HelloX is not found in provider.","errtype":1}', $rs); } @@ -91,7 +92,6 @@ public function testGetResponse() $params = "testmsg"; $this->object->doCall('Hello', $params); $rs = $this->object->getResponse(); - $this->assertObjectHasAttribute('_type',$rs); $this->assertEquals(MSG_TYPE_RESPONSE, $rs->getType()); } @@ -115,6 +115,10 @@ public function test__call() $params = "testmsg"; $rs = $this->object->Hello($params); $this->assertEquals("hello testmsg", $rs); + + // use array params(for multi params) + $rs = $this->object->Hello([$params]); + $this->assertEquals("hello testmsg", $rs); } /**