Usage metering and quota enforcement middleware.
This crate provides trait abstractions for tracking API usage and enforcing
quotas, along with a [MeteringMiddleware] that wires them into the proxy's
middleware chain. Integrators bring their own storage backends by implementing
[UsageRecorder] and [QuotaChecker].
Quick start
use ;
// Implement UsageRecorder and QuotaChecker for your storage backend,
// then register the middleware on the ProxyGateway builder:
let metering = new;
gateway_builder.add_middleware;
Architecture
- Pre-dispatch: [
QuotaChecker::check_quota] runs before the request proceeds, usingContent-Lengthas a byte estimate. ReturnErr(QuotaExceeded)to reject with HTTP 429. - Post-dispatch: [
UsageRecorder::record_operation] runs after the response is available, recording actual status and byte counts from the backend response.