authentik.api.throttle

Custom DRF throttle classes for authentik.

 1"""Custom DRF throttle classes for authentik."""
 2
 3from django.core.cache import caches
 4from rest_framework.throttling import AnonRateThrottle
 5
 6
 7class LocalAnonRateThrottle(AnonRateThrottle):
 8    """Anonymous IP-based rate throttle backed by an in-process cache.
 9
10    DRF's stock ``AnonRateThrottle`` uses the default cache. With authentik's
11    PG-backed default cache, every API request issues a ``cache.get`` +
12    ``cache.set`` against PG just to make the throttle decision — the
13    protective layer amplifies DB load under flood.
14
15    Points the throttle's cache at the ``throttle`` LocMemCache alias instead.
16    Counters are per-process (per gunicorn worker)
17    """
18
19    cache = caches["throttle"]
class LocalAnonRateThrottle(rest_framework.throttling.AnonRateThrottle):
 8class LocalAnonRateThrottle(AnonRateThrottle):
 9    """Anonymous IP-based rate throttle backed by an in-process cache.
10
11    DRF's stock ``AnonRateThrottle`` uses the default cache. With authentik's
12    PG-backed default cache, every API request issues a ``cache.get`` +
13    ``cache.set`` against PG just to make the throttle decision — the
14    protective layer amplifies DB load under flood.
15
16    Points the throttle's cache at the ``throttle`` LocMemCache alias instead.
17    Counters are per-process (per gunicorn worker)
18    """
19
20    cache = caches["throttle"]

Anonymous IP-based rate throttle backed by an in-process cache.

DRF's stock AnonRateThrottle uses the default cache. With authentik's PG-backed default cache, every API request issues a cache.get + cache.set against PG just to make the throttle decision — the protective layer amplifies DB load under flood.

Points the throttle's cache at the throttle LocMemCache alias instead. Counters are per-process (per gunicorn worker)

cache = <django.core.cache.backends.locmem.LocMemCache object>