Aaron Schulz has uploaded a new change for review.
https://gerrit.wikimedia.org/r/80130
Change subject: Added a getLazyConnectionRef() function to load balancer
......................................................................
Added a getLazyConnectionRef() function to load balancer
* This is useful for injecting dependencies without adding pointless traffic
in some cases
Change-Id: I755ed008c387a3e4555934c84a5dc013ee8c2392
---
M includes/db/LoadBalancer.php
1 file changed, 34 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/30/80130/1
diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php
index 60c2833..3358488 100644
--- a/includes/db/LoadBalancer.php
+++ b/includes/db/LoadBalancer.php
@@ -562,6 +562,22 @@
}
/**
+ * Get a database connection handle reference without connecting yet
+ *
+ * The handle's methods wrap simply wrap those of a DatabaseBase handle
+ *
+ * @see LoadBalancer::getConnection() for parameter information
+ *
+ * @param integer $db
+ * @param mixed $groups
+ * @param string $wiki
+ * @return DBConnRef
+ */
+ public function getLazyConnectionRef( $db, $groups = array(), $wiki =
false ) {
+ return new DBConnRef( $this, array( $db, $groups, $wiki ) );
+ }
+
+ /**
* Open a connection to the server given by the specified index
* Index must be an actual index into the array.
* If the server is already open, returns it.
@@ -1088,6 +1104,7 @@
/**
* Helper class to handle automatically marking connectons as reusable (via
RAII pattern)
+ * as well handling deferring the actual network connection until the handle
is used
*
* @ingroup Database
* @since 1.22
@@ -1095,23 +1112,35 @@
class DBConnRef implements IDatabase {
/** @var LoadBalancer */
protected $lb;
- /** @var DatabaseBase */
+ /** @var DatabaseBase|null */
protected $conn;
+ /** @var Array|null */
+ protected $params;
/**
* @param $lb LoadBalancer
- * @param $conn DatabaseBase
+ * @param $conn DatabaseBase|array Connection or (server index, group,
wiki ID) array
*/
- public function __construct( LoadBalancer $lb, DatabaseBase $conn ) {
+ public function __construct( LoadBalancer $lb, $conn ) {
$this->lb = $lb;
- $this->conn = $conn;
+ if ( $conn instanceof DatabaseBase ) {
+ $this->conn = $conn;
+ } else {
+ $this->params = $conn;
+ }
}
public function __call( $name, $arguments ) {
+ if ( $this->conn === null ) {
+ list( $db, $groups, $wiki ) = $this->params;
+ $this->conn = $this->lb->getConnection( $db, $groups,
$wiki );
+ }
return call_user_func_array( array( $this->conn, $name ),
$arguments );
}
function __destruct() {
- $this->lb->reuseConnection( $this->conn );
+ if ( $this->conn !== null ) {
+ $this->lb->reuseConnection( $this->conn );
+ }
}
}
--
To view, visit https://gerrit.wikimedia.org/r/80130
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I755ed008c387a3e4555934c84a5dc013ee8c2392
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits