|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Lock\Store; |
| 13 | + |
| 14 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
| 15 | +use Symfony\Component\Lock\Key; |
| 16 | +use Symfony\Component\Lock\StoreInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * MysqlStore is a StoreInterface implementation using MySQL/MariaDB GET_LOCK function |
| 20 | + * |
| 21 | + * @author Jérôme TAMARELLE <jerome@tamarelle.net> |
| 22 | + */ |
| 23 | +class MysqlStore implements StoreInterface |
| 24 | +{ |
| 25 | + private $dsn; |
| 26 | + private $username; |
| 27 | + private $password; |
| 28 | + private $options; |
| 29 | + |
| 30 | + private $waitTimeout; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param array $connection |
| 34 | + * - db_dsn: |
| 35 | + * - db_username: |
| 36 | + * - db_password: |
| 37 | + * - db_options: |
| 38 | + * - timeout: Time in seconds to wait for a lock to be released. |
| 39 | + * A negative timeout value means infinite timeout. |
| 40 | + */ |
| 41 | + public function __construct(array $options) |
| 42 | + { |
| 43 | + $this->dsn = $options['dsn'] ?? 'mysql:host=localhost'; |
| 44 | + $this->username = $options['db_username'] ?? 'root'; |
| 45 | + $this->password = $options['db_password'] ?? null; |
| 46 | + $this->options = $options['db_options'] ?? array(); |
| 47 | + $this->waitTimeout = $options['timeout'] ?? -1; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function save(Key $key) |
| 54 | + { |
| 55 | + $this->lock($key, false); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function waitAndSave(Key $key) |
| 62 | + { |
| 63 | + $this->lock($key, true); |
| 64 | + } |
| 65 | + |
| 66 | + private function lock(Key $key, bool $blocking) |
| 67 | + { |
| 68 | + // The lock is maybe already acquired. |
| 69 | + if ($key->hasState(__CLASS__)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // no timeout for impatient |
| 74 | + $timeout = $blocking ? $this->waitTimeout : 0; |
| 75 | + |
| 76 | + $connection = new \PDO($this->dsn, $this->username, $this->password, $this->options); |
| 77 | + $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 78 | + |
| 79 | + $stmt = $connection->prepare('SELECT GET_LOCK(:key, :timeout)'); |
| 80 | + $stmt->bindValue(':key', hash('sha256', $key), \PDO::PARAM_STR); |
| 81 | + $stmt->bindValue(':timeout', $timeout, \PDO::PARAM_INT); |
| 82 | + $stmt->setFetchMode(\PDO::FETCH_COLUMN, 0); |
| 83 | + $stmt->execute(); |
| 84 | + $success = $stmt->fetchColumn(); |
| 85 | + |
| 86 | + if ('0' === $success) { |
| 87 | + throw new LockConflictedException(); |
| 88 | + } |
| 89 | + |
| 90 | + // store the release statement in the state |
| 91 | + $releaseStmt = $connection->prepare('SELECT RELEASE_LOCK(:key)'); |
| 92 | + $releaseStmt->bindValue(':key', hash('sha256', $key), \PDO::PARAM_STR); |
| 93 | + |
| 94 | + $key->setState(__CLASS__, $releaseStmt); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritdoc} |
| 99 | + */ |
| 100 | + public function putOffExpiration(Key $key, $ttl) |
| 101 | + { |
| 102 | + // do nothing, the GET_LOCK locks forever, until the session terminates. |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + */ |
| 108 | + public function delete(Key $key) |
| 109 | + { |
| 110 | + if (!$key->hasState(__CLASS__)) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + $releaseStmt = $key->getState(__CLASS__); |
| 115 | + $releaseStmt->execute(); |
| 116 | + |
| 117 | + // Close the connection. |
| 118 | + $key->removeState(__CLASS__); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * {@inheritdoc} |
| 123 | + */ |
| 124 | + public function exists(Key $key) |
| 125 | + { |
| 126 | + return $key->hasState(__CLASS__); |
| 127 | + } |
| 128 | +} |
0 commit comments