Opinionated defaults¶
paceapi is opinionated. Where a choice has to be made it picks the safe behavior, and it raises configuration mistakes when a limit, cache route, or middleware is declared instead of on the first request. This page lists the defaults and how to change each.
Rate limiting¶
- Limits count by client address:
key=defaults toclient_ip.key=Noneis rejected (Keys). - A key that returns nothing fails the request:
on_missing_key="raise"raisesMissingKeyError, so a broken key cannot switch a limit off unnoticed. Pass"skip"to exempt such requests. - Each route has its own bucket: Pass
scope=to share one bucket across routes (Multiple limits). - Sliding window: The default algorithm.
FixedWindowandTokenBucketare opt-in (Algorithms). - Store failures fail open:
on_store_error="allow"lets requests through, logs the failure, and sends no rate limit headers. Pass"deny"or"raise"to change it (Failure modes). X-RateLimit-*headers are sent: Passheader_style=Noneto omit them (Headers and errors).
Caching¶
- Signed-in requests are cached only when the cache knows the caller: A request with
AuthorizationorCookieis not cached on an@ns.responseroute.Cache(credentials=["Authorization", "Cookie"])names the headers callers sign in with and turns on a cached copy per caller. An app that uses Starlette'sAuthenticationMiddlewarewith a user class that definesidentitygets per-caller copies withoutcredentials=.shared=Truecaches one copy for every caller (Caching responses). Cache-Control: publicis not sent to a signed-in caller: An endpoint that sets it on a response it returns has it removed for such requests. AnHTTPExceptionit raises, and a method outsidemethods=, are sent as they are.entity=on a decorator is for invalidation only: It names what a value is about and does not decide who may be sent it. (ResponseCacheMiddleware(entity=)is different: there it is the key that says whose a response is.)- Values are stored as JSON:
JSONSerializeris the default;PickleSerializeris opt-in (Quickstart). - Failed reads are misses; failed writes and invalidations raise: A silently lost invalidation would leave stale data behind (Consistency guarantees).
Traffic management¶
- No queueing:
ConcurrencyLimit(wait=0)rejects with503as soon as every slot is held. Passwait=for bounded queueing (Concurrency limits). - Unlisted priorities are never shed:
LoadShedderonly sheds the classes it is given (Load shedding). - Deadlines need async endpoints:
@deadline.enforced()rejects sync endpoints, because a worker thread cannot be cancelled (Deadlines). - Signed-in callers coalesce only with themselves:
CoalescingMiddlewarematches onAuthorizationandCookieby default (Request coalescing).
Everywhere¶
- Mistakes fail at startup: Options are validated when they are declared. A wrong type, an unknown policy, or a key that cannot work raises before the app serves a request.
- No global state: Every
Limiter,Cache, and limit is an object you create and pass. asyncioonly: Timeouts, locks, and tasks useasyncio; Trio is not supported.