Skip to content

Commit

Permalink
Added LeaseId property to expose fencing token
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jul 18, 2024
1 parent 0538715 commit b08d2a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/DotNext.Tests/Threading/Leases/LeaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public static async Task ConsumerTokenState()
True(consumer.Token.IsCancellationRequested);
True(consumer.Expiration.IsExpired);

True((await consumer.TryAcquireAsync()));
True(await consumer.TryAcquireAsync());
Equal(1UL, consumer.LeaseId.Version);
False(consumer.Token.IsCancellationRequested);
False(consumer.Expiration.IsExpired);

Expand Down
14 changes: 10 additions & 4 deletions src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ private TimeSpan AdjustTimeToLive(TimeSpan originalTtl)
/// Gets lease expiration timeout.
/// </summary>
public ref readonly Timeout Expiration => ref timeout;

/// <summary>
/// Gets the lease version.
/// </summary>
/// <remarks>The returned value can be used as a fencing token.</remarks>
public LeaseIdentity LeaseId => identity;

private ValueTask CancelAndStopTimerAsync()
{
Expand Down Expand Up @@ -181,7 +187,7 @@ public async Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TR
var leaseToken = Token;
var operationToken = token;
var cts = operationToken.LinkTo(leaseToken);

var task = Fork(worker, operationToken);
try
{
Expand Down Expand Up @@ -216,10 +222,10 @@ public async Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TR
{
cts?.Dispose();
}
}

private static Task<TResult> Fork<TResult>(Func<CancellationToken, Task<TResult>> function, CancellationToken token)
=> Task.Run(() => function(token), token);
static Task<TResult> Fork(Func<CancellationToken, Task<TResult>> function, CancellationToken token)
=> Task.Run(() => function(token), token);
}

/// <summary>
/// Performs a call to <see cref="LeaseProvider{TMetadata}.TryAcquireAsync(CancellationToken)"/> across the application boundaries.
Expand Down

0 comments on commit b08d2a7

Please sign in to comment.