Show / Hide Table of Contents

Class LeaseConsumer

Represents client side of a lease in a distributed environment.

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

Constructors

| Edit this page View Source

LeaseConsumer(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 Source

ClockDriftBound

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.

| Edit this page View Source

Expiration

Gets lease expiration timeout.

Declaration
public ref readonly Timeout Expiration { get; }
Property Value
Type Description
Timeout
| Edit this page View Source

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.

| Edit this page View Source

Token

Gets the token bounded to the lease lifetime.

Declaration
public CancellationToken Token { get; }
Property Value
Type Description
CancellationToken

Methods

| Edit this page View Source

AcquireAsync(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 pauseDuration.

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

pauseDuration is less than or equal to Zero; or greater than MaxTimeoutParameterTicks.

| Edit this page 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)
| Edit this page View Source

DisposeAsync()

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.

| Edit this page View Source

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
Disposable.DisposeAsyncCore()
Remarks

This method makes sense only if derived class implements IAsyncDisposable interface.

| Edit this page View Source

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 worker.

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 worker.

| Edit this page View Source

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>

true if lease canceled successfully; otherwise, false.

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

Implements

IDisposable
IAsyncDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)

See Also

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