File: rate_limiter_slide_window.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (76 lines) | stat: -rw-r--r-- 2,824 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_WEBAUTHN_CORE_BROWSER_RATE_LIMITER_SLIDE_WINDOW_H_
#define COMPONENTS_WEBAUTHN_CORE_BROWSER_RATE_LIMITER_SLIDE_WINDOW_H_

#include <cstddef>
#include <queue>

#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"

namespace webauthn {

// Rate limiter implementation of the bucket slide window algorithm.
// It limits the total size of the events by `total_size` and tracks them
// using `bucket_count` buckets comprising `time_window` (thus each bucket
// tracks a period of `time_window / bucket_count`).
// This is a copy of the reporting::RateLimiterSlideWindow class.
class RateLimiterSlideWindow {
 public:
  RateLimiterSlideWindow(size_t total_size,
                         base::TimeDelta time_window,
                         size_t bucket_count);

  RateLimiterSlideWindow(const RateLimiterSlideWindow&) = delete;
  RateLimiterSlideWindow& operator=(const RateLimiterSlideWindow&) = delete;

  ~RateLimiterSlideWindow();

  // If the event is allowed, the method returns `true` and updates state to
  // prepare for the next call. Otherwise returns false.
  bool Acquire(size_t event_size);

 private:
  // Trims `bucket_events_size_` to make sure it fits
  // (now - `bucket_count_` * `time_bucket_`, now] sliding window.
  // Can be called whenever necessary (including `BucketsShift` and `Acquire`).
  void TrimBuckets(base::Time now);

  // Called every `time_bucket_` as long as `bucket_events_size_` is not empty.
  // Shifts buckets by one (drops the oldest bucket, adds a new empty one).
  void BucketsShift();

  SEQUENCE_CHECKER(sequence_checker_);

  // Total size of all events to be accepted in the window.
  const size_t total_size_;

  // Window bucket time period. Must be positive.
  const base::TimeDelta time_bucket_;

  // Window time span is determined as `bucket_count_` * `time_bucket_`.
  // Must be positive.
  const size_t bucket_count_;

  // The events size aggregated over `bucket_count_` * `time_bucket_`.
  size_t current_size_ GUARDED_BY_CONTEXT(sequence_checker_) = 0u;

  // Events sizes per bucket; undefined if empty, otherwise `front()` matches
  // `[start_timestamp_, start_timestamp_ + time_bucket_)` interval.
  std::queue<size_t> bucket_events_size_ GUARDED_BY_CONTEXT(sequence_checker_);

  // First bucket timestamp.
  base::Time start_timestamp_ GUARDED_BY_CONTEXT(sequence_checker_);

  // Weak ptr factory.
  base::WeakPtrFactory<RateLimiterSlideWindow> weak_ptr_factory_{this};
};
}  // namespace webauthn

#endif  // COMPONENTS_WEBAUTHN_CORE_BROWSER_RATE_LIMITER_SLIDE_WINDOW_H_