aws/internal/retry/rate_limiter

Token-bucket retry rate limiter, port of the AWS Smithy runtime’s TokenBucket (aws-sdk-rust/…/client/retries/token_bucket.rs).

Semantics:

Concurrency: messages are processed sequentially per gleam_otp actor semantics.

Types

pub type AcquireResult {
  Acquired(permit: Permit)
  Empty
}

Constructors

  • Acquired(permit: Permit)
  • Empty

Opaque bucket handle.

pub opaque type Bucket
pub type BucketState {
  BucketState(available: Int, capacity: Int)
}

Constructors

  • BucketState(available: Int, capacity: Int)

An acquired set of tokens. Hold this until success or final outcome, then release it to return the tokens to the bucket.

pub opaque type Permit
pub type StartError {
  StartFailed(actor.StartError)
}

Constructors

Values

pub fn current(bucket: Bucket) -> BucketState

Read the current bucket state. Tests only.

pub const default_capacity: Int

Rust SDK default capacity. See DEFAULT_CAPACITY in token_bucket.rs.

pub const default_success_reward: Int

Rust SDK default success reward. See DEFAULT_SUCCESS_REWARD.

The bucket relies on permit RAII for normal recovery; success_reward is the additional top-up on success and defaults to 0.

pub fn permit_cost(p: Permit) -> Int
pub fn release(bucket: Bucket, permit permit: Permit) -> Nil

Return the permit’s tokens to the bucket. Called on success, on a final non-retryable response, and before replacing one held permit with a new one mid-retry-loop.

pub fn reward_success(bucket: Bucket) -> Nil

Top up the bucket by success_reward tokens, capped at capacity. Called once per successful operation.

pub fn shutdown(bucket: Bucket) -> Nil

Tell the bucket actor to exit. Fire-and-forget. Mirrors credentials_cache.shutdown; both call into aws/internal/actor_lifecycle.shutdown_via_stop. Safe to call multiple times.

pub fn shutdown_sync(
  bucket: Bucket,
  timeout_ms: Int,
) -> Result(Nil, Nil)

Synchronous teardown — monitors the actor, sends Stop, waits for DOWN. Ok(Nil) on clean exit, Error(Nil) on real timeout. Idempotent for already-dead actors.

pub fn start(
  capacity capacity: Int,
  success_reward success_reward: Int,
) -> Result(Bucket, StartError)

Start a bucket with explicit capacity + reward.

pub fn start_default() -> Result(Bucket, StartError)

Start a bucket with Rust SDK defaults (500 / 0).

pub fn try_acquire(
  bucket: Bucket,
  cost cost: Int,
) -> AcquireResult

Try to acquire cost tokens. Returns Acquired(permit) if the bucket can pay; Empty if not (caller must NOT retry).

Search Document