Skip to content

Commit

Permalink
fixed lease comparison on time limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmd committed Dec 10, 2024
1 parent 1296311 commit fe63f01
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/DotJEM.Json.Index2/IO/IndexWriterSafeProxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics;
using Lucene.Net.Analysis;
using Lucene.Net.Index;
using Lucene.Net.Search;
Expand Down
12 changes: 7 additions & 5 deletions src/DotJEM.Json.Index2/Leases/LeaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ private class TimeLimitedLease<T> : Disposable, IRecallableLease<T>

private readonly T value;
private readonly Action<TimeLimitedLease<T>> onReturned;
private readonly TimeSpan timeLimit;
private readonly long timeLimitMilliseconds;
private readonly long leaseTime = Stopwatch.GetTimestamp();
private readonly AutoResetEvent handle = new(false);

public bool IsExpired => (ElapsedMs > timeLimit.Milliseconds) || IsDisposed;
public bool IsExpired => (ElapsedMs > timeLimitMilliseconds) || IsDisposed;
public bool IsTerminated { get; private set; }
private long ElapsedMs => (long)((Stopwatch.GetTimestamp() - leaseTime) / (Stopwatch.Frequency / (double)1000));

Expand All @@ -147,9 +147,11 @@ public T Value
{
throw new LeaseDisposedException($"This lease has been disposed.");
}
if (IsExpired)
long elapsed = ElapsedMs;
if (ElapsedMs > timeLimitMilliseconds)
{
throw new LeaseExpiredException($"Lease is expired either because the time limit '{timeLimit}' has exceeded or the lease was returned.");
throw new LeaseExpiredException($"Lease is expired either because the time limit '{timeLimitMilliseconds}'" +
$" was exceeded by: '{elapsed - timeLimitMilliseconds}'.");
}
return value;
}
Expand All @@ -159,7 +161,7 @@ public TimeLimitedLease(T value, Action<TimeLimitedLease<T>> onReturned, TimeSpa
{
this.value = value;
this.onReturned = onReturned;
this.timeLimit = timeLimit;
this.timeLimitMilliseconds = (long)timeLimit.TotalMilliseconds;
}

public bool TryRenew()
Expand Down

0 comments on commit fe63f01

Please sign in to comment.