craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

[4.x]: Mutex configuration for redis doesn't respect namePrefix #12181

Closed jishi closed 2 years ago

jishi commented 2 years ago

What happened?

Description

I have configured redis as mutex store and verified that I indeed get mutex keys momentarily. However, I re-use my redis cluster for multiple installs, and want to separate installs via database, and each database should use prefixes for keys to separate different type of data (sessions and mutex locks)

Steps to reproduce

  1. configure mutex via redis using:
'mutex' => [
            'mutex' => 'yii\redis\Mutex',
            'namePrefix' => 'mutex_',
        ],

Expected behavior

Keys for mutex locks should be prefixed

Actual behavior

Keys are not prefixed, they only consists of some guid/uuid

Craft CMS version

4.2.8

PHP version

8.0.22

Operating system and version

Linux 5.10.124-linuxkit

Database type and version

Redis (latest ElastiCache in AWS)

Image driver and version

No response

Installed plugins and versions

brandonkelly commented 2 years ago

Your namePrefix is getting included when defining mutex keys, but yii\redis\Mutex is running the whole thing through md5(), so it’s getting obfuscated. Two separate installs should still be getting two separate keys though, so long as each has a unique namePrefix.

Alternatively, yii\redis\Mutex has its own keyPrefix property, which will get included before the md5()-d key, so if you use that instead, it will be much more obvious that each install is getting its own set of mutex locks.

'mutex' => [
    'mutex' => [
        'class' => 'yii\redis\Mutex',
        'keyPrefix' => 'mutex_',
    ],
],