ChannelRef.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. *
  4. * Copyright 2018 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. namespace Grpc\Gcp;
  20. /**
  21. * ChannelRef is used to record how many active streams the channel has.
  22. * This is a private class
  23. */
  24. class ChannelRef
  25. {
  26. // $opts has all information except Credentials for creating a Grpc\Channel.
  27. private $opts;
  28. private $channel_id;
  29. private $affinity_ref;
  30. private $active_stream_ref;
  31. private $target;
  32. private $has_deserialized;
  33. private $real_channel;
  34. public function __construct($target, $channel_id, $opts, $affinity_ref=0, $active_stream_ref=0)
  35. {
  36. $this->target = $target;
  37. $this->channel_id = $channel_id;
  38. $this->affinity_ref = $affinity_ref;
  39. $this->active_stream_ref = $active_stream_ref;
  40. $this->opts = $opts;
  41. $this->has_deserialized = new CreatedByDeserializeCheck();
  42. }
  43. public function getRealChannel($credentials)
  44. {
  45. // TODO(ddyihai): remove this check once the serialize handler for
  46. // \Grpc\Channel is implemented(issue https://github.com/grpc/grpc/issues/15870).
  47. if (!$this->has_deserialized->getData()) {
  48. // $real_channel exists and is not created by the deserialization.
  49. return $this->real_channel;
  50. }
  51. // If this ChannelRef is created by deserialization, $real_channel is invalid
  52. // thus needs to be recreated becasue Grpc\Channel don't have serialize and
  53. // deserialize handler.
  54. // Since [target + augments + credentials] will be the same during the recreation,
  55. // it will reuse the underline grpc channel in C extension without creating a
  56. // new connection.
  57. // 'credentials' in the array $opts will be unset during creating the channel.
  58. if (!array_key_exists('credentials', $this->opts)) {
  59. $this->opts['credentials'] = $credentials;
  60. }
  61. $real_channel = new \Grpc\Channel($this->target, $this->opts);
  62. $this->real_channel = $real_channel;
  63. // Set deserialization to false so it won't be recreated within the same script.
  64. $this->has_deserialized->setData(0);
  65. return $real_channel;
  66. }
  67. public function getAffinityRef()
  68. {
  69. return $this->affinity_ref;
  70. }
  71. public function getActiveStreamRef()
  72. {
  73. return $this->active_stream_ref;
  74. }
  75. public function affinityRefIncr()
  76. {
  77. $this->affinity_ref += 1;
  78. }
  79. public function affinityRefDecr()
  80. {
  81. $this->affinity_ref -= 1;
  82. }
  83. public function activeStreamRefIncr()
  84. {
  85. $this->active_stream_ref += 1;
  86. }
  87. public function activeStreamRefDecr()
  88. {
  89. $this->active_stream_ref -= 1;
  90. }
  91. }