-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from php-lock/mysql-lock
Add locking backend for MySQL GET_LOCK() function
- Loading branch information
Showing
7 changed files
with
124 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace malkusch\lock\mutex; | ||
|
||
use malkusch\lock\exception\LockAcquireException; | ||
use malkusch\lock\exception\TimeoutException; | ||
|
||
class MySQLMutex extends LockMutex | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
private $pdo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
/** | ||
* @var int | ||
*/ | ||
private $timeout; | ||
|
||
public function __construct(\PDO $PDO, $name, $timeout = 0) | ||
{ | ||
$this->pdo = $PDO; | ||
|
||
if (\strlen($name) > 64) { | ||
throw new \InvalidArgumentException("The maximum length of the lock name is 64 characters."); | ||
} | ||
|
||
$this->name = $name; | ||
$this->timeout = $timeout; | ||
} | ||
|
||
/** | ||
* @throws LockAcquireException | ||
*/ | ||
public function lock() | ||
{ | ||
$statement = $this->pdo->prepare("SELECT GET_LOCK(?,?)"); | ||
|
||
$statement->execute([ | ||
$this->name, | ||
$this->timeout, | ||
]); | ||
|
||
$statement->setFetchMode(\PDO::FETCH_NUM); | ||
$row = $statement->fetch(); | ||
|
||
if ($row[0] == 1) { | ||
/* | ||
* Returns 1 if the lock was obtained successfully. | ||
*/ | ||
return; | ||
} | ||
|
||
if ($row[0] === null) { | ||
/* | ||
* NULL if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill). | ||
*/ | ||
throw new LockAcquireException("An error occurred while acquiring the lock"); | ||
} | ||
|
||
throw new TimeoutException("Timeout when acquiring lock."); | ||
} | ||
|
||
public function unlock() | ||
{ | ||
$statement = $this->pdo->prepare("DO RELEASE_LOCK(?)"); | ||
$statement->execute([ | ||
$this->name | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters