Tinkex.BytesSemaphore (Tinkex v0.3.4)

View Source

Byte-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

t()

@type t() :: pid()

Functions

acquire(semaphore, bytes)

@spec acquire(t(), non_neg_integer()) :: :ok

Acquire bytes from the semaphore, blocking while the budget is negative.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

release(semaphore, bytes)

@spec release(t(), non_neg_integer()) :: :ok

Release bytes back to the semaphore.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start a BytesSemaphore with the given byte budget.

with_bytes(semaphore, bytes, fun)

@spec with_bytes(t(), non_neg_integer(), (-> result)) :: result when result: any()

Execute fun while holding the requested byte budget.