3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 namespace Wikimedia\Rdbms
;
22 use BadMethodCallException
;
23 use InvalidArgumentException
;
26 * LoadBalancer manager for sites with one "main" cluster using only injected database connections
28 * This class assumes that there are no "external" clusters.
30 * LoadBalancerDisabled will be used if a null connection handle is injected.
35 class LBFactorySingle
extends LBFactory
{
36 /** @var LoadBalancerSingle|LoadBalancerDisabled */
40 * @note Use of {@link newFromConnection} is preferable
42 * @param array $conf An associative array containing one of the following:
43 * - connection: The IDatabase connection handle to use; null to disable access
45 public function __construct( array $conf ) {
46 parent
::__construct( $conf );
48 if ( !array_key_exists( 'connection', $conf ) ) {
49 throw new InvalidArgumentException( "Missing 'connection' argument." );
52 $conn = $conf['connection'];
54 $mainLB = new LoadBalancerSingle( array_merge(
55 $this->baseLoadBalancerParams(),
56 [ 'connection' => $conn ]
59 $mainLB = new LoadBalancerDisabled( $this->baseLoadBalancerParams() );
61 $this->initLoadBalancer( $mainLB );
63 $this->mainLB
= $mainLB;
67 * @param IDatabase $db Live connection handle
68 * @param array $params Parameter map to LBFactorySingle::__construct()
69 * @return LBFactorySingle
72 public static function newFromConnection( IDatabase
$db, array $params = [] ) {
73 return new static( array_merge(
74 [ 'localDomain' => $db->getDomainID() ],
76 [ 'connection' => $db ]
81 * @param array $params Parameter map to LBFactorySingle::__construct()
82 * @return LBFactorySingle
85 public static function newDisabled( array $params = [] ) {
86 return new static( array_merge(
88 [ 'connection' => null ]
92 public function newMainLB( $domain = false ): ILoadBalancerForOwner
{
93 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
94 throw new BadMethodCallException( "Method is not supported." );
97 public function getMainLB( $domain = false ): ILoadBalancer
{
101 public function newExternalLB( $cluster ): ILoadBalancerForOwner
{
102 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
103 throw new BadMethodCallException( "Method is not supported." );
106 public function getExternalLB( $cluster ): ILoadBalancer
{
107 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
108 throw new BadMethodCallException( "Method is not supported." );
111 public function getAllMainLBs(): array {
112 return [ self
::CLUSTER_MAIN_DEFAULT
=> $this->mainLB
];
115 public function getAllExternalLBs(): array {
119 protected function getLBsForOwner() {
120 if ( $this->mainLB
!== null ) {
125 public function __destruct() {
126 // do nothing since the connection was injected