26use InvalidArgumentException;
58 parent::__construct( $conf );
60 $this->mainServers = $conf[
'servers'] ?? [];
61 foreach ( $this->mainServers as $i => $server ) {
63 $this->mainServers[$i][
'master'] =
true;
65 $this->mainServers[$i][
'replica'] =
true;
69 foreach ( ( $conf[
'externalClusters'] ?? [] ) as $cluster => $servers ) {
70 foreach ( $servers as $index => $server ) {
71 $this->externalServersByCluster[$cluster][$index] = $server;
75 $this->loadMonitorClass = $conf[
'loadMonitorClass'] ?? LoadMonitor::class;
78 public function newMainLB( $domain =
false, $owner =
null ) {
83 if ( $this->mainLB ===
null ) {
91 if ( !isset( $this->externalServersByCluster[$cluster] ) ) {
92 throw new InvalidArgumentException(
"Unknown cluster '$cluster'." );
95 return $this->
newLoadBalancer( $this->externalServersByCluster[$cluster], $owner );
99 if ( !isset( $this->externalLBs[$cluster] ) ) {
103 return $this->externalLBs[$cluster];
107 return [ self::CLUSTER_MAIN_DEFAULT => $this->
getMainLB() ];
112 foreach ( array_keys( $this->externalServersByCluster ) as $cluster ) {
123 'servers' => $servers,
124 'loadMonitor' => [
'class' => $this->loadMonitorClass ],
132 public function forEachLB( $callback, array $params = [] ) {
133 if ( $this->mainLB !==
null ) {
134 $callback( $this->mainLB, ...$params );
136 foreach ( $this->externalLBs as $lb ) {
137 $callback( $lb, ...$params );