diff options
author | Stanley Cheung <stanleycheung@google.com> | 2015-06-15 11:45:00 -0700 |
---|---|---|
committer | Stanley Cheung <stanleycheung@google.com> | 2015-06-15 17:22:39 -0700 |
commit | cc019af9cc2951bf7e0cdc17fb118e239dc785c3 (patch) | |
tree | e2fa22fb3dac1de958b5feddfaae413806a1c273 /src/php/lib | |
parent | 0d6cf992e8f840ae257c039f15fc045e9605090f (diff) |
add PHP timeout interop test
Diffstat (limited to 'src/php/lib')
-rw-r--r-- | src/php/lib/Grpc/AbstractCall.php | 14 | ||||
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 31 |
2 files changed, 35 insertions, 10 deletions
diff --git a/src/php/lib/Grpc/AbstractCall.php b/src/php/lib/Grpc/AbstractCall.php index 1add972589..5b28417a0d 100644 --- a/src/php/lib/Grpc/AbstractCall.php +++ b/src/php/lib/Grpc/AbstractCall.php @@ -43,9 +43,19 @@ abstract class AbstractCall { * Create a new Call wrapper object. * @param Channel $channel The channel to communicate on * @param string $method The method to call on the remote server + * @param callback $deserialize A callback function to deserialize + * the response + * @param (optional) long $timeout Timeout in microseconds */ - public function __construct(Channel $channel, $method, $deserialize) { - $this->call = new Call($channel, $method, Timeval::infFuture()); + public function __construct(Channel $channel, $method, $deserialize, $timeout = false) { + if ($timeout) { + $now = Timeval::now(); + $delta = new Timeval($timeout); + $deadline = $now->add($delta); + } else { + $deadline = Timeval::infFuture(); + } + $this->call = new Call($channel, $method, $deadline); $this->deserialize = $deserialize; $this->metadata = null; } diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index b84b6b86f8..48c00977eb 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -83,6 +83,21 @@ class BaseStub { return "https://" . $this->hostname . $service_name; } + /** + * extract $timeout from $metadata + * @param $metadata The metadata map + * @return list($metadata_copy, $timeout) + */ + private function _extract_timeout_from_metadata($metadata) { + $timeout = false; + $metadata_copy = $metadata; + if (isset($metadata['timeout'])) { + $timeout = $metadata['timeout']; + unset($metadata_copy['timeout']); + } + return array($metadata_copy, $timeout); + } + /* This class is intended to be subclassed by generated code, so all functions begin with "_" to avoid name collisions. */ @@ -99,8 +114,8 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - $call = new UnaryCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new UnaryCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -126,8 +141,8 @@ class BaseStub { $arguments, callable $deserialize, $metadata = array()) { - $call = new ClientStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -152,8 +167,8 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - $call = new ServerStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, @@ -175,8 +190,8 @@ class BaseStub { public function _bidiRequest($method, callable $deserialize, $metadata = array()) { - $call = new BidiStreamingCall($this->channel, $method, $deserialize); - $actual_metadata = $metadata; + list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); + $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout); $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, |