app.repository.abuse_limit_counter_repository module

class app.repository.abuse_limit_counter_repository.AbuseLimitCounterRepository(session_factory, model=<class 'app.model.abuse_limit_counter.AbuseLimitCounter'>)[source]

Bases: BaseRepository

Repository for abuse prevention counters.

The increment operation is safe under concurrent writes: - update existing bucket atomically (counter = counter + 1) - if bucket does not exist, insert - if concurrent insert collides, retry update

Parameters:

session_factory (Callable[[...], AbstractAsyncContextManager[AsyncSession]])

async increment_and_get(scope_type, scope_value, window_name, window_start)[source]

Atomically increment a rate-limit bucket and return its new value.

Concurrency-safe: it first tries an atomic counter = counter + 1 update; if the bucket does not yet exist it inserts it, and if a concurrent insert collides it rolls back and retries the update. The window_start is normalized to UTC.

Parameters:
  • scope_type (str) – Dimension being limited (e.g. "ip", "api_key").

  • scope_value (str) – Concrete value within scope_type.

  • window_name (str) – Name of the limit window (e.g. "per_minute").

  • window_start (datetime) – Start of the bucket’s time window.

Returns:

int – The counter value after this increment.

Return type:

int