$ show ThreeMammals/Ocelot
Ocelot is a .NET library providing a highly configurable, middleware-based API Gateway for ASP.NET Core applications. It is not a standalone application but a framework that integrates into an ASP.NET Core host via `AddOcelot()` and `UseOcelot()`. Its architecture centers on a fixed-order middleware pipeline that processes incoming HTTP requests through stages including configuration loading, route matching, security, rate limiting, authentication, authorization, load balancing, URL rewriting, and proxying to downstream services. The pipeline is extensible through custom delegates and handlers, and supports advanced features like WebSocket proxying, response aggregation, and circuit breaking. The codebase demonstrates strong engineering practices with a clean separation of concerns across modules for Configuration, Routing, Load Balancing, Quality of Service, Rate Limiting, Caching, and WebSocket proxying. It features an extensive test suite with over a thousand unit and acceptance tests, many of which are linked to specific GitHub issues, indicating rigorous issue-driven development. The configuration system is particularly robust, supporting JSON file merging, environment-specific overrides, dynamic reloading, and fluent validation. While the core architecture is modern and well-maintained, targeting .NET 8 through 10, the codebase exhibits some technical drift. It relies on deprecated dependencies like `Microsoft.Extensions.DiagnosticAdapter` and `Newtonsoft.Json` alongside the newer `System.Text.Json`. There are known concurrency bottlenecks (e.g., a single static lock in rate limiting, blocking calls in the async configuration poller) and a reliance on reflection in unit tests that makes them brittle. Several properties are marked obsolete with planned removal in version 25.0. Despite this debt, Ocelot contains several genuinely reusable components that demonstrate non-obvious craftsmanship, such as a generic, thread-safe Circuit Breaker, a flexible Retry helper, and a clean DSL parser for claim transformations. It is an ideal choice for .NET teams requiring a robust, self-hosted, and extensible API gateway without the overhead of a commercial solution. Its primary value lies in its comprehensive feature set, extensibility, and the quality of its testing infrastructure.
The LLM identified 5 files worth highlighting for reuse:
Implements a generic, thread-safe circuit breaker with two modes (count-based and ratio-based), a half-open probe mechanism, and configurable thresholds. It is self-contained with no internal Ocelot dependencies, making it directly extractable into a standalone resilience library. The implementation demonstrates careful state management and thread safety.
A generic retry helper for both synchronous and asynchronous operations. It accepts a predicate to determine retry eligibility, configurable wait times, and a logger for diagnostics. The class is fully self-contained and can be reused in any .NET project requiring resilient operation execution.
Parses a domain-specific language (`Claims[...] > value[...] > delimiter`) used to define claim-to-header, claim-to-query, and claim-to-path transformations. The parsing logic is cleanly isolated from the rest of the gateway, handling edge cases like missing instructions and malformed syntax with specific error types. This demonstrates a well-encapsulated parsing pattern.
A clean implementation of the least-connections load balancing algorithm using atomic counters for thread safety. It maintains a concurrent dictionary of active connection counts per service and selects the service with the fewest active connections. The implementation is concise, correct, and follows the `ILoadBalancer` interface cleanly.
A clever test utility that wraps configuration model objects to provide a fluent interface for setting up test data. It significantly reduces boilerplate in test files by allowing chained property assignment. While specific to Ocelot's file configuration models, the boxing pattern itself is a reusable technique for creating expressive test fixtures in any project.