mediawiki.api: Adopt async-await and assert.rejects() in various tests
[mediawiki.git] / includes / libs / rdbms / lbfactory / LBFactorySingle.php
blob7642cb1df6a94d43c67720f92c90491fd9444394
1 <?php
2 /**
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
18 * @file
20 namespace Wikimedia\Rdbms;
22 use BadMethodCallException;
23 use InvalidArgumentException;
25 /**
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.
32 * @see ILBFactory
33 * @ingroup Database
35 class LBFactorySingle extends LBFactory {
36 /** @var LoadBalancerSingle|LoadBalancerDisabled */
37 private $mainLB;
39 /**
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'];
53 if ( $conn ) {
54 $mainLB = new LoadBalancerSingle( array_merge(
55 $this->baseLoadBalancerParams(),
56 [ 'connection' => $conn ]
57 ) );
58 } else {
59 $mainLB = new LoadBalancerDisabled( $this->baseLoadBalancerParams() );
61 $this->initLoadBalancer( $mainLB );
63 $this->mainLB = $mainLB;
66 /**
67 * @param IDatabase $db Live connection handle
68 * @param array $params Parameter map to LBFactorySingle::__construct()
69 * @return LBFactorySingle
70 * @since 1.28
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 ) );
80 /**
81 * @param array $params Parameter map to LBFactorySingle::__construct()
82 * @return LBFactorySingle
83 * @since 1.40
85 public static function newDisabled( array $params = [] ) {
86 return new static( array_merge(
87 $params,
88 [ 'connection' => null ]
89 ) );
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 {
98 return $this->mainLB;
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 {
116 return [];
119 protected function getLBsForOwner() {
120 if ( $this->mainLB !== null ) {
121 yield $this->mainLB;
125 public function __destruct() {
126 // do nothing since the connection was injected