Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jun 14, 2024
1 parent cd5b927 commit 38896f5
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions src/DotNext.Threading/Threading/Leases/LeaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,6 @@ protected LeaseProvider(TimeSpan ttl, TimeProvider? provider = null)
}
}

private async ValueTask<(LeaseIdentity Identity, TimeSpan RemainingTime, bool IsAcquired)> TryAcquireAsync<TUpdater>(TUpdater updater, CancellationToken token = default)
where TUpdater : notnull, ISupplier<TMetadata, CancellationToken, ValueTask<TMetadata>>
{
var cts = token.LinkTo(LifetimeToken);
try
{
var state = await GetStateAsync(token).ConfigureAwait(false);

if (!state.IsExpired(provider, TimeToLive, out var remainingTime))
return new(state.Identity, remainingTime, false);

state = state with
{
CreatedAt = provider.GetUtcNow(),
Identity = state.Identity.BumpVersion(),
Metadata = await updater.Invoke(state.Metadata, token).ConfigureAwait(false),
};

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

remainingTime = TimeToLive - ts.Elapsed;
return remainingTime > TimeSpan.Zero
? new(state.Identity, remainingTime, true)
: new(state.Identity, TimeSpan.Zero, false);
}
catch (OperationCanceledException e) when (e.CausedBy(cts, LifetimeToken))
{
throw new ObjectDisposedException(GetType().Name);
}
finally
{
cts?.Dispose();
}
}

/// <summary>
/// Tries to acquire the lease.
/// </summary>
Expand Down

0 comments on commit 38896f5

Please sign in to comment.