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:
- Each retry attempt
acquires tokens. The cost depends on the error class (see retry.gleam:retry_cost5 for normal retryable,timeout_retry_cost10 for timeouts / transient). - The acquired tokens are HELD as a
Permitwhile the retry is in flight. On success or final non-retryable outcome the callerreleases the permit, returning the tokens. On retry-failure-then- -retry, the caller releases the prior permit before acquiring the next (Rust does this via permit drop onset_retry_permit). reward_successadditionally tops up the bucket bysuccess_rewardtokens. Rust’s default is 0 — the bucket is a concurrent-retry semaphore, not an AIMD-style limiter; the time-basedrefill_ratehandles long-term recovery in Rust. We follow Rust’s default.
Concurrency: messages are processed sequentially per gleam_otp
actor semantics.
Types
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
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).