Class LeaseConsumer
Represents client side of a lease in a distributed environment.
Inherited Members
Namespace: DotNext.Threading.Leases
Assembly: DotNext.Threading.dll
Syntax
public abstract class LeaseConsumer : Disposable, IDisposable, IAsyncDisposable
Constructors
| Edit this page View SourceLeaseConsumer(TimeProvider?)
Initializes a new lease consumer.
Declaration
protected LeaseConsumer(TimeProvider? provider = null)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeProvider | provider | The timer factory. |
Properties
| Edit this page View SourceClockDriftBound
Gets or sets wall clock desync degree in the cluster.
Declaration
public double ClockDriftBound { get; init; }
Property Value
| Type | Description |
|---|---|
| double | A value in range [1..∞). The default value is 1. |
Expiration
Gets lease expiration timeout.
Declaration
public ref readonly Timeout Expiration { get; }
Property Value
| Type | Description |
|---|---|
| Timeout |
LeaseId
Gets the lease version.
Declaration
public ref readonly LeaseIdentity LeaseId { get; }
Property Value
| Type | Description |
|---|---|
| LeaseIdentity |
Remarks
The returned value can be used as a fencing token.
Token
Gets the token bounded to the lease lifetime.
Declaration
public CancellationToken Token { get; }
Property Value
| Type | Description |
|---|---|
| CancellationToken |
Methods
| Edit this page View SourceAcquireAsync(TimeSpan, Random?, CancellationToken)
Acquires the lease.
Declaration
public ValueTask AcquireAsync(TimeSpan pauseDuration, Random? pauseRandomizer = null, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | pauseDuration | The time to wait between TryAcquireAsync(CancellationToken) calls. |
| Random | pauseRandomizer | The source of random values that can be used to generate random pauses between TryAcquireAsync(CancellationToken) calls.
If null then use a value of |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing state of asynchronous execution of this method. |
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
| ArgumentOutOfRangeException |
|
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
| Edit this page View SourceDisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
Declaration
public ValueTask DisposeAsync()
Returns
| Type | Description |
|---|---|
| ValueTask | A task that represents the asynchronous dispose operation. |
DisposeAsyncCore()
Releases managed resources associated with this object asynchronously.
Declaration
protected override ValueTask DisposeAsyncCore()
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this method. |
Overrides
Remarks
This method makes sense only if derived class implements IAsyncDisposable interface.
ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>>, CancellationToken)
Executes the specified long-running operation that is protected by the lease lifetime.
Declaration
public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> worker, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task<TResult>> | worker | The function to be executed in the background. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task<TResult> | The value returned by |
Type Parameters
| Name | Description |
|---|---|
| TResult | The type of the result. |
Remarks
During execution, the lease is renewed automatically. On return, this method guarantees that the execution
of worker is completed.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
| TimeoutException | The lease has been expired. |
| AggregateException | TryRenewAsync(CancellationToken) throws an exception, and it is combined with the exception from |
ReleaseAsync(CancellationToken)
Releases a lease.
Declaration
public ValueTask<bool> ReleaseAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> |
Remarks
This method cancels Token immediately.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
| InvalidOperationException | This consumer never took the lease. |
| InvalidOperationException | This consumer never took the lease. |
ReleaseCoreAsync(LeaseIdentity, CancellationToken)
Performs a call to ReleaseAsync(LeaseIdentity, CancellationToken) across the application boundaries.
Declaration
protected abstract ValueTask<LeaseIdentity?> ReleaseCoreAsync(LeaseIdentity identity, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| LeaseIdentity | identity | The identity of the lease to renew. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<LeaseIdentity?> | The response from the lease provider; or null if the lease cannot be taken. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
TryAcquireAsync(CancellationToken)
Tries to acquire the lease.
Declaration
public ValueTask<bool> TryAcquireAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> | The expiration timeout; otherwise, Expired. |
Remarks
This method cancels Token immediately. If the method returns true, the token can be used to perform async operation bounded to the lease lifetime.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
TryAcquireCoreAsync(CancellationToken)
Performs a call to TryAcquireAsync(CancellationToken) across the application boundaries.
Declaration
protected abstract ValueTask<LeaseConsumer.AcquisitionResult?> TryAcquireCoreAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<LeaseConsumer.AcquisitionResult?> | The response from the lease provider; or null if the lease cannot be taken. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
TryRenewAsync(CancellationToken)
Tries to renew a lease.
Declaration
public ValueTask<bool> TryRenewAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> | The expiration timeout; otherwise, Expired. |
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
| InvalidOperationException | This consumer never took the lease. |
TryRenewCoreAsync(LeaseIdentity, CancellationToken)
Performs a call to TryRenewAsync(LeaseIdentity, bool, CancellationToken) or TryAcquireOrRenewAsync(LeaseIdentity, CancellationToken) across the application boundaries.
Declaration
protected abstract ValueTask<LeaseConsumer.AcquisitionResult?> TryRenewCoreAsync(LeaseIdentity identity, CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| LeaseIdentity | identity | The identity of the lease to renew. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<LeaseConsumer.AcquisitionResult?> | The response from the lease provider; or null if the lease cannot be taken. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |