Skip to content

Commit cda65aa

Browse files
author
Jonathan Gaillard
committed
Remove on readUnlock if the caller is the last reader.
This is slower as its another internal mongo write lock is taken on the update, but given most apps read more than write, we don't want to the lock documents to build.
1 parent a127b01 commit cda65aa

File tree

2 files changed

+2
-11
lines changed

2 files changed

+2
-11
lines changed

src/Locker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static function readLock(
8787
public static function readUnlock(\MongoCollection $collection, $id, \MongoId $readerId)
8888
{
8989
$collection->update(['_id' => $id], ['$pull' => ['readers' => ['id' => $readerId]]]);
90+
$collection->remove(['_id' => $id, 'writing' => false, 'readers' => ['$size' => 0]]);
9091
}
9192

9293
/**

tests/LockerTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,7 @@ public function readUnlockEmptyCollection()
209209
$readerId = Locker::readLock($collection, 'theId', new \MongoDate(time() + 1000));
210210
Locker::readUnlock($collection, 'theId', $readerId);
211211

212-
$this->assertSame(1, $collection->count());
213-
$expected = [
214-
'_id' => 'theId',
215-
'readers' => [],
216-
'writePending' => false,
217-
'writeStaleTs' => null,
218-
'writing' => false,
219-
];
220-
$actual = $collection->findOne();
221-
ksort($actual);
222-
$this->assertSame($expected, $actual);
212+
$this->assertSame(0, $collection->count());
223213
}
224214

225215
/**

0 commit comments

Comments
 (0)