|
19 | 19 |
|
20 | 20 | final class PostgresAdvisoryLocker |
21 | 21 | { |
22 | | - public function tryAcquireLock( |
| 22 | + /** |
| 23 | + * Acquire an advisory lock with configurable scope, mode and behavior. |
| 24 | + */ |
| 25 | + public function acquireLock( |
23 | 26 | PDO $dbConnection, |
24 | 27 | PostgresLockId $postgresLockId, |
| 28 | + PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Transaction, |
| 29 | + PostgresAdvisoryLockTypeEnum $type = PostgresAdvisoryLockTypeEnum::NonBlocking, |
| 30 | + PostgresLockModeEnum $mode = PostgresLockModeEnum::Exclusive, |
25 | 31 | ): bool { |
26 | | - // TODO: Need to sanitize humanReadableValue? |
27 | | - $statement = $dbConnection->prepare( |
28 | | - <<<SQL |
29 | | - SELECT PG_TRY_ADVISORY_LOCK(:lock_id); -- $postgresLockId->humanReadableValue |
30 | | - SQL, |
31 | | - ); |
32 | | - $statement->execute( |
33 | | - [ |
34 | | - 'lock_id' => $postgresLockId->id, |
35 | | - ], |
36 | | - ); |
37 | | - |
38 | | - return $statement->fetchColumn(0); |
39 | | - } |
40 | | - |
41 | | - public function tryAcquireLockWithinTransaction( |
42 | | - PDO $dbConnection, |
43 | | - PostgresLockId $postgresLockId, |
44 | | - ): bool { |
45 | | - if ($dbConnection->inTransaction() === false) { |
46 | | - $lockId = $postgresLockId->humanReadableValue; |
47 | | - |
| 32 | + if ($scope === PostgresAdvisoryLockScopeEnum::Transaction && $dbConnection->inTransaction() === false) { |
48 | 33 | throw new LogicException( |
49 | | - "Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction", |
| 34 | + "Transaction-level advisory lock `$postgresLockId->humanReadableValue` cannot be acquired outside of transaction", |
50 | 35 | ); |
51 | 36 | } |
52 | 37 |
|
53 | | - // TODO: Need to sanitize humanReadableValue? |
54 | | - $statement = $dbConnection->prepare( |
55 | | - <<<SQL |
56 | | - SELECT PG_TRY_ADVISORY_XACT_LOCK(:lock_id); -- $postgresLockId->humanReadableValue |
57 | | - SQL, |
58 | | - ); |
| 38 | + $sql = match ([$scope, $type, $mode]) { |
| 39 | + [ |
| 40 | + PostgresAdvisoryLockScopeEnum::Transaction, |
| 41 | + PostgresAdvisoryLockTypeEnum::NonBlocking, |
| 42 | + PostgresLockModeEnum::Exclusive, |
| 43 | + ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK(:class_id, :object_id);', |
| 44 | + [ |
| 45 | + PostgresAdvisoryLockScopeEnum::Transaction, |
| 46 | + PostgresAdvisoryLockTypeEnum::Blocking, |
| 47 | + PostgresLockModeEnum::Exclusive, |
| 48 | + ] => 'SELECT PG_ADVISORY_XACT_LOCK(:class_id, :object_id);', |
| 49 | + [ |
| 50 | + PostgresAdvisoryLockScopeEnum::Transaction, |
| 51 | + PostgresAdvisoryLockTypeEnum::NonBlocking, |
| 52 | + PostgresLockModeEnum::Share, |
| 53 | + ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARE(:class_id, :object_id);', |
| 54 | + [ |
| 55 | + PostgresAdvisoryLockScopeEnum::Transaction, |
| 56 | + PostgresAdvisoryLockTypeEnum::Blocking, |
| 57 | + PostgresLockModeEnum::Share, |
| 58 | + ] => 'SELECT PG_ADVISORY_XACT_LOCK_SHARE(:class_id, :object_id);', |
| 59 | + [ |
| 60 | + PostgresAdvisoryLockScopeEnum::Session, |
| 61 | + PostgresAdvisoryLockTypeEnum::NonBlocking, |
| 62 | + PostgresLockModeEnum::Exclusive, |
| 63 | + ] => 'SELECT PG_TRY_ADVISORY_LOCK(:class_id, :object_id);', |
| 64 | + [ |
| 65 | + PostgresAdvisoryLockScopeEnum::Session, |
| 66 | + PostgresAdvisoryLockTypeEnum::Blocking, |
| 67 | + PostgresLockModeEnum::Exclusive, |
| 68 | + ] => 'SELECT PG_ADVISORY_LOCK(:class_id, :object_id);', |
| 69 | + [ |
| 70 | + PostgresAdvisoryLockScopeEnum::Session, |
| 71 | + PostgresAdvisoryLockTypeEnum::NonBlocking, |
| 72 | + PostgresLockModeEnum::Share, |
| 73 | + ] => 'SELECT PG_TRY_ADVISORY_LOCK_SHARE(:class_id, :object_id);', |
| 74 | + [ |
| 75 | + PostgresAdvisoryLockScopeEnum::Session, |
| 76 | + PostgresAdvisoryLockTypeEnum::Blocking, |
| 77 | + PostgresLockModeEnum::Share, |
| 78 | + ] => 'SELECT PG_ADVISORY_LOCK_SHARE(:class_id, :object_id);', |
| 79 | + }; |
| 80 | + $sql .= " -- $postgresLockId->humanReadableValue"; |
| 81 | + |
| 82 | + $statement = $dbConnection->prepare($sql); |
59 | 83 | $statement->execute( |
60 | 84 | [ |
61 | | - 'lock_id' => $postgresLockId->id, |
| 85 | + 'class_id' => $postgresLockId->classId, |
| 86 | + 'object_id' => $postgresLockId->objectId, |
62 | 87 | ], |
63 | 88 | ); |
64 | 89 |
|
65 | 90 | return $statement->fetchColumn(0); |
66 | 91 | } |
67 | 92 |
|
| 93 | + /** |
| 94 | + * Release session level advisory lock. |
| 95 | + */ |
68 | 96 | public function releaseLock( |
69 | 97 | PDO $dbConnection, |
70 | 98 | PostgresLockId $postgresLockId, |
| 99 | + PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Session, |
71 | 100 | ): bool { |
| 101 | + if ($scope === PostgresAdvisoryLockScopeEnum::Transaction) { |
| 102 | + throw new \InvalidArgumentException('Transaction-level advisory lock cannot be released'); |
| 103 | + } |
| 104 | + |
72 | 105 | $statement = $dbConnection->prepare( |
73 | 106 | <<<SQL |
74 | | - SELECT PG_ADVISORY_UNLOCK(:lock_id); -- $postgresLockId->humanReadableValue |
| 107 | + SELECT PG_ADVISORY_UNLOCK(:class_id, :object_id); -- $postgresLockId->humanReadableValue |
75 | 108 | SQL, |
76 | 109 | ); |
77 | 110 | $statement->execute( |
78 | 111 | [ |
79 | | - 'lock_id' => $postgresLockId->id, |
| 112 | + 'class_id' => $postgresLockId->classId, |
| 113 | + 'object_id' => $postgresLockId->objectId, |
80 | 114 | ], |
81 | 115 | ); |
82 | 116 |
|
83 | 117 | return $statement->fetchColumn(0); |
84 | 118 | } |
85 | 119 |
|
| 120 | + /** |
| 121 | + * Release all session level advisory locks held by the current session. |
| 122 | + */ |
86 | 123 | public function releaseAllLocks( |
87 | 124 | PDO $dbConnection, |
| 125 | + PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Session, |
88 | 126 | ): void { |
| 127 | + if ($scope === PostgresAdvisoryLockScopeEnum::Transaction) { |
| 128 | + throw new \InvalidArgumentException('Transaction-level advisory lock cannot be released'); |
| 129 | + } |
| 130 | + |
89 | 131 | $statement = $dbConnection->prepare( |
90 | 132 | <<<'SQL' |
91 | 133 | SELECT PG_ADVISORY_UNLOCK_ALL(); |
|
0 commit comments