Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/util/PhabricatorGlobalLock.php
| Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | public static function newLock($name) { | ||||
| if (!$lock) { | if (!$lock) { | ||||
| $lock = new PhabricatorGlobalLock($full_name); | $lock = new PhabricatorGlobalLock($full_name); | ||||
| self::registerLock($lock); | self::registerLock($lock); | ||||
| } | } | ||||
| return $lock; | return $lock; | ||||
| } | } | ||||
| /** | |||||
epriestley: This should just be `MySQLDatabaseConnection` (or `DatabaseConnection`? Or something? Go one… | |||||
| * Use a specific database connection for locking. | |||||
| * | |||||
| * By default, `PhabricatorGlobalLock` will lock on the "repository" database | |||||
| * (somewhat arbitrarily). In most cases this is fine, but this method can | |||||
| * be used to lock on a specific connection. | |||||
| * | |||||
| * @param AphrontDatabaseConnection | |||||
| * @return this | |||||
| */ | |||||
| public function useSpecificConnection(AphrontDatabaseConnection $conn) { | |||||
| $this->conn = $conn; | |||||
| return $this; | |||||
| } | |||||
| /* -( Implementation )----------------------------------------------------- */ | /* -( Implementation )----------------------------------------------------- */ | ||||
| protected function doLock($wait) { | protected function doLock($wait) { | ||||
| $conn = $this->conn; | $conn = $this->conn; | ||||
| if (!$conn) { | if (!$conn) { | ||||
| // Try to reuse a connection from the connection pool. | // Try to reuse a connection from the connection pool. | ||||
| $conn = array_pop(self::$pool); | $conn = array_pop(self::$pool); | ||||
| } | } | ||||
| if (!$conn) { | if (!$conn) { | ||||
| // NOTE: Using the 'repository' database somewhat arbitrarily, mostly | // NOTE: Using the 'repository' database somewhat arbitrarily, mostly | ||||
| // because the first client of locks is the repository daemons. We must | // because the first client of locks is the repository daemons. We must | ||||
| // always use the same database for all locks, but don't access any | // always use the same database for all locks, but don't access any | ||||
| // tables so we could use any valid database. We could build a | // tables so we could use any valid database. We could build a | ||||
| // database-free connection instead, but that's kind of messy and we | // database-free connection instead, but that's kind of messy and we | ||||
| // might forget about it in the future if we vertically partition the | // might forget about it in the future if we vertically partition the | ||||
| // application. | // application. | ||||
| $dao = new PhabricatorRepository(); | $dao = new PhabricatorRepository(); | ||||
| // NOTE: Using "force_new" to make sure each lock is on its own | // NOTE: Using "force_new" to make sure each lock is on its own | ||||
| // connection. | // connection. | ||||
| $conn = $dao->establishConnection('w', $force_new = true); | $conn = $dao->establishConnection('w', $force_new = true); | ||||
| } | |||||
| // NOTE: Since MySQL will disconnect us if we're idle for too long, we set | // NOTE: Since MySQL will disconnect us if we're idle for too long, we set | ||||
| // the wait_timeout to an enormous value, to allow us to hold the | // the wait_timeout to an enormous value, to allow us to hold the | ||||
| // connection open indefinitely (or, at least, for 24 days). | // connection open indefinitely (or, at least, for 24 days). | ||||
| $max_allowed_timeout = 2147483; | $max_allowed_timeout = 2147483; | ||||
| queryfx($conn, 'SET wait_timeout = %d', $max_allowed_timeout); | queryfx($conn, 'SET wait_timeout = %d', $max_allowed_timeout); | ||||
| } | |||||
| $result = queryfx_one( | $result = queryfx_one( | ||||
| $conn, | $conn, | ||||
| 'SELECT GET_LOCK(%s, %f)', | 'SELECT GET_LOCK(%s, %f)', | ||||
| $this->getName(), | $this->getName(), | ||||
| $wait); | $wait); | ||||
| $ok = head($result); | $ok = head($result); | ||||
| Show All 19 Lines | |||||
This should just be MySQLDatabaseConnection (or DatabaseConnection? Or something? Go one level up the class tree.), not MySQLi specifically.