diff options
author | Stanley Cheung <stanleycheung@google.com> | 2015-08-13 09:39:04 -0700 |
---|---|---|
committer | Stanley Cheung <stanleycheung@google.com> | 2015-08-13 10:48:32 -0700 |
commit | 04b7a41d18744d2e882a486862ec2afb7433655f (patch) | |
tree | 63460f01d251f8bd957b6e252f0e3f4b354854b5 /src/php/lib/Grpc/BaseStub.php | |
parent | 4c5c7b8647522fa9f21b33add09ae4b7d5ca6512 (diff) |
php: add watchForReady method
Diffstat (limited to 'src/php/lib/Grpc/BaseStub.php')
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index 9f5c07b5f5..dc507df92c 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -83,6 +83,40 @@ class BaseStub { } /** + * @param $timeout in microseconds + * @return bool true if channel is ready + */ + public function watchForReady($timeout) { + $new_state = $this->getConnectivityState(true); + if ($this->_checkConnectivityState($new_state)) { + return true; + } + + $now = Timeval::now(); + $delta = new Timeval($timeout); + $deadline = $now->add($delta); + + if (!$this->channel->watchConnectivityState($new_state, $deadline)) { + return false; + } + $new_state = $this->getConnectivityState(); + if ($this->_checkConnectivityState($new_state)) { + return true; + } + return false; + } + + private function _checkConnectivityState($new_state) { + if ($new_state == Grpc\CHANNEL_READY) { + return true; + } + if ($new_state == Grpc\CHANNEL_FATAL_ERROR) { + throw new Exception('Failed to connect to server'); + } + return false; + } + + /** * Close the communication channel associated with this stub */ public function close() { |