Tink.RateLimiter (Tink v1.0.0)

Copy Markdown View Source

Client-side rate limiting using Hammer (token bucket / ETS backend).

Prevents your application from sending too many requests and hitting Tink's 429 rate limit. Configured per client_id so multiple application clients share the same bucket.

Per Tink's docs: rate limits are validated on a per-app-ID basis, and exceeding them returns an HTTP 429 Too Many Requests response. Tink does not publish an exact request-per-minute number — contact Tink support if you are consistently hitting 429s during expected use.

Configuration

config :tink,
  rate_limit: [
    enabled:          true,
    requests_per_min: 600,    # conservative client-side default — Tink
                               # enforces its own per-app-ID server-side
                               # limit but does not publish an exact
                               # number; tune this to match your plan
    burst_size:       60      # max burst above the per-minute rate
  ]

Disabling rate limiting

config :tink, rate_limit: [enabled: false]

Usage

Rate limiting is applied automatically by Tink.Client before each request. You can also check or consume the bucket manually:

case Tink.RateLimiter.check(client) do
  :ok                          -> # proceed
  {:error, :rate_limited, ms}  -> # wait ms milliseconds
end

Telemetry

Emits [:tink, :rate_limit, :check] with metadata %{key, allowed}.

Summary

Functions

Return the burst size.

Check and consume one token for the given client. Returns :ok or {:error, :rate_limited, retry_after_ms}.

Whether rate limiting is enabled per config.

Return the configured requests-per-minute limit.

Functions

burst_size()

@spec burst_size() :: pos_integer()

Return the burst size.

check(client_key)

@spec check(String.t()) :: :ok | {:error, :rate_limited, non_neg_integer()}

Check and consume one token for the given client. Returns :ok or {:error, :rate_limited, retry_after_ms}.

enabled?()

@spec enabled?() :: boolean()

Whether rate limiting is enabled per config.

requests_per_minute()

@spec requests_per_minute() :: pos_integer()

Return the configured requests-per-minute limit.