Skip to content

Commit

Permalink
Added TimeProvider supports
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jun 17, 2024
1 parent fa17e7f commit 393dde9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public async ValueTask<Timeout> TryAcquireAsync(CancellationToken token = defaul
{
ObjectDisposedException.ThrowIf(IsDisposingOrDisposed, this);

var ts = new Timestamp();
var ts = new Timestamp(provider);
if (await TryAcquireCoreAsync(token).ConfigureAwait(false) is { } response)
{
identity = response.Identity;
await CancelAndStopTimerAsync().ConfigureAwait(false);

var remainingTime = AdjustTimeToLive(response.TimeToLive) - ts.Elapsed;
var remainingTime = AdjustTimeToLive(response.TimeToLive) - ts.GetElapsedTime(provider);
if (remainingTime > TimeSpan.Zero)
{
timer = provider.CreateTimer(callback, source = new(), remainingTime, InfiniteTimeSpan);
Expand Down Expand Up @@ -146,14 +146,14 @@ public async ValueTask<Timeout> TryRenewAsync(CancellationToken token = default)
if (identity.Version is LeaseIdentity.InitialVersion)
throw new InvalidOperationException();

var ts = new Timestamp();
var ts = new Timestamp(provider);
if (await TryRenewCoreAsync(identity, token).ConfigureAwait(false) is { } response)
{
identity = response.Identity;

// ensure that the timer has been stopped
await timer.DisposeAsync().ConfigureAwait(false);
var remainingTime = AdjustTimeToLive(response.TimeToLive) - ts.Elapsed;
var remainingTime = AdjustTimeToLive(response.TimeToLive) - ts.GetElapsedTime(provider);

if (remainingTime > TimeSpan.Zero)
{
Expand Down
8 changes: 4 additions & 4 deletions src/DotNext.Threading/Threading/Leases/LeaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ protected LeaseProvider(TimeSpan ttl, TimeProvider? provider = null)
Metadata = await updater.Invoke(state.Metadata, token).ConfigureAwait(false),
};

var ts = new Timestamp();
var ts = new Timestamp(provider);
if (!await TryUpdateStateAsync(state, token).ConfigureAwait(false))
return null;

remainingTime = TimeToLive - ts.Elapsed;
remainingTime = TimeToLive - ts.GetElapsedTime(provider);
return remainingTime > TimeSpan.Zero
? new(in state, remainingTime)
: null;
Expand Down Expand Up @@ -132,11 +132,11 @@ private async ValueTask<AcquisitionResult> AcquireAsync<TUpdater>(TUpdater updat
Metadata = await updater.Invoke(state.Metadata, token).ConfigureAwait(false),
};

var ts = new Timestamp();
var ts = new Timestamp(provider);
if (!await TryUpdateStateAsync(state, token).ConfigureAwait(false))
continue;

remainingTime = TimeToLive - ts.Elapsed;
remainingTime = TimeToLive - ts.GetElapsedTime(provider);

if (remainingTime <= TimeSpan.Zero)
continue;
Expand Down

0 comments on commit 393dde9

Please sign in to comment.