LoadBalancerSingle.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Simple generator of database connections that always returns the same object.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Database
  22. */
  23. namespace Wikimedia\Rdbms;
  24. use InvalidArgumentException;
  25. /**
  26. * Trivial LoadBalancer that always returns an injected connection handle.
  27. */
  28. class LoadBalancerSingle extends LoadBalancer {
  29. /** @var IDatabase */
  30. private $db;
  31. /**
  32. * @param array $params An associative array with one member:
  33. * - connection: An IDatabase connection object
  34. */
  35. public function __construct( array $params ) {
  36. if ( !isset( $params['connection'] ) ) {
  37. throw new InvalidArgumentException( "Missing 'connection' argument." );
  38. }
  39. $this->db = $params['connection'];
  40. parent::__construct( [
  41. 'servers' => [
  42. [
  43. 'type' => $this->db->getType(),
  44. 'host' => $this->db->getServer(),
  45. 'dbname' => $this->db->getDBname(),
  46. 'load' => 1,
  47. ]
  48. ],
  49. 'trxProfiler' => $params['trxProfiler'] ?? null,
  50. 'srvCache' => $params['srvCache'] ?? null,
  51. 'wanCache' => $params['wanCache'] ?? null,
  52. 'localDomain' => $params['localDomain'] ?? $this->db->getDomainID(),
  53. 'readOnlyReason' => $params['readOnlyReason'] ?? false,
  54. ] );
  55. if ( isset( $params['readOnlyReason'] ) ) {
  56. $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
  57. }
  58. }
  59. /**
  60. * @param IDatabase $db Live connection handle
  61. * @param array $params Parameter map to LoadBalancerSingle::__constructs()
  62. * @return LoadBalancerSingle
  63. * @since 1.28
  64. */
  65. public static function newFromConnection( IDatabase $db, array $params = [] ) {
  66. return new static( array_merge(
  67. [ 'localDomain' => $db->getDomainID() ],
  68. $params,
  69. [ 'connection' => $db ]
  70. ) );
  71. }
  72. protected function reallyOpenConnection( array $server, DatabaseDomain $domain ) {
  73. return $this->db;
  74. }
  75. public function __destruct() {
  76. // do nothing since the connection was injected
  77. }
  78. }
  79. /**
  80. * @deprecated since 1.29
  81. */
  82. class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );