HTTP Client & Rate Limiting
Relevant source files
The following files were used as context for generating this wiki page:
The HTTP Client is a central component of the regxa project, responsible for all network communication between the library and external package registries. It provides a unified interface for fetching JSON data while abstracting complex concerns such as retry logic, timeout management, and rate limit handling.
By consolidating network behavior into a single Client class, the project ensures consistent performance and reliability across different ecosystem adapters (npm, PyPI, Cargo, etc.). This architecture prevents individual adapters from implementing their own fetching logic and ensures that project-wide policies, such as exponential backoff and jitter, are applied universally.
Sources: AGENTS.md:79-81, src/core/AGENTS.md:7-9
Core Architecture
The Client class serves as the primary engine for HTTP requests. It is designed to be injected into registry adapters via a factory pattern, allowing for shared configuration and state across different registry instances.
Key Components
| Component | Description |
|---|---|
Client | The main class handling getJSON requests, retries, and timeouts. |
Retry Policy | Logic determining which HTTP status codes trigger a retry and the backoff duration. |
Rate Limiter Hook | A mechanism to handle 429 Too Many Requests responses, including parsing Retry-After headers. |
Error Mapping | Translation of standard HTTP errors into typed domain errors like HTTPError or RateLimitError. |
Sources: src/core/client.ts, src/core/AGENTS.md:20-22, AGENTS.md:54-57
Request Flow
The following diagram illustrates how a request from a registry adapter passes through the Client to the external API and handles potential failures.
This diagram shows the lifecycle of a request managed by the core Client.
Sources: src/core/client.ts, AGENTS.md:79-81
Retry and Backoff Logic
The Client implements a robust retry mechanism to handle transient network issues and server-side errors. It specifically targets a set of "retryable" status codes, including server errors (500, 502, 503, 504) and rate limits (429).
Configuration Defaults
Retries use an exponential backoff strategy combined with jitter to prevent "thundering herd" problems where many clients retry simultaneously.
- Max Retries: Defined within
client.ts. - Retryable Codes: 429, 500, 502, 503, 504.
- Backoff Policy: Exponentially increasing delays between attempts.
Sources: src/core/client.ts, src/core/AGENTS.md:21-22, AGENTS.md:81-81
Rate Limiting Handling
When a registry returns an HTTP 429 status code, the Client automatically transitions into rate-limiting mode. It is capable of parsing the Retry-After header to determine how long the client should wait before attempting the request again.
Header Parsing
The client supports multiple formats for the Retry-After header, including:
- Seconds: A simple integer representing the delay in seconds.
- HTTP-Date: A full date string indicating when the block will be lifted.
Sources: CHANGELOG.md:68-68, src/core/client.ts
Sequence of Rate Limit Recovery
The sequence above details the automated recovery process when hitting a rate limit.
Sources: src/core/client.ts, CHANGELOG.md:68-68
Error Hierarchy
The Client does not throw generic errors. Instead, it maps HTTP failure states to a typed error hierarchy defined in src/core/errors.ts. This allows callers to implement specific catch blocks for different failure modes.
HTTPError: The base class for all non-2xx responses.NotFoundError: Specifically thrown for 404 responses, often used by adapters to signal a missing package.RateLimitError: Thrown when a 429 is received and cannot be resolved within retry limits.TimeoutError: Thrown when a request exceeds the configured timeout duration.
Sources: src/core/errors.ts, AGENTS.md:54-57, test/unit/registries.test.ts:51-54
Implementation in Registry Adapters
Registry adapters (e.g., npm, PyPI) are forbidden from using direct fetch calls. They must use the provided Client instance, which ensures that all ecosystem-specific requests benefit from the global HTTP policies.
// Example from src/registries/npm.ts
async fetchPackage(name: string, signal?: AbortSignal): Promise<Package> {
const encodedName = this.encodeName(name);
const url = `${this.baseURL}/${encodedName}`;
try {
const data = await this.client.getJSON<NpmPackageResponse>(url, signal);
// ... normalization logic
} catch (error) {
if (error instanceof HTTPError && error.isNotFound()) {
throw new NotFoundError("npm", name);
}
throw error;
}
}
Sources: src/registries/npm.ts:81-105, AGENTS.md:104-104
The HTTP Client & Rate Limiting system provides the necessary resilience for a universal package registry client. By centralizing network logic, regxa maintains a stable and predictable interaction model with various external services, regardless of their individual API quirks or performance characteristics.