Tinkex.BytesSemaphore (Tinkex v0.3.4)
View SourceByte-budget semaphore for rate limiting by payload size.
Tracks a shared byte budget across concurrent callers. Acquisitions can push the budget negative to allow in-flight work to complete; new acquisitions block while the budget is negative and resume once releases bring it back to a non-negative value.
Summary
Functions
Acquire bytes from the semaphore, blocking while the budget is negative.
Returns a specification to start this module under a supervisor.
Release bytes back to the semaphore.
Start a BytesSemaphore with the given byte budget.
Execute fun while holding the requested byte budget.
Types
@type t() :: pid()
Functions
@spec acquire(t(), non_neg_integer()) :: :ok
Acquire bytes from the semaphore, blocking while the budget is negative.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec release(t(), non_neg_integer()) :: :ok
Release bytes back to the semaphore.
@spec start_link(keyword()) :: GenServer.on_start()
Start a BytesSemaphore with the given byte budget.
@spec with_bytes(t(), non_neg_integer(), (-> result)) :: result when result: any()
Execute fun while holding the requested byte budget.