countAttempts method

  1. @override
Future<int> countAttempts(
  1. Session session, {
  2. required String key,
  3. Transaction? transaction,
})
override

Counts attempts for key within the configured window, or all time.

Implementation

@override
Future<int> countAttempts(
  final Session session, {
  required final String key,
  final Transaction? transaction,
}) async {
  return await RateLimitedRequestAttempt.db.count(
    session,
    where: (final t) {
      var expression =
          t.domain.equals(config.domain) &
          t.source.equals(config.source) &
          t.key.equals(key);

      if (config.timeframe != null) {
        final oldestRelevantAttemptTimestamp = clock.now().subtract(
          config.timeframe!,
        );
        expression &= t.attemptedAt > oldestRelevantAttemptTimestamp;
      }

      return expression;
    },
    transaction: transaction,
  );
}