Gaiia.RateLimit (Gaiia v0.2.0)

Copy Markdown View Source

Rate-limit accounting reported by the Gaiia API.

Limits are query-cost based, not request based: each API key has a point bucket, every operation removes its calculated cost, and points refill over time. Every response reports the bucket state in x-rate-limit-* headers, and a rejected operation additionally reports it in the extensions of a RATE_LIMITED GraphQL error.

Read remaining and retry_at to throttle before being rejected — the documented limits are not fixed, so they cannot be hardcoded by a client.

Both Gaiia.Response and Gaiia.Error carry this struct when the API reported it.

Summary

Functions

Build a rate-limit struct from the extensions of a RATE_LIMITED GraphQL error.

Build a rate-limit struct from response headers.

Types

t()

@type t() :: %Gaiia.RateLimit{
  allowed: boolean() | nil,
  cost: integer() | nil,
  limit: integer() | nil,
  remaining: integer() | nil,
  retry_at: DateTime.t() | nil,
  used: integer() | nil
}

Functions

from_extensions(extensions)

@spec from_extensions(map()) :: t() | nil

Build a rate-limit struct from the extensions of a RATE_LIMITED GraphQL error.

Such an operation was rejected, so allowed is false.

from_headers(headers)

@spec from_headers(map() | [{String.t(), String.t()}]) :: t() | nil

Build a rate-limit struct from response headers.

Returns nil when the response carries no x-rate-limit-* header, so callers can distinguish "not reported" from "reported as zero".