MediaWiki  master
LoadBalancerSingle.php
Go to the documentation of this file.
1 <?php
20 namespace Wikimedia\Rdbms;
21 
22 use InvalidArgumentException;
23 
31  private $conn;
32 
39  public function __construct( array $params ) {
41  $conn = $params['connection'] ?? null;
42  if ( !$conn ) {
43  throw new InvalidArgumentException( "Missing 'connection' argument." );
44  }
45 
46  $this->conn = $conn;
47 
48  parent::__construct( [
49  'servers' => [ [
50  'type' => $conn->getType(),
51  'host' => $conn->getServer(),
52  'dbname' => $conn->getDBname(),
53  'load' => 1,
54  ] ],
55  'trxProfiler' => $params['trxProfiler'] ?? null,
56  'srvCache' => $params['srvCache'] ?? null,
57  'wanCache' => $params['wanCache'] ?? null,
58  'localDomain' => $params['localDomain'] ?? $this->conn->getDomainID(),
59  'readOnlyReason' => $params['readOnlyReason'] ?? false,
60  'clusterName' => $params['clusterName'] ?? null,
61  ] );
62 
63  if ( isset( $params['readOnlyReason'] ) ) {
64  $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] );
65  }
66  }
67 
74  public static function newFromConnection( IDatabase $db, array $params = [] ) {
75  return new static( array_merge(
76  [ 'localDomain' => $db->getDomainID() ],
77  $params,
78  [ 'connection' => $db ]
79  ) );
80  }
81 
82  protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) {
83  foreach ( $lbInfo as $k => $v ) {
84  $this->conn->setLBInfo( $k, $v );
85  }
86 
87  return $this->conn;
88  }
89 
90  public function reuseConnection( IDatabase $conn ) {
91  // do nothing since the connection was injected
92  }
93 
94  public function __destruct() {
95  // do nothing since the connection was injected
96  }
97 }
98 
102 class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );
Class to handle database/schema/prefix specifications for IDatabase.
setLBInfo( $nameOrArray, $value=null)
Set the entire array or a particular key of the managing load balancer info array.
Definition: Database.php:422
getServer()
Get the hostname or IP address of the server.
Definition: Database.php:1614
getDBname()
Get the current database name; null if there isn't one.
Definition: Database.php:1610
Trivial LoadBalancer that always returns an injected connection handle.
reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo)
Open a new network connection to a server (uncached)
static newFromConnection(IDatabase $db, array $params=[])
__construct(array $params)
You probably want to use newFromConnection instead.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
getType()
Get the RDBMS type of the server (e.g.
getDomainID()
Return the currently selected domain ID.