SemaphoreState

Represents the abstract state of a semaphore used by a RateLimiter which, depending on the implementation, can be stored in-memory or externally (e.g., in a database or cache). Additionally, an internal queue is used to store the requests that are waiting for permits to be available. If this state is to be shared between multiple instances of the rate limiter, it should be thread-safe. If not, it is protected by the rate limiter's internal synchronization mechanisms.

The state enforces a disposable pattern by implementing the AutoCloseable interface.

See also

Inheritors

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
abstract val permitsInUse: Int

The number of permits currently in use (i.e., permits that have been acquired but not yet released).

Link copied to clipboard

The queue to store the requests that are waiting for permits to be available.

Link copied to clipboard

The time mark indicating when the semaphore state was last replenished. It is used to determine when the rate limiting replenishment period has passed.

Functions

Link copied to clipboard
expect abstract fun close()
Link copied to clipboard
abstract fun setPermits(updateFunction: (Int) -> Int)

Updates the number of permits optionally considering the current value.

Link copied to clipboard

Sets a new replenishment time mark by essentially taking a snapshot of the current time.