| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use BadMethodCallException; |
| 23 |
use InvalidArgumentException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class LBFactorySingle extends LBFactory { |
| 36 |
|
| 37 |
private $mainLB; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function __construct( array $conf ) { |
| 46 |
parent::__construct( $conf ); |
| 47 |
|
| 48 |
if ( !array_key_exists( 'connection', $conf ) ) { |
| 49 |
throw new InvalidArgumentException( "Missing 'connection' argument." ); |
| 50 |
} |
| 51 |
|
| 52 |
$conn = $conf['connection']; |
| 53 |
if ( $conn ) { |
| 54 |
$mainLB = new LoadBalancerSingle( array_merge( |
| 55 |
$this->baseLoadBalancerParams(), |
| 56 |
[ 'connection' => $conn ] |
| 57 |
) ); |
| 58 |
} else { |
| 59 |
$mainLB = new LoadBalancerDisabled( $this->baseLoadBalancerParams() ); |
| 60 |
} |
| 61 |
$this->initLoadBalancer( $mainLB ); |
| 62 |
|
| 63 |
$this->mainLB = $mainLB; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function newFromConnection( IDatabase $db, array $params = [] ) { |
| 73 |
return new static( array_merge( |
| 74 |
[ 'localDomain' => $db->getDomainID() ], |
| 75 |
$params, |
| 76 |
[ 'connection' => $db ] |
| 77 |
) ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static function newDisabled( array $params = [] ) { |
| 86 |
return new static( array_merge( |
| 87 |
$params, |
| 88 |
[ 'connection' => null ] |
| 89 |
) ); |
| 90 |
} |
| 91 |
|
| 92 |
public function newMainLB( $domain = false ): ILoadBalancerForOwner { |
| 93 |
|
| 94 |
throw new BadMethodCallException( "Method is not supported." ); |
| 95 |
} |
| 96 |
|
| 97 |
public function getMainLB( $domain = false ): ILoadBalancer { |
| 98 |
return $this->mainLB; |
| 99 |
} |
| 100 |
|
| 101 |
public function newExternalLB( $cluster ): ILoadBalancerForOwner { |
| 102 |
|
| 103 |
throw new BadMethodCallException( "Method is not supported." ); |
| 104 |
} |
| 105 |
|
| 106 |
public function getExternalLB( $cluster ): ILoadBalancer { |
| 107 |
|
| 108 |
throw new BadMethodCallException( "Method is not supported." ); |
| 109 |
} |
| 110 |
|
| 111 |
public function getAllMainLBs(): array { |
| 112 |
return [ self::CLUSTER_MAIN_DEFAULT => $this->mainLB ]; |
| 113 |
} |
| 114 |
|
| 115 |
public function getAllExternalLBs(): array { |
| 116 |
return []; |
| 117 |
} |
| 118 |
|
| 119 |
protected function getLBsForOwner() { |
| 120 |
if ( $this->mainLB !== null ) { |
| 121 |
yield $this->mainLB; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function __destruct() { |
| 126 |
|
| 127 |
} |
| 128 |
} |