xtr_rate_limiter.bundle ¶
The xtr-dependency-injection bundle for xtr-rate-limiter.
BuilderConfig
dataclass
¶
BuilderConfig(storage: str | Reference = CACHE_STORAGE, cache_pool: str = DEFAULT_CACHE_POOL, lock: str | None = AUTO_LOCK)
Configuration for the :class:~xtr_rate_limiter.RateLimiterBuilder service.
The builder counts over a storage, so its limiters keep their state in a cache pool, in this process, or in a storage the container provides — not in Redis, which counts with scripts of its own.
Attributes:
-
storage(str | Reference) –"cache","in-memory", or a :class:~xtr_dependency_injection.Referenceto a :class:~xtr_rate_limiter.storage.StorageInterface. -
cache_pool(str) –The cache pool a
"cache"storage keeps states in. -
lock(str | None) –The lock around each read and write of a state:
"auto", a lock resource's name, orNoneto lock within this process only.
RateLimiterBundle ¶
Bases: Bundle[RateLimiterConfig]
Turns a :class:RateLimiterConfig into a limiter factory per name.
prepend_extension ¶
When cache is active, give it the rate_limiter pool limiters keep their state in.
The pool uses the cache's application adapter under a namespace of its own, so clearing another pool never resets a limit. An application declaring the pool itself keeps its own.
load_extension ¶
load_extension(config: RateLimiterConfig, services: ServiceConfigurator, builder: ContainerBuilder) -> None
Register a factory under each limiter's name, and the builder.
Raises:
-
InvalidArgumentError–When a limiter names a lock resource while the lock bundle is not active.
boot
async
¶
Refuse a storage the bundle cannot build, or a service nobody registered, before any hit.
The configuration is read resolved, so a storage given as
env(...) is read here, and a variable that is not set fails the
boot. The builder is checked only when it was pointed somewhere: its
default needs the cache, which an application limiting nothing in
code need not have.
Raises:
-
InvalidArgumentError–Naming the limiter whose storage is wrong.
RateLimiterConfig
dataclass
¶
RateLimiterConfig(limiters: Mapping[str, LimiterConfig] = _no_limiters(), builder: BuilderConfig = BuilderConfig(), redis_prefix: str = DEFAULT_PREFIX)
Which limiters exist, by name.
Every limiter becomes a
:class:~xtr_rate_limiter.rate_limiter_factory_interface.RateLimiterFactoryInterface
qualified by its name. With no configuration there are none.
RateLimiterConfig(
limiters={
"api": LimiterConfig("sliding_window", limit=100, interval="1 minute"),
"login": LimiterConfig(
"fixed_window", limit=5, interval="15 minutes", storage=env("REDIS_DSN")
),
"uploads": LimiterConfig("token_bucket", limit=10, rate=Rate("1 minute", amount=2)),
"strict": LimiterConfig("compound", limiters=["api", "uploads"]),
},
)
Attributes:
-
limiters(Mapping[str, LimiterConfig]) –Every limiter, by name.
-
builder(BuilderConfig) –Where the limiters the :class:
~xtr_rate_limiter.rate_limiter_builder.RateLimiterBuilderservice builds keep their state. -
redis_prefix(str) –What every key a limiter counting in Redis writes starts with; give two applications sharing a server different ones.