Skip to main content

Class Semaphore

Namespace: Consul
Assembly: Consul.dll

Semaphore is used to implement a distributed semaphore using the Consul KV primitives.

public class Semaphore : IDistributedSemaphore

Inheritance​

object ← Semaphore

Implements​

IDistributedSemaphore

Inherited Members​

object.Equals(object) , object.Equals(object, object) , object.GetHashCode() , object.GetType() , object.MemberwiseClone() , object.ReferenceEquals(object, object) , object.ToString()

Fields​

DefaultMonitorRetryTime​

DefaultMonitorRetryTime is how long we wait after a failed monitor check of a semaphore (500 response code). This allows the monitor to ride out brief periods of unavailability, subject to the MonitorRetries setting in the lock options which is by default set to 0, disabling this feature.

public static readonly TimeSpan DefaultMonitorRetryTime

Field Value​

TimeSpan

DefaultSemaphoreKey​

DefaultSemaphoreKey is the key used within the prefix to use for coordination between all the contenders.

public static readonly string DefaultSemaphoreKey

Field Value​

string

DefaultSemaphoreRetryTime​

DefaultSemaphoreRetryTime is how long we wait after a failed lock acquisition before attempting to do the lock again. This is so that once a lock-delay is in affect, we do not hot loop retrying the acquisition.

public static readonly TimeSpan DefaultSemaphoreRetryTime

Field Value​

TimeSpan

DefaultSemaphoreWaitTime​

DefaultSemaphoreWaitTime is how long we block for at a time to check if semaphore acquisition is possible. This affects the minimum time it takes to cancel a Semaphore acquisition.

public static readonly TimeSpan DefaultSemaphoreWaitTime

Field Value​

TimeSpan

Properties​

IsHeld​

public bool IsHeld { get; }

Property Value​

bool

Methods​

Acquire()​

Acquire attempts to reserve a slot in the semaphore, blocking until success. Not providing a CancellationToken means the thread can block indefinitely until the lock is acquired. There is no notification that the semaphore slot has been lost, but IsHeld may be set to False at any time due to session invalidation, communication errors, operator intervention, etc. It is NOT safe to assume that the slot is held until Release() unless the Session is specifically created without any associated health checks. By default Consul sessions prefer liveness over safety and an application must be able to handle the session being lost.

public Task<CancellationToken> Acquire()

Returns​

Task <CancellationToken >

Acquire(CancellationToken)​

Acquire attempts to reserve a slot in the semaphore, blocking until success, interrupted via CancellationToken or if an error is encountered. A provided CancellationToken can be used to abort the attempt. There is no notification that the semaphore slot has been lost, but IsHeld may be set to False at any time due to session invalidation, communication errors, operator intervention, etc. It is NOT safe to assume that the slot is held until Release() unless the Session is specifically created without any associated health checks. By default Consul sessions prefer liveness over safety and an application must be able to handle the session being lost.

public Task<CancellationToken> Acquire(CancellationToken ct)

Parameters​

ct CancellationToken

The cancellation token to cancel semaphore acquisition

Returns​

Task <CancellationToken >

Destroy(CancellationToken)​

Destroy is used to cleanup the semaphore entry. It is not necessary to invoke. It will fail if the semaphore is in use.

public Task Destroy(CancellationToken ct = default)

Parameters​

ct CancellationToken

Returns​

Task

Release(CancellationToken)​

Release is used to voluntarily give up our semaphore slot. It is an error to call this if the semaphore has not been acquired.

public Task Release(CancellationToken ct = default)

Parameters​

ct CancellationToken

Returns​

Task

  • Fields
    • DefaultMonitorRetryTime
    • DefaultSemaphoreKey
    • DefaultSemaphoreRetryTime
    • DefaultSemaphoreWaitTime
  • Properties
    • IsHeld
  • Methods
    • Acquire()
    • Acquire(CancellationToken)
    • Destroy(CancellationToken)
    • Release(CancellationToken)