IbkrApi.RateLimiter (ibkr_api v1.0.3)

View Source

Rate limiting implementation for IBKR API requests using Hammer v7.

Enforces both global and per-endpoint rate limits according to IBKR's published limits:

  • Global: 50 requests per second
  • Gateway: 10 requests per second
  • Per-endpoint: Various limits as documented

Rate limits are configurable through options passed to each function.

Summary

Functions

Checks the endpoint-specific rate limit.

Checks the global rate limit for all IBKR API requests.

Checks if a request should be allowed based on the rate limits.

Extracts the endpoint name from a URL path.

Executes a function with rate limiting.

Functions

check_endpoint_rate(url_path, endpoint, opts \\ [])

@spec check_endpoint_rate(binary(), atom(), keyword()) :: :ok | {:error, any()}

Checks the endpoint-specific rate limit.

Each endpoint may have its own rate limit as defined in IBKR's documentation.

check_global_rate(opts \\ [])

@spec check_global_rate(keyword()) :: :ok | {:error, any()}

Checks the global rate limit for all IBKR API requests.

By default, this is 50 requests per second for authenticated users.

check_rate(url_path, opts \\ [])

@spec check_rate(
  binary(),
  keyword()
) :: :ok | {:error, any()}

Checks if a request should be allowed based on the rate limits.

Options

  • :rate_limits (keyword/0) - Rate limit configuration as a keyword list. If not provided, uses Config defaults.

  • :bucket_prefix (String.t/0) - Prefix for rate limit buckets (useful for test isolation) The default value is "".

Returns

  • :ok if the request is allowed
  • {:error, reason} if the request exceeds rate limits

extract_endpoint(url_path)

@spec extract_endpoint(binary()) :: atom()

Extracts the endpoint name from a URL path.

Examples: "/iserver/marketdata/snapshot" -> "iserver_marketdata_snapshot" "/iserver/scanner/run" -> "iserver_scanner_run"

with_rate_limit(url_path, opts \\ [], fun)

@spec with_rate_limit(binary(), keyword(), function()) :: any() | {:error, any()}

Executes a function with rate limiting.

Returns the function result if the rate limit allows the request, or {:error, error_message} if the rate limit is exceeded.