Show / Hide Table of Contents

Struct LeaseState<TMetadata>

Represents a state of the lease.

Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
ValueType.ToString()
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: DotNext.Threading.Leases
Assembly: DotNext.Threading.dll
Syntax
public readonly struct LeaseState<TMetadata>
Type Parameters
Name Description
TMetadata

The type of metadata associated with a lease.

Properties

View Source

CreatedAt

A timestamp of when this state was created.

Declaration
public required DateTimeOffset CreatedAt { get; init; }
Property Value
Type Description
DateTimeOffset
Remarks

The value is informational and not used for expiration tracking, except the default value that indicates the released lease.

View Source

Metadata

The metadata associated with the lease.

Declaration
public TMetadata Metadata { get; init; }
Property Value
Type Description
TMetadata
View Source

OwnerId

The unique identifier of the lease owner.

Declaration
public required Guid OwnerId { get; init; }
Property Value
Type Description
Guid
Remarks

Empty if the lease is released or there is no state in the underlying persistent storage.

View Source

RenewalStamp

Gets or sets the stamp of the latest renewal of the lease.

Declaration
public Guid RenewalStamp { get; init; }
Property Value
Type Description
Guid
Remarks

The stamp allows renewing the lease without incrementing Version, so the fencing token remains the same while the lease is held by the same owner, and the renewal doesn't need to be durable. The value is opaque, it is compared only for equality and must not be interpreted.

Empty means that the underlying storage doesn't support renewal stamps. In that case, every renewal increments Version. The renewal that updates the metadata always increments Version.

If the underlying storage supports renewal stamps, it must return the stamp of the current state, including the released state and the state of the lease that doesn't exist, and never Empty. The compare-and-set operation must check the stamp in addition to Version, and store the stamp of the new state on success. The stamp can be kept in memory only: if it's lost, e.g., on restart, the storage must generate a new one using NewGuid(). The storage must never return the stamp again after it has been replaced or lost, otherwise the providers can consider the lease expired while it's still held.

View Source

TimeToLive

Gets or sets lease duration.

Declaration
public required TimeSpan TimeToLive { get; init; }
Property Value
Type Description
TimeSpan

The positive lease duration.

Remarks

The value is provided by the underlying storage, so all the providers sharing the same storage agree on the lease duration. It must be set even if there is no state in the underlying storage.

The underlying storage must not decrease the value while the lease is acquired, because the current owner relies on the duration it received on acquisition or renewal, while the other providers apply the decreased value immediately, and take over the lease before the owner considers it expired. The value can be decreased only when the lease is released. Increasing the value is always safe.

Exceptions
Type Condition
ArgumentOutOfRangeException

The value is not positive or greater than MaxTimeoutParameterTicks.

View Source

Version

A version of the lease.

Declaration
public required long Version { get; init; }
Property Value
Type Description
long
Remarks

The version never decreases, so it can be used as a fencing token. It is incremented on acquisition and release of the lease. If the underlying storage supports RenewalStamp, the renewal of the lease by the same owner doesn't change the version. Otherwise, the version is incremented on renewal as well.

The underlying storage must create a lease (including the re-creation after deletion) with the version taken from the global non-negative sequence number that never decreases, e.g., a database sequence. Otherwise, the re-created lease can reuse the versions of the deleted one, which breaks the expiration tracking, the compare-and-set semantics of the storage and fencing. If there is no state in the underlying storage, return the released state with such version.

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)

See Also

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