@@ -28,7 +28,7 @@ class DatabaseLock extends Lock
2828 /**
2929 * The prune probability odds.
3030 *
31- * @var array
31+ * @var array{int, int}|null
3232 */
3333 protected $ lottery ;
3434
@@ -47,7 +47,7 @@ class DatabaseLock extends Lock
4747 * @param string $name
4848 * @param int $seconds
4949 * @param string|null $owner
50- * @param array $lottery
50+ * @param array{int, int}|null $lottery
5151 * @param int $defaultTimeoutInSeconds
5252 */
5353 public function __construct (Connection $ connection , $ table , $ name , $ seconds , $ owner = null , $ lottery = [2 , 100 ], $ defaultTimeoutInSeconds = 86400 )
@@ -65,7 +65,7 @@ public function __construct(Connection $connection, $table, $name, $seconds, $ow
6565 *
6666 * @return bool
6767 *
68- * @throws Throwable
68+ * @throws \ Throwable
6969 */
7070 public function acquire ()
7171 {
@@ -90,16 +90,8 @@ public function acquire()
9090 $ acquired = $ updated >= 1 ;
9191 }
9292
93- if (random_int (1 , $ this ->lottery [1 ]) <= $ this ->lottery [0 ]) {
94- try {
95- $ this ->connection ->table ($ this ->table )
96- ->where ('expiration ' , '<= ' , $ this ->currentTime ())
97- ->delete ();
98- } catch (Throwable $ e ) {
99- if (! $ this ->causedByConcurrencyError ($ e )) {
100- throw $ e ;
101- }
102- }
93+ if (count ($ this ->lottery ?? []) === 2 && random_int (1 , $ this ->lottery [1 ]) <= $ this ->lottery [0 ]) {
94+ $ this ->pruneExpiredLocks ();
10395 }
10496
10597 return $ acquired ;
@@ -148,6 +140,26 @@ public function forceRelease()
148140 ->delete ();
149141 }
150142
143+ /**
144+ * Deletes locks that are past expiration.
145+ *
146+ * @return void
147+ *
148+ * @throws \Throwable
149+ */
150+ public function pruneExpiredLocks ()
151+ {
152+ try {
153+ $ this ->connection ->table ($ this ->table )
154+ ->where ('expiration ' , '<= ' , $ this ->currentTime ())
155+ ->delete ();
156+ } catch (Throwable $ e ) {
157+ if (! $ this ->causedByConcurrencyError ($ e )) {
158+ throw $ e ;
159+ }
160+ }
161+ }
162+
151163 /**
152164 * Returns the owner value written into the driver for this lock.
153165 *
0 commit comments