PgsqlMutex.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mutex;
  8. use yii\base\InvalidArgumentException;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * PgsqlMutex implements mutex "lock" mechanism via PgSQL locks.
  12. *
  13. * Application configuration example:
  14. *
  15. * ```
  16. * [
  17. * 'components' => [
  18. * 'db' => [
  19. * 'class' => 'yii\db\Connection',
  20. * 'dsn' => 'pgsql:host=127.0.0.1;dbname=demo',
  21. * ]
  22. * 'mutex' => [
  23. * 'class' => 'yii\mutex\PgsqlMutex',
  24. * ],
  25. * ],
  26. * ]
  27. * ```
  28. *
  29. * @see Mutex
  30. *
  31. * @author nineinchnick <janek.jan@gmail.com>
  32. * @since 2.0.8
  33. */
  34. class PgsqlMutex extends DbMutex
  35. {
  36. use RetryAcquireTrait;
  37. /**
  38. * Initializes PgSQL specific mutex component implementation.
  39. * @throws InvalidConfigException if [[db]] is not PgSQL connection.
  40. */
  41. public function init()
  42. {
  43. parent::init();
  44. if ($this->db->driverName !== 'pgsql') {
  45. throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.');
  46. }
  47. }
  48. /**
  49. * Converts a string into two 16 bit integer keys using the SHA1 hash function.
  50. * @param string $name
  51. * @return array contains two 16 bit integer keys
  52. */
  53. private function getKeysFromName($name)
  54. {
  55. return array_values(unpack('n2', sha1($name, true)));
  56. }
  57. /**
  58. * Acquires lock by given name.
  59. * @param string $name of the lock to be acquired.
  60. * @param int $timeout time (in seconds) to wait for lock to become released.
  61. * @return bool acquiring result.
  62. * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
  63. */
  64. protected function acquireLock($name, $timeout = 0)
  65. {
  66. list($key1, $key2) = $this->getKeysFromName($name);
  67. return $this->retryAcquire($timeout, function () use ($key1, $key2) {
  68. return $this->db->useMaster(function ($db) use ($key1, $key2) {
  69. /** @var \yii\db\Connection $db */
  70. return (bool) $db->createCommand(
  71. 'SELECT pg_try_advisory_lock(:key1, :key2)',
  72. [':key1' => $key1, ':key2' => $key2]
  73. )->queryScalar();
  74. });
  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 http://www.postgresql.org/docs/9.0/static/functions-admin.html
  82. */
  83. protected function releaseLock($name)
  84. {
  85. list($key1, $key2) = $this->getKeysFromName($name);
  86. return $this->db->useMaster(function ($db) use ($key1, $key2) {
  87. /** @var \yii\db\Connection $db */
  88. return (bool) $db->createCommand(
  89. 'SELECT pg_advisory_unlock(:key1, :key2)',
  90. [':key1' => $key1, ':key2' => $key2]
  91. )->queryScalar();
  92. });
  93. }
  94. }