$ show Nya-Foundation/NyaProxy
NyaProxy is a lightweight API gateway/proxy service built with FastAPI and Uvicorn that manages authentication, credential rotation, load balancing, rate limiting, and observability for HTTP APIs. It acts as an intermediary between clients and upstream services, injecting credentials, enforcing quotas, and providing features like retry logic, key quarantine, and request/response transformation. The system includes a web dashboard for monitoring traffic, metrics, and queue status, and exposes Prometheus metrics for external monitoring. The architecture follows a modular design with separate concerns: core proxy logic (queue-first request handling), traffic management (load balancing and rate limiting), configuration management (using Nacho for layered config and schema validation), services (metrics, state persistence), and a dashboard interface. Requests are processed via a priority queue with worker pools, enabling prioritization of retries and high-priority requests. State persistence saves rate limit windows and key cooldowns to disk using SHA256-hashed keys to avoid storing secrets, allowing survival across restarts. Key entry points include the CLI (`python -m nya` or `nyaproxy` script) and the main application factory in `nya/server/app.py`. The system relies on dependencies like FastAPI, Uvicorn, Pydantic, Nacho, and the Prometheus client, with explicit version pins in `pyproject.toml` and regular updates shown in the changelog. The codebase demonstrates strong engineering practices including comprehensive test coverage (unit and end-to-end), secure secret handling, and clean separation of concerns.
The LLM identified 3 files worth highlighting for reuse:
Handles atomic saving and loading of runtime state (rate limit windows, key cooldowns) to JSON files. Uses SHA256 hashing to avoid storing secrets in plain text, includes versioning and migration of state files, and provides graceful degradation when state is missing/corrupt. This isolated module could be extracted for any application needing secure, versioned state persistence.
Implements five load balancing algorithms (round_robin, random, least_requests, fastest_response, weighted) as interchangeable strategies. Each strategy is a self-contained class with runtime selection capability. The module defines a clear abstraction (LoadBalancer base class) and could be used in any load balancing scenario beyond HTTP proxies.
Provides utilities to redact sensitive information (API keys, tokens) from logs and metrics using pattern matching and explicit header lists. Includes hop-by-hop header filtering and constant-time secret comparison. This focused utility solves a common security concern in proxy services and could be reused in any application handling sensitive data.