LoadBalancerManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\LoadBalancer;
  12. use InvalidArgumentException;
  13. use function Hyperf\Support\make;
  14. class LoadBalancerManager
  15. {
  16. /**
  17. * @var array<string, class-string<LoadBalancerInterface>>
  18. */
  19. private array $algorithms = [
  20. 'random' => Random::class,
  21. 'round-robin' => RoundRobin::class,
  22. 'weighted-random' => WeightedRandom::class,
  23. 'weighted-round-robin' => WeightedRoundRobin::class,
  24. ];
  25. /**
  26. * @var array<string, LoadBalancerInterface>
  27. */
  28. private array $instances = [];
  29. /**
  30. * Retrieve a class name of load balancer.
  31. * @return class-string<LoadBalancerInterface>
  32. */
  33. public function get(string $name): string
  34. {
  35. if (! $this->has($name)) {
  36. throw new InvalidArgumentException(sprintf('The %s algorithm does not exists.', $name));
  37. }
  38. return $this->algorithms[$name];
  39. }
  40. /**
  41. * Retrieve a class name of load balancer and create an object instance,
  42. * If $container object exists, then the class will create via container.
  43. *
  44. * @param string $key key of the load balancer instance
  45. * @param string $algorithm The name of the load balance algorithm
  46. */
  47. public function getInstance(string $key, string $algorithm): LoadBalancerInterface
  48. {
  49. if (isset($this->instances[$key])) {
  50. return $this->instances[$key];
  51. }
  52. $class = $this->get($algorithm);
  53. if (function_exists('Hyperf\Support\make')) {
  54. $instance = make($class);
  55. } else {
  56. $instance = new $class();
  57. }
  58. $this->instances[$key] = $instance;
  59. return $instance;
  60. }
  61. /**
  62. * Determine if the algorithm is exists.
  63. */
  64. public function has(string $name): bool
  65. {
  66. return isset($this->algorithms[$name]);
  67. }
  68. /**
  69. * Override the algorithms.
  70. * @param array<string, class-string<LoadBalancerInterface>> $algorithms
  71. */
  72. public function set(array $algorithms): static
  73. {
  74. foreach ($algorithms as $algorithm) {
  75. if (! class_exists($algorithm)) {
  76. throw new InvalidArgumentException(sprintf('The class of %s algorithm does not exists.', $algorithm));
  77. }
  78. }
  79. $this->algorithms = $algorithms;
  80. return $this;
  81. }
  82. /**
  83. * Register an algorithm to the manager.
  84. * @param class-string<LoadBalancerInterface> $algorithm
  85. */
  86. public function register(string $key, string $algorithm): self
  87. {
  88. if (! class_exists($algorithm)) {
  89. throw new InvalidArgumentException(sprintf('The class of %s algorithm does not exists.', $algorithm));
  90. }
  91. $this->algorithms[$key] = $algorithm;
  92. return $this;
  93. }
  94. }