ratelimit

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limit tracking for the MarketData SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Headers

type Headers struct {
	Limit     int
	Remaining int
	Consumed  int
	ResetAt   time.Time

	HasLimit     bool
	HasRemaining bool
	HasConsumed  bool
	HasReset     bool
}

Headers is the X-Api-Ratelimit-* set parsed from one response.

It exists because the same four headers were parsed in four places, by three different rules, and they disagreed. On a single response carrying "X-Api-Ratelimit-Reset: 0" the SDK reported the reset time as the zero time through Response.RateLimit, and as 1969-12-31 through both Client.RateLimits() and UserInfo — the 1969 rendering being a bug this release had already fixed, in one of the four parsers. The copies also diverged on malformed input: a Sscanf-based reader accepted "100abc" as 100 where a strconv-based one read 0.

One parser, one set of rules:

  • strconv, so a value with trailing garbage is rejected rather than silently truncated.
  • A missing or unparseable header leaves its Has* flag false, so a caller can tell "absent" from "present and zero" — which matters for the reset time, where zero is not a plausible instant.
  • ResetAt is normalized to US/Eastern like every other time-bearing field in the SDK (ADR-005).

func ParseHeaders

func ParseHeaders(h http.Header) Headers

ParseHeaders reads the rate-limit headers from h. A nil header set yields the zero value, with every Has* flag false.

type State

type State struct {
	// Limit is the maximum requests allowed in the current window
	Limit int

	// Remaining is the number of requests remaining
	Remaining int

	// Consumed is the credit cost of the MOST RECENT request, not a running
	// total for the window: the API reports it per response and the tracker
	// stores the latest value. For the window total use Limit - Remaining.
	Consumed int

	// ResetAt is when the rate limit resets
	ResetAt time.Time
}

State represents the current rate limit state.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker tracks API rate limit state. It is safe for concurrent use.

func New

func New() *Tracker

New creates a new rate limit tracker.

func (*Tracker) Release

func (t *Tracker) Release()

Release releases a credit reserved by Reserve.

func (*Tracker) Reserve

func (t *Tracker) Reserve() bool

Reserve reserves one credit for an in-flight request. It returns false when credits are exhausted (accounting for other in-flight requests) and the reset time hasn't passed yet. Callers must call Release when the request completes.

A limit of 0 marks unmetered access, not exhaustion: the API sends limit=0 headers on anonymous (demo) responses, so a zero limit must never block — the same reading the Java SDK applies.

func (*Tracker) State

func (t *Tracker) State() State

State returns the current rate limit state.

func (*Tracker) Update

func (t *Tracker) Update(resp *http.Response)

Update updates the rate limit state from response headers. Out-of-order updates are discarded: a response carrying an older reset window, or a higher remaining count within the same window, completed out of order and would move the snapshot backwards.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL