Passed
Pull Request — master (#19603)
by
unknown
08:10
created

MysqlMutex   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 67
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 3
A hashLockName() 0 2 1
A releaseLock() 0 8 1
A acquireLock() 0 8 1
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\mutex;
9
10
use yii\base\InvalidConfigException;
11
use yii\db\Expression;
12
13
/**
14
 * MysqlMutex implements mutex "lock" mechanism via MySQL locks.
15
 *
16
 * Application configuration example:
17
 *
18
 * ```
19
 * [
20
 *     'components' => [
21
 *         'db' => [
22
 *             'class' => 'yii\db\Connection',
23
 *             'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
24
 *         ]
25
 *         'mutex' => [
26
 *             'class' => 'yii\mutex\MysqlMutex',
27
 *         ],
28
 *     ],
29
 * ]
30
 * ```
31
 *
32
 * @see Mutex
33
 *
34
 * @author resurtm <[email protected]>
35
 * @since 2.0
36
 */
37
class MysqlMutex extends DbMutex
38
{
39
    /**
40
     * @var Expression|string|null prefix value. If null (by default) then connection's current database name is used.
41
     */
42
    public $keyPrefix = null;
43
44
    /**
45
     * Initializes MySQL specific mutex component implementation.
46
     * @throws InvalidConfigException if [[db]] is not MySQL connection.
47
     */
48 19
    public function init()
49
    {
50 19
        parent::init();
51 19
        if ($this->db->driverName !== 'mysql') {
52
            throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
53
        }
54 19
        if ($this->keyPrefix === null) {
55 13
            $this->keyPrefix = new Expression('DATABASE()');
56
        }
57 19
    }
58
59
    /**
60
     * Acquires lock by given name.
61
     * @param string $name of the lock to be acquired.
62
     * @param int $timeout time (in seconds) to wait for lock to become released.
63
     * @return bool acquiring result.
64
     * @see https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock
65
     */
66 19
    protected function acquireLock($name, $timeout = 0)
67
    {
68
        return $this->db->useMaster(function ($db) use ($name, $timeout) {
69
            /** @var \yii\db\Connection $db */
70 19
            return (bool) $db->createCommand(
71 19
                'SELECT GET_LOCK(CONCAT(:prefix, :name), :timeout)',
72 19
                [':name' => $this->hashLockName($name), ':timeout' => $timeout, ':prefix' => $this->keyPrefix]
73 19
            )->queryScalar();
74 19
        });
75
    }
76
77
    /**
78
     * Releases lock by given name.
79
     * @param string $name of the lock to be released.
80
     * @return bool release result.
81
     * @see https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_release-lock
82
     */
83
    protected function releaseLock($name)
84
    {
85 19
        return $this->db->useMaster(function ($db) use ($name) {
86
            /** @var \yii\db\Connection $db */
87 19
            return (bool) $db->createCommand(
88 19
                'SELECT RELEASE_LOCK(CONCAT(:prefix, :name))',
89 19
                [':name' => $this->hashLockName($name), ':prefix' => $this->keyPrefix]
90 19
            )->queryScalar();
91 19
        });
92
    }
93
94
    /**
95
     * Generate hash for lock name to avoid exceeding lock name length limit.
96
     *
97
     * @param string $name
98
     * @return string
99
     * @since 2.0.16
100
     * @see https://github.com/yiisoft/yii2/pull/16836
101
     */
102 19
    protected function hashLockName($name) {
103 19
        return sha1($name);
104
    }
105
}
106