MediaWiki  master
LoadBalancerSingle.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
26 use InvalidArgumentException;
27 
33  private $db;
34 
41  public function __construct( array $params ) {
43  $conn = $params['connection'] ?? null;
44  if ( !$conn ) {
45  throw new InvalidArgumentException( "Missing 'connection' argument." );
46  }
47 
48  $this->db = $conn;
49 
50  parent::__construct( [
51  'servers' => [ [
52  'type' => $conn->getType(),
53  'host' => $conn->getServer(),
54  'dbname' => $conn->getDBname(),
55  'load' => 1,
56  ] ],
57  'trxProfiler' => $params['trxProfiler'] ?? null,
58  'srvCache' => $params['srvCache'] ?? null,
59  'wanCache' => $params['wanCache'] ?? null,
60  'localDomain' => $params['localDomain'] ?? $this->db->getDomainID(),
61  'readOnlyReason' => $params['readOnlyReason'] ?? false,
62  'clusterName' => $params['clusterName'] ?? null,
63  ] );
64 
65  if ( isset( $params['readOnlyReason'] ) ) {
66  $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] );
67  }
68  }
69 
76  public static function newFromConnection( IDatabase $db, array $params = [] ) {
77  return new static( array_merge(
78  [ 'localDomain' => $db->getDomainID() ],
79  $params,
80  [ 'connection' => $db ]
81  ) );
82  }
83 
84  protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo = [] ) {
85  return $this->db;
86  }
87 
88  public function __destruct() {
89  // do nothing since the connection was injected
90  }
91 }
92 
96 class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );
Class to handle database/schema/prefix specifications for IDatabase.
Trivial LoadBalancer that always returns an injected connection handle.
static newFromConnection(IDatabase $db, array $params=[])
__construct(array $params)
You probably want to use newFromConnection instead.
reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo=[])
Open a new network connection to a server (uncached)
Database connection, tracking, load balancing, and transaction manager for a cluster.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39
getDomainID()
Return the currently selected domain ID.