Repository navigation
Expand file tree
/
Copy pathLoadBalancerManager.php
More file actions
105 lines (94 loc) · 2.93 KB
/
Copy pathLoadBalancerManager.php
File metadata and controls
105 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\LoadBalancer;
use InvalidArgumentException;
use function Hyperf\Support\make;
class LoadBalancerManager
{
/**
* @var array<string, class-string<LoadBalancerInterface>>
*/
private array $algorithms = [
'random' => Random::class,
'round-robin' => RoundRobin::class,
'weighted-random' => WeightedRandom::class,
'weighted-round-robin' => WeightedRoundRobin::class,
];
/**
* @var array<string, LoadBalancerInterface>
*/
private array $instances = [];
/**
* Retrieve a class name of load balancer.
* @return class-string<LoadBalancerInterface>
*/
public function get(string $name): string
{
if (! $this->has($name)) {
throw new InvalidArgumentException(sprintf('The %s algorithm does not exists.', $name));
}
return $this->algorithms[$name];
}
/**
* Retrieve a class name of load balancer and create an object instance,
* If $container object exists, then the class will create via container.
*
* @param string $key key of the load balancer instance
* @param string $algorithm The name of the load balance algorithm
*/
public function getInstance(string $key, string $algorithm): LoadBalancerInterface
{
if (isset($this->instances[$key])) {
return $this->instances[$key];
}
$class = $this->get($algorithm);
if (function_exists('Hyperf\Support\make')) {
$instance = make($class);
} else {
$instance = new $class();
}
$this->instances[$key] = $instance;
return $instance;
}
/**
* Determine if the algorithm is exists.
*/
public function has(string $name): bool
{
return isset($this->algorithms[$name]);
}
/**
* Override the algorithms.
* @param array<string, class-string<LoadBalancerInterface>> $algorithms
*/
public function set(array $algorithms): static
{
foreach ($algorithms as $algorithm) {
if (! class_exists($algorithm)) {
throw new InvalidArgumentException(sprintf('The class of %s algorithm does not exists.', $algorithm));
}
}
$this->algorithms = $algorithms;
return $this;
}
/**
* Register an algorithm to the manager.
* @param class-string<LoadBalancerInterface> $algorithm
*/
public function register(string $key, string $algorithm): self
{
if (! class_exists($algorithm)) {
throw new InvalidArgumentException(sprintf('The class of %s algorithm does not exists.', $algorithm));
}
$this->algorithms[$key] = $algorithm;
return $this;
}
}