aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/php/lib
diff options
context:
space:
mode:
authorGravatar Alistair Veitch <aveitch@google.com>2015-08-25 15:36:20 -0700
committerGravatar Alistair Veitch <aveitch@google.com>2015-08-25 15:36:20 -0700
commita4e884721d4333eef8b208b26ff35f1dde25b6c1 (patch)
treea82dabdddbe7d00190be04b299a378aed4041f24 /src/php/lib
parentf886985d2c775d480079ea979323ae22efc0afc6 (diff)
parent3cfb4795ceaaebff3b33f2ef7612ffcff72b53ab (diff)
merge to head
Diffstat (limited to 'src/php/lib')
-rwxr-xr-xsrc/php/lib/Grpc/BaseStub.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php
index a0c677908c..2e980c5eed 100755
--- a/src/php/lib/Grpc/BaseStub.php
+++ b/src/php/lib/Grpc/BaseStub.php
@@ -75,6 +75,51 @@ class BaseStub {
}
/**
+ * @param $try_to_connect bool
+ * @return int The grpc connectivity state
+ */
+ public function getConnectivityState($try_to_connect = false) {
+ return $this->channel->getConnectivityState($try_to_connect);
+ }
+
+ /**
+ * @param $timeout in microseconds
+ * @return bool true if channel is ready
+ * @throw Exception if channel is in FATAL_ERROR state
+ */
+ public function waitForReady($timeout) {
+ $new_state = $this->getConnectivityState(true);
+ if ($this->_checkConnectivityState($new_state)) {
+ return true;
+ }
+
+ $now = Timeval::now();
+ $delta = new Timeval($timeout);
+ $deadline = $now->add($delta);
+
+ while ($this->channel->watchConnectivityState($new_state, $deadline)) {
+ // state has changed before deadline
+ $new_state = $this->getConnectivityState();
+ if ($this->_checkConnectivityState($new_state)) {
+ return true;
+ }
+ }
+ // deadline has passed
+ $new_state = $this->getConnectivityState();
+ return $this->_checkConnectivityState($new_state);
+ }
+
+ private function _checkConnectivityState($new_state) {
+ if ($new_state == \Grpc\CHANNEL_READY) {
+ return true;
+ }
+ if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
+ throw new Exception('Failed to connect to server');
+ }
+ return false;
+ }
+
+ /**
* Close the communication channel associated with this stub
*/
public function close() {