JsonRpcHttpTransporter.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 GuzzleHttp\Client;
  13. use GuzzleHttp\RequestOptions;
  14. use Hyperf\Guzzle\ClientFactory;
  15. use Hyperf\LoadBalancer\LoadBalancerInterface;
  16. use Hyperf\LoadBalancer\Node;
  17. use Hyperf\Rpc\Contract\TransporterInterface;
  18. use RuntimeException;
  19. use function Hyperf\Support\value;
  20. class JsonRpcHttpTransporter implements TransporterInterface
  21. {
  22. private ?LoadBalancerInterface $loadBalancer = null;
  23. /**
  24. * If $loadBalancer is null, will select a node in $nodes to request,
  25. * otherwise, use the nodes in $loadBalancer.
  26. *
  27. * @var Node[]
  28. */
  29. private array $nodes = [];
  30. private float $connectTimeout = 5;
  31. private float $recvTimeout = 5;
  32. private array $clientOptions;
  33. public function __construct(private ClientFactory $clientFactory, array $config = [])
  34. {
  35. if (! isset($config['recv_timeout'])) {
  36. $config['recv_timeout'] = $this->recvTimeout;
  37. }
  38. if (! isset($config['connect_timeout'])) {
  39. $config['connect_timeout'] = $this->connectTimeout;
  40. }
  41. $this->clientOptions = $config;
  42. }
  43. public function send(string $data)
  44. {
  45. $node = $this->getNode();
  46. $uri = $node->host . ':' . $node->port . $node->pathPrefix;
  47. $schema = value(function () use ($node) {
  48. $schema = 'http';
  49. if ($node->schema !== null) {
  50. $schema = $node->schema;
  51. }
  52. if (! in_array($schema, ['http', 'https'])) {
  53. $schema = 'http';
  54. }
  55. $schema .= '://';
  56. return $schema;
  57. });
  58. $url = $schema . $uri;
  59. $response = $this->getClient()->post($url, [
  60. RequestOptions::HEADERS => [
  61. 'Content-Type' => 'application/json',
  62. ],
  63. RequestOptions::HTTP_ERRORS => false,
  64. RequestOptions::BODY => $data,
  65. ]);
  66. if ($response->getStatusCode() === 200) {
  67. return (string) $response->getBody();
  68. }
  69. return '';
  70. }
  71. public function recv()
  72. {
  73. throw new RuntimeException(__CLASS__ . ' does not support recv method.');
  74. }
  75. public function getClient(): Client
  76. {
  77. $clientOptions = $this->clientOptions;
  78. // Swoole HTTP Client cannot set recv_timeout and connect_timeout options, use timeout.
  79. $clientOptions['timeout'] = $clientOptions['recv_timeout'] + $clientOptions['connect_timeout'];
  80. unset($clientOptions['recv_timeout'], $clientOptions['connect_timeout']);
  81. return $this->clientFactory->create($clientOptions);
  82. }
  83. public function getLoadBalancer(): ?LoadBalancerInterface
  84. {
  85. return $this->loadBalancer;
  86. }
  87. public function setLoadBalancer(LoadBalancerInterface $loadBalancer): TransporterInterface
  88. {
  89. $this->loadBalancer = $loadBalancer;
  90. return $this;
  91. }
  92. /**
  93. * @param \Hyperf\LoadBalancer\Node[] $nodes
  94. */
  95. public function setNodes(array $nodes): self
  96. {
  97. $this->nodes = $nodes;
  98. return $this;
  99. }
  100. public function getNodes(): array
  101. {
  102. return $this->nodes;
  103. }
  104. public function getClientOptions(): array
  105. {
  106. return $this->clientOptions;
  107. }
  108. /**
  109. * If the load balancer is exists, then the node will select by the load balancer,
  110. * otherwise will get a random node.
  111. */
  112. private function getNode(): Node
  113. {
  114. if ($this->loadBalancer instanceof LoadBalancerInterface) {
  115. return $this->loadBalancer->select();
  116. }
  117. return $this->nodes[array_rand($this->nodes)];
  118. }
  119. }