Show / Hide Table of Contents

Class LeaseProvider<TMetadata>

Represents provider side of a lease in a distributed environment.

Inheritance
object
Disposable
LeaseProvider<TMetadata>
Implements
IDisposable
Inherited Members
Disposable.CreateException()
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.DisposeAsyncCore()
Disposable.DisposeAsync()
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(params IEnumerable<IAsyncDisposable>)
Disposable.Dispose<TDisposable>(params ReadOnlySpan<TDisposable>)
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.DisposedTask
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Threading.Leases
Assembly: DotNext.Threading.dll
Syntax
public abstract class LeaseProvider<TMetadata> : Disposable, IDisposable
Type Parameters
Name Description
TMetadata

The type of metadata associated with a lease.

Remarks

An instance of this type must support concurrent calls.

Each instance tracks the lease expiration using its own monotonic clock, starting from the moment it observes a particular version of the lease. Thus, the wall clocks of the instances sharing the same storage don't need to be synchronized. As a consequence, a newly created instance waits for the full TimeToLive before taking over the lease that was not released explicitly. Call ObserveAsync(CancellationToken) immediately after the instantiation to start tracking the expiration as early as possible.

The lease is held on behalf of the owner identified by Guid. The owner must be unique across all consumers of the lease. Acquisition, renewal and release by the owner don't require the actual version of the lease. Thus, these operations can be retried safely if the response is lost.

Constructors

View Source

LeaseProvider(TimeProvider?)

Initializes a new instance of lease provider.

Declaration
protected LeaseProvider(TimeProvider? provider = null)
Parameters
Type Name Description
TimeProvider provider

The time provider.

Properties

View Source

LifetimeToken

A token that represents state of this object.

Declaration
protected CancellationToken LifetimeToken { get; }
Property Value
Type Description
CancellationToken
Remarks

A call to Dispose(bool) cancels the token.

Methods

View Source

AcquireAsync(Guid, CancellationToken)

Acquires the lease.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>> AcquireAsync(Guid ownerId, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>>

The status of the operation.

Remarks

If the lease is already held by ownerId, it is renewed.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentException

ownerId is Empty.

View Source

AcquireAsync<TArg>(Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Acquires the lease.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>> AcquireAsync<TArg>(Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>>

The status of the operation.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

If the lease is already held by ownerId, it is renewed.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

ArgumentException

ownerId is Empty.

View Source

Dispose(bool)

Releases managed and unmanaged resources associated with this object.

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

true if called from Dispose(); false if called from finalizer ~Disposable().

Overrides
Disposable.Dispose(bool)
View Source

GetStateAsync(CancellationToken)

Loads the state of a lease from the underlying storage.

Declaration
protected abstract ValueTask<LeaseState<TMetadata>> GetStateAsync(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseState<TMetadata>>

The state restored from the underlying storage.

Remarks

The method can be called concurrently.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

ObserveAsync(CancellationToken)

Starts tracking the expiration of the lease.

Declaration
public ValueTask<bool> ObserveAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<bool>

true if the lease is alive from the perspective of this instance; false if it's expired, released, or never acquired. The first observation of the lease that was not released explicitly always returns true.

Remarks

The expiration of the lease is tracked from the moment when this instance observes the particular version of the lease for the first time. This method can be called immediately after the instantiation of the provider to reduce the time needed to take over the lease that was not released explicitly.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

View Source

ReleaseAsync(Guid, CancellationToken)

Releases the lease.

Declaration
public ValueTask<long?> ReleaseAsync(Guid ownerId, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long?>

The version of the released lease; or null if the lease is taken by another owner or already released.

Remarks

The expired lease can be released as well if it is not taken by another owner.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentException

ownerId is Empty.

View Source

ReleaseAsync<TArg>(Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Releases the lease.

Declaration
public ValueTask<long?> ReleaseAsync<TArg>(Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long?>

The version of the released lease; or null if the lease is taken by another owner or already released.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

The expired lease can be released as well if it is not taken by another owner.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

ArgumentException

ownerId is Empty.

View Source

TryAcquireAsync(Guid, CancellationToken)

Tries to acquire the lease.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryAcquireAsync(Guid ownerId, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>?>

The acquisition result; or null if the lease is taken by another owner.

Remarks

If the lease is already held by ownerId, it is renewed.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentException

ownerId is Empty.

View Source

TryAcquireAsync<TArg>(Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Tries to acquire the lease.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryAcquireAsync<TArg>(Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>?>

The acquisition result; or null if the lease is taken by another owner.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

If the lease is already held by ownerId, it is renewed.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

ArgumentException

ownerId is Empty.

View Source

TryRenewAsync(Guid, bool, CancellationToken)

Tries to renew the lease.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryRenewAsync(Guid ownerId, bool reacquire, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

bool reacquire

true to acquire the lease on renewal if it is expired.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>?>

The status of the operation; or null if the lease is taken by another owner, released, or expired.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentException

ownerId is Empty.

View Source

TryRenewAsync<TArg>(Guid, bool, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Tries to renew the lease and update its metadata.

Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryRenewAsync<TArg>(Guid ownerId, bool reacquire, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
Guid ownerId

The unique identifier of the lease owner.

bool reacquire

true to acquire the lease on renewal if it is expired.

TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<LeaseAcquisitionResult<TMetadata>?>

The status of the operation; or null if the lease is taken by another owner, released, or expired.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

Unlike the renewal without the metadata updater, this method always increments Version, even if the underlying storage supports RenewalStamp, because the metadata update is a durable change of the state. As a result, the owner gets a new fencing token.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

ArgumentException

ownerId is Empty.

View Source

TryUpdateStateAsync(LeaseState<TMetadata>, LeaseState<TMetadata>, CancellationToken)

Attempts to update the state in the underlying storage using compare-and-set semantics.

Declaration
protected abstract ValueTask<bool> TryUpdateStateAsync(LeaseState<TMetadata> expected, LeaseState<TMetadata> update, CancellationToken token)
Parameters
Type Name Description
LeaseState<TMetadata> expected

The state previously returned by GetStateAsync(CancellationToken).

LeaseState<TMetadata> update

The state to be stored in the underlying storage.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<bool>

true if update is performed successfully; false is compare-and-set failed.

Remarks

The operation must use compare-and-set semantics in the following way: save update only if Version and RenewalStamp of the currently stored state are equal to the corresponding properties of expected. Other properties must not be compared. If the lease doesn't exist in the storage, save update only if the lease is still absent. Note that the method can be called concurrently.

View Source

UnsafeReleaseAsync(CancellationToken)

Releases the lease ungracefully.

Declaration
public ValueTask<long> UnsafeReleaseAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long>

The version of the released lease.

Remarks

It's possible to call AcquireAsync(Guid, CancellationToken) method after the current one and get a new lease while the existing owner thinks that the lease is exclusively owned.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

View Source

UnsafeReleaseAsync<TArg>(TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Releases the lease ungracefully.

Declaration
public ValueTask<long> UnsafeReleaseAsync<TArg>(TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long>

The version of the released lease.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

It's possible to call AcquireAsync(Guid, CancellationToken) method after the current one and get a new lease while the existing owner thinks that the lease is exclusively owned.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

View Source

UnsafeTryReleaseAsync(CancellationToken)

Tries to release the lease ungracefully.

Declaration
public ValueTask<long?> UnsafeTryReleaseAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long?>

The version of the released lease; or null if updated or taken by another process.

Remarks

It's possible to call AcquireAsync(Guid, CancellationToken) method after the current one and get a new lease while the existing owner thinks that the lease is exclusively owned.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

View Source

UnsafeTryReleaseAsync<TArg>(TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)

Tries to release the lease ungracefully.

Declaration
public ValueTask<long?> UnsafeTryReleaseAsync<TArg>(TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
Type Name Description
TArg arg

The argument to be passed to the metadata updater.

Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater

An idempotent operation to update the metadata on successful acquisition of the lease.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<long?>

The version of the released lease; or null if updated or taken by another process.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the metadata updater.

Remarks

It's possible to call AcquireAsync(Guid, CancellationToken) method after the current one and get a new lease while the existing owner thinks that the lease is exclusively owned.

Exceptions
Type Condition
ObjectDisposedException

The provider has been disposed.

OperationCanceledException

The operation has been canceled.

ArgumentNullException

updater is null.

Implements

IDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)

See Also

LeaseConsumer
LeaseProvider<TKey, TMetadata>
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾