JsonRpcTransporter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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;
  12. use Hyperf\Context\ApplicationContext;
  13. use Hyperf\Context\Context;
  14. use Hyperf\Engine\Contract\Socket\SocketFactoryInterface;
  15. use Hyperf\Engine\Contract\SocketInterface;
  16. use Hyperf\Engine\Socket\SocketOption;
  17. use Hyperf\LoadBalancer\LoadBalancerInterface;
  18. use Hyperf\LoadBalancer\Node;
  19. use Hyperf\Rpc\Contract\TransporterInterface;
  20. use RuntimeException;
  21. use function Hyperf\Support\retry;
  22. class JsonRpcTransporter implements TransporterInterface
  23. {
  24. use RecvTrait;
  25. private ?LoadBalancerInterface $loadBalancer;
  26. /**
  27. * If $loadBalancer is null, will select a node in $nodes to request,
  28. * otherwise, use the nodes in $loadBalancer.
  29. *
  30. * @var Node[]
  31. */
  32. private array $nodes = [];
  33. private float $connectTimeout;
  34. private float $recvTimeout;
  35. private array $config = [];
  36. public function __construct(array $config = [])
  37. {
  38. $this->config = array_replace_recursive($this->config, $config);
  39. $this->recvTimeout = $this->config['recv_timeout'] ?? 5.0;
  40. $this->connectTimeout = $this->config['connect_timeout'] ?? 5.0;
  41. }
  42. public function send(string $data)
  43. {
  44. $client = retry(2, function () use ($data) {
  45. $client = $this->getClient();
  46. if ($client->sendAll($data) === false) {
  47. throw new RuntimeException('Connect to server failed.');
  48. }
  49. return $client;
  50. });
  51. return $this->recvAndCheck($client, $this->recvTimeout);
  52. }
  53. public function recv()
  54. {
  55. $client = $this->getClient();
  56. return $this->recvAndCheck($client, $this->recvTimeout);
  57. }
  58. public function getClient(): SocketInterface
  59. {
  60. $class = spl_object_hash($this) . '.Connection';
  61. if (Context::has($class)) {
  62. return Context::get($class);
  63. }
  64. return Context::set($class, retry(2, function () {
  65. $node = $this->getNode();
  66. return $this->getSocketFactory()->make(new SocketOption(
  67. $node->host,
  68. $node->port,
  69. $this->connectTimeout,
  70. $this->config['settings'] ?? []
  71. ));
  72. }));
  73. }
  74. public function getLoadBalancer(): ?LoadBalancerInterface
  75. {
  76. return $this->loadBalancer;
  77. }
  78. public function setLoadBalancer(LoadBalancerInterface $loadBalancer): TransporterInterface
  79. {
  80. $this->loadBalancer = $loadBalancer;
  81. return $this;
  82. }
  83. /**
  84. * @param \Hyperf\LoadBalancer\Node[] $nodes
  85. */
  86. public function setNodes(array $nodes): self
  87. {
  88. $this->nodes = $nodes;
  89. return $this;
  90. }
  91. public function getNodes(): array
  92. {
  93. return $this->nodes;
  94. }
  95. /**
  96. * If the load balancer is exists, then the node will select by the load balancer,
  97. * otherwise will get a random node.
  98. */
  99. private function getNode(): Node
  100. {
  101. if ($this->loadBalancer instanceof LoadBalancerInterface) {
  102. return $this->loadBalancer->select();
  103. }
  104. return $this->nodes[array_rand($this->nodes)];
  105. }
  106. private function getSocketFactory(): SocketFactoryInterface
  107. {
  108. return ApplicationContext::getContainer()->get(SocketFactoryInterface::class);
  109. }
  110. }