RpcConnection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\JsonRpc\Pool;
  12. use Closure;
  13. use Hyperf\Contract\ConnectionInterface;
  14. use Hyperf\Engine\Contract\Socket\SocketFactoryInterface;
  15. use Hyperf\Engine\Contract\SocketInterface;
  16. use Hyperf\Engine\Socket\SocketOption;
  17. use Hyperf\LoadBalancer\Node;
  18. use Hyperf\Pool\Connection as BaseConnection;
  19. use Hyperf\Pool\Exception\ConnectionException;
  20. use Hyperf\Pool\Pool;
  21. use Psr\Container\ContainerInterface;
  22. use function Hyperf\Support\value;
  23. /**
  24. * @property int $errCode
  25. * @property string $errMsg
  26. */
  27. class RpcConnection extends BaseConnection implements ConnectionInterface
  28. {
  29. protected SocketInterface $connection;
  30. protected SocketFactoryInterface $factory;
  31. protected array $config = [
  32. 'node' => null,
  33. 'connect_timeout' => 5.0,
  34. 'settings' => [],
  35. ];
  36. public function __construct(ContainerInterface $container, Pool $pool, array $config)
  37. {
  38. parent::__construct($container, $pool);
  39. $this->factory = $container->get(SocketFactoryInterface::class);
  40. $this->config = array_replace($this->config, $config);
  41. $this->reconnect();
  42. }
  43. public function __get($name)
  44. {
  45. return $this->connection->{$name};
  46. }
  47. public function send(string $data): false|int
  48. {
  49. return $this->connection->sendAll($data);
  50. }
  51. public function recv(float $timeout = 0): false|string
  52. {
  53. return $this->recvPacket($timeout);
  54. }
  55. public function recvPacket(float $timeout = 0): false|string
  56. {
  57. return $this->connection->recvPacket($timeout);
  58. }
  59. /**
  60. * @return $this
  61. * @throws ConnectionException
  62. */
  63. public function getActiveConnection()
  64. {
  65. if ($this->check()) {
  66. return $this;
  67. }
  68. if (! $this->reconnect()) {
  69. throw new ConnectionException('Connection reconnect failed.');
  70. }
  71. return $this;
  72. }
  73. public function reconnect(): bool
  74. {
  75. if (! $this->config['node'] instanceof Closure) {
  76. throw new ConnectionException('Node of Connection is invalid.');
  77. }
  78. /** @var Node $node */
  79. $node = value($this->config['node']);
  80. $host = $node->host;
  81. $port = $node->port;
  82. $connectTimeout = $this->config['connect_timeout'];
  83. $this->connection = $this->factory->make(new SocketOption(
  84. $host,
  85. $port,
  86. $connectTimeout,
  87. $this->config['settings'] ?? []
  88. ));
  89. $this->lastUseTime = microtime(true);
  90. return true;
  91. }
  92. public function close(): bool
  93. {
  94. $this->lastUseTime = 0.0;
  95. $this->connection->close();
  96. return true;
  97. }
  98. public function resetLastUseTime(): void
  99. {
  100. $this->lastUseTime = 0.0;
  101. }
  102. }