PgsqlMutex.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  37. * Initializes PgSQL specific mutex component implementation.
  38. * @throws InvalidConfigException if [[db]] is not PgSQL connection.
  39. */
  40. public function init()
  41. {
  42. parent::init();
  43. if ($this->db->driverName !== 'pgsql') {
  44. throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.');
  45. }
  46. }
  47. /**
  48. * Converts a string into two 16 bit integer keys using the SHA1 hash function.
  49. * @param string $name
  50. * @return array contains two 16 bit integer keys
  51. */
  52. private function getKeysFromName($name)
  53. {
  54. return array_values(unpack('n2', sha1($name, true)));
  55. }
  56. /**
  57. * Acquires lock by given name.
  58. * @param string $name of the lock to be acquired.
  59. * @param int $timeout time (in seconds) to wait for lock to become released.
  60. * @return bool acquiring result.
  61. * @see http://www.postgresql.org/docs/9.0/static/functions-admin.html
  62. */
  63. protected function acquireLock($name, $timeout = 0)
  64. {
  65. if ($timeout !== 0) {
  66. throw new InvalidArgumentException('PgsqlMutex does not support timeout.');
  67. }
  68. list($key1, $key2) = $this->getKeysFromName($name);
  69. return $this->db->useMaster(function ($db) use ($key1, $key2) {
  70. /** @var \yii\db\Connection $db */
  71. return (bool) $db->createCommand(
  72. 'SELECT pg_try_advisory_lock(:key1, :key2)',
  73. [':key1' => $key1, ':key2' => $key2]
  74. )->queryScalar();
  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. }