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
View SourceLeaseConsumer(TimeProvider?)
Initializes a new lease consumer.
Declaration
protected LeaseConsumer(TimeProvider? provider = null)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeProvider | provider | The timer factory. |
Properties
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 |
Token
Gets the token bounded to the lease lifetime.
Declaration
public CancellationToken Token { get; }
Property Value
| Type | Description |
|---|---|
| CancellationToken |
Version
Gets the lease version.
Declaration
public long Version { get; }
Property Value
| Type | Description |
|---|---|
| long | The version of the lease. The value is meaningful only after the lease has been acquired. |
Remarks
The returned value can be used as a fencing token. The property can be read concurrently with the renewal of the lease.
Methods
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
View SourceDisposeAsync()
Declaration
public ValueTask DisposeAsync()
Returns
| Type | Description |
|---|---|
| ValueTask |
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, or it has been released. |
ReleaseCoreAsync(CancellationToken)
Performs a call to ReleaseAsync(Guid, CancellationToken) across the application boundaries.
Declaration
protected abstract ValueTask<long?> ReleaseCoreAsync(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask<long?> | 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> | true if the lease is acquired successfully; otherwise, false. |
Remarks
If the lease is acquired, the previously obtained Token is canceled and the new token is issued. It can be used to perform async operation bounded to the lease lifetime. If the provider rejects the request, the state of the currently held lease remains unchanged.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
TryAcquireCoreAsync(CancellationToken)
Performs a call to TryAcquireAsync(Guid, 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> | true if the lease is renewed successfully; otherwise, false. |
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The consumer is disposed. |
| OperationCanceledException | The operation has been canceled. |
| InvalidOperationException | This consumer never took the lease, or it has been released. |
TryRenewCoreAsync(CancellationToken)
Performs a call to TryRenewAsync(Guid, bool, CancellationToken) across the application boundaries.
Declaration
protected abstract ValueTask<LeaseConsumer.AcquisitionResult?> TryRenewCoreAsync(CancellationToken token)
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. |