Expand description
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 multistore_metering::{MeteringMiddleware, UsageRecorder, QuotaChecker};
// Implement UsageRecorder and QuotaChecker for your storage backend,
// then register the middleware on the ProxyGateway builder:
let metering = MeteringMiddleware::new(my_quota_checker, my_usage_recorder);
gateway_builder.add_middleware(metering);§Architecture
- Pre-dispatch:
QuotaChecker::check_quotaruns before the request proceeds, usingContent-Lengthas a byte estimate. ReturnErr(QuotaExceeded)to reject with HTTP 429. - Post-dispatch:
UsageRecorder::record_operationruns after the response is available, recording actual status and byte counts from the backend response.
Structs§
- Metering
Middleware - Middleware that enforces quotas pre-dispatch and records usage post-dispatch.
- Noop
Quota Checker - A
QuotaCheckerthat always allows requests. Useful when only usage recording is needed, or for testing. - Noop
Recorder - A
UsageRecorderthat does nothing. Useful when only quota checking is needed, or for testing. - Quota
Exceeded - Quota violation error returned by
QuotaChecker::check_quota. - Usage
Event - A completed operation’s metadata, passed to
UsageRecorder::record_operation.
Traits§
- Quota
Checker - Pre-dispatch quota enforcement.
- Usage
Recorder - Records completed operations for usage tracking.