Class LeaseProvider<TKey, TMetadata>
Represents provider side of multiple leases in a distributed environment.
Implements
Inherited Members
Namespace: DotNext.Threading.Leases
Assembly: DotNext.Threading.dll
Syntax
public abstract class LeaseProvider<TKey, TMetadata> : Disposable, IDisposable where TKey : notnull
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of the lease identifier. |
| TMetadata | The type of metadata associated with a lease. |
Remarks
An instance of this type must support concurrent calls. Each lease is identified by the key and tracked independently in the same way as by LeaseProvider<TMetadata>.
Each instance tracks the expiration of every lease 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(TKey, CancellationToken) immediately after the instantiation to start tracking the expiration as early as possible.
The provider keeps the tracking information for every lease it has observed, until it observes the lease released.
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 SourceLeaseProvider(IEqualityComparer<TKey>?, TimeProvider?)
Initializes a new instance of lease provider.
Declaration
protected LeaseProvider(IEqualityComparer<TKey>? comparer = null, TimeProvider? provider = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IEqualityComparer<TKey> | comparer | The comparer of the lease identifiers. |
| TimeProvider | provider | The time provider. |
Properties
View SourceLifetimeToken
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 SourceAcquireAsync(TKey, Guid, CancellationToken)
Acquires the lease.
Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>> AcquireAsync(TKey leaseId, Guid ownerId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
| ArgumentNullException |
|
| ArgumentException |
|
AcquireAsync<TArg>(TKey, Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)
Acquires the lease.
Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>> AcquireAsync<TArg>(TKey leaseId, Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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 |
|
| ArgumentException |
|
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
View SourceForget(TKey)
Drops the tracking information of the lease.
Declaration
protected bool Forget(TKey leaseId)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
Returns
| Type | Description |
|---|---|
| bool | true if the tracking information has been dropped; false if the lease was not tracked. |
Remarks
The provider keeps the tracking information for every lease it has observed, until it observes the lease released. This method allows dropping it without the observation, e.g., when the lease is deleted from the underlying storage. If the lease is observed after this call, the expiration of its current version is tracked from that moment.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GetStateAsync(TKey, CancellationToken)
Loads the state of a lease from the underlying storage.
Declaration
protected abstract ValueTask<LeaseState<TMetadata>> GetStateAsync(TKey leaseId, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
ObserveAsync(TKey, CancellationToken)
Starts tracking the expiration of the lease.
Declaration
public ValueTask<bool> ObserveAsync(TKey leaseId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
| ArgumentNullException |
|
ReleaseAsync(TKey, Guid, CancellationToken)
Releases the lease.
Declaration
public ValueTask<long?> ReleaseAsync(TKey leaseId, Guid ownerId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
| ArgumentNullException |
|
| ArgumentException |
|
ReleaseAsync<TArg>(TKey, Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)
Releases the lease.
Declaration
public ValueTask<long?> ReleaseAsync<TArg>(TKey leaseId, Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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 |
|
| ArgumentException |
|
TryAcquireAsync(TKey, Guid, CancellationToken)
Tries to acquire the lease.
Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryAcquireAsync(TKey leaseId, Guid ownerId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
| ArgumentNullException |
|
| ArgumentException |
|
TryAcquireAsync<TArg>(TKey, Guid, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)
Tries to acquire the lease.
Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryAcquireAsync<TArg>(TKey leaseId, Guid ownerId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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 |
|
| ArgumentException |
|
TryRenewAsync(TKey, Guid, bool, CancellationToken)
Tries to renew the lease.
Declaration
public ValueTask<LeaseAcquisitionResult<TMetadata>?> TryRenewAsync(TKey leaseId, Guid ownerId, bool reacquire, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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. |
| ArgumentNullException |
|
| ArgumentException |
|
TryRenewAsync<TArg>(TKey, 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>(TKey leaseId, Guid ownerId, bool reacquire, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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 |
|
| ArgumentException |
|
TryUpdateStateAsync(TKey, 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(TKey leaseId, LeaseState<TMetadata> expected, LeaseState<TMetadata> update, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| LeaseState<TMetadata> | expected | The state previously returned by GetStateAsync(TKey, 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.
UnsafeReleaseAsync(TKey, CancellationToken)
Releases the lease ungracefully.
Declaration
public ValueTask<long> UnsafeReleaseAsync(TKey leaseId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier 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. |
Remarks
It's possible to call AcquireAsync(TKey, 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 |
|
UnsafeReleaseAsync<TArg>(TKey, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)
Releases the lease ungracefully.
Declaration
public ValueTask<long> UnsafeReleaseAsync<TArg>(TKey leaseId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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(TKey, 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 |
|
UnsafeTryReleaseAsync(TKey, CancellationToken)
Tries to release the lease ungracefully.
Declaration
public ValueTask<long?> UnsafeTryReleaseAsync(TKey leaseId, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier 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. |
Remarks
It's possible to call AcquireAsync(TKey, 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 |
|
UnsafeTryReleaseAsync<TArg>(TKey, TArg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>>, CancellationToken)
Tries to release the lease ungracefully.
Declaration
public ValueTask<long?> UnsafeTryReleaseAsync<TArg>(TKey leaseId, TArg arg, Func<TArg, TMetadata, CancellationToken, ValueTask<TMetadata>> updater, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TKey | leaseId | The identifier of the lease. |
| 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(TKey, 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 |
|