Skip to content

Commit

Permalink
Avoid constantly attempting to acquire a lock (#491)
Browse files Browse the repository at this point in the history
- ignore expiration timeout on WaitForChangesAsync when there are queued writer locks
  • Loading branch information
ddaspit authored Sep 16, 2024
1 parent 8c5f802 commit 19f6f95
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public async Task<T> ReaderLockAsync<T>(
RWLock? rwLock = sub.Change.Entity;
if (rwLock is not null && !rwLock.IsAvailableForReading())
{
TimeSpan? timeout = default;
if (rwLock.WriterLock?.ExpiresAt is not null)
TimeSpan? timeout = null;
if (rwLock.WriterQueue.Count == 0 && rwLock.WriterLock?.ExpiresAt is not null)
{
timeout = rwLock.WriterLock.ExpiresAt - DateTime.UtcNow;
if (timeout < TimeSpan.Zero)
Expand Down Expand Up @@ -138,15 +138,18 @@ await _locks.UpdateAsync(
RWLock? rwLock = sub.Change.Entity;
if (rwLock is not null && !rwLock.IsAvailableForWriting(lockId))
{
var dateTimes = rwLock.ReaderLocks.Select(l => l.ExpiresAt).ToList();
if (rwLock.WriterLock?.ExpiresAt is not null)
dateTimes.Add(rwLock.WriterLock.ExpiresAt);
TimeSpan? timeout = default;
if (dateTimes.Count > 0)
TimeSpan? timeout = null;
if (rwLock.WriterQueue[0].Id == lockId)
{
timeout = dateTimes.Max() - DateTime.UtcNow;
if (timeout < TimeSpan.Zero)
timeout = TimeSpan.Zero;
var dateTimes = rwLock.ReaderLocks.Select(l => l.ExpiresAt).ToList();
if (rwLock.WriterLock?.ExpiresAt is not null)
dateTimes.Add(rwLock.WriterLock.ExpiresAt);
if (dateTimes.Count > 0)
{
timeout = dateTimes.Max() - DateTime.UtcNow;
if (timeout < TimeSpan.Zero)
timeout = TimeSpan.Zero;
}
}
if (timeout != TimeSpan.Zero)
await sub.WaitForChangeAsync(timeout, cancellationToken);
Expand Down

0 comments on commit 19f6f95

Please sign in to comment.