forked from prooph/pdo-event-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prooph#189 Use locking strategy when appending to event-streams
- Loading branch information
1 parent
9f6b1cb
commit 92046a6
Showing
14 changed files
with
997 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of prooph/pdo-event-store. | ||
* (c) 2016-2019 prooph software GmbH <contact@prooph.de> | ||
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventStore\Pdo; | ||
|
||
interface WriteLockStrategy | ||
{ | ||
public function getLock(string $name): bool; | ||
|
||
public function releaseLock(string $name): bool; | ||
} |
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,71 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of prooph/pdo-event-store. | ||
* (c) 2016-2019 prooph software GmbH <contact@prooph.de> | ||
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventStore\Pdo\WriteLockStrategy; | ||
|
||
use Prooph\EventStore\Pdo\WriteLockStrategy; | ||
|
||
final class MariaDbMetadataLockStrategy implements WriteLockStrategy | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $timeout; | ||
|
||
public function __construct(\PDO $connection, int $timeout = 0xffffff) | ||
{ | ||
if ($timeout < 0) { | ||
throw new \InvalidArgumentException('$timeout must be larger 0.'); | ||
} | ||
|
||
$this->connection = $connection; | ||
$this->timeout = $timeout; | ||
} | ||
|
||
public function getLock(string $name): bool | ||
{ | ||
try { | ||
$res = $this->connection->query('SELECT GET_LOCK("' . $name . '", ' . $this->timeout . ') as `get_lock`'); | ||
} catch (\PDOException $e) { | ||
// ER_USER_LOCK_DEADLOCK: we only care for deadlock errors and fail locking | ||
if ('3058' === $this->connection->errorCode()) { | ||
return false; | ||
} | ||
|
||
throw $e; | ||
} | ||
|
||
if (!$res) { | ||
return false; | ||
} | ||
|
||
$lockStatus = $res->fetchAll(); | ||
if ('1' === $lockStatus[0]['get_lock']) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function releaseLock(string $name): bool | ||
{ | ||
$this->connection->exec('DO RELEASE_LOCK("' . $name . '") as `release_lock`'); | ||
|
||
return true; | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of prooph/pdo-event-store. | ||
* (c) 2016-2019 prooph software GmbH <contact@prooph.de> | ||
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\EventStore\Pdo\WriteLockStrategy; | ||
|
||
use Prooph\EventStore\Pdo\WriteLockStrategy; | ||
|
||
final class MysqlMetadataLockStrategy implements WriteLockStrategy | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $timeout; | ||
|
||
public function __construct(\PDO $connection, int $timeout = -1) | ||
{ | ||
$this->connection = $connection; | ||
$this->timeout = $timeout; | ||
} | ||
|
||
public function getLock(string $name): bool | ||
{ | ||
try { | ||
$res = $this->connection->query('SELECT GET_LOCK("' . $name . '", ' . $this->timeout . ') as `get_lock`'); | ||
} catch (\PDOException $e) { | ||
// ER_USER_LOCK_DEADLOCK: we only care for deadlock errors and fail locking | ||
if ('3058' === $this->connection->errorCode()) { | ||
return false; | ||
} | ||
|
||
throw $e; | ||
} | ||
|
||
if (!$res) { | ||
return false; | ||
} | ||
|
||
$lockStatus = $res->fetchAll(); | ||
if ('1' === $lockStatus[0]['get_lock']) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function releaseLock(string $name): bool | ||
{ | ||
$this->connection->exec('DO RELEASE_LOCK("' . $name . '") as `release_lock`'); | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.