deleteAttempts method

  1. @override
Future<int> deleteAttempts(
  1. Session session, {
  2. String? key,
  3. Duration? olderThan,
  4. Transaction? transaction,
})
override

Deletes attempts within this limiter's domain and source.

If key is omitted, matches every key in this scope. If olderThan is provided, only attempts strictly older than that duration are removed, measured from the current time. Omitting olderThan removes all matching attempts, regardless of the configured window. Returns the number of deleted attempts.

Implementation

@override
Future<int> deleteAttempts(
  final Session session, {
  final String? key,
  final Duration? olderThan,
  final Transaction? transaction,
}) async {
  final removeBefore = olderThan == null
      ? null
      : clock.now().subtract(olderThan);

  final deletedAttempts = await RateLimitedRequestAttempt.db.deleteWhere(
    session,
    where: (final t) {
      var expression =
          t.domain.equals(config.domain) & t.source.equals(config.source);
      if (removeBefore != null) {
        expression &= t.attemptedAt < removeBefore;
      }
      if (key != null) {
        expression &= t.key.equals(key);
      }
      return expression;
    },
    transaction: transaction,
  );

  return deletedAttempts.length;
}