File: access_rate_limiter.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 (85 lines) | stat: -rw-r--r-- 3,590 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
77
78
79
80
81
82
83
84
85
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_FEEDBACK_PRIVATE_ACCESS_RATE_LIMITER_H_
#define EXTENSIONS_BROWSER_API_FEEDBACK_PRIVATE_ACCESS_RATE_LIMITER_H_

#include "base/memory/raw_ptr.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"

namespace extensions {

// Keeps track of how frequently a resource may be accessed. Uses a rechargeable
// time counter to keep track. Each time an access is attempted, if the counter
// has more than `recharge_period_`, it will be a successful access, and then
// `recharge_period_` is deducted from it. If the counter has less than
// `recharge_period_`, it will not be a successful access.
//
// The counter is automatically charged as time goes on. It is updated based on
// the current time right before each access attempt. The counter maxes out at
// |recharge_period_ * max_num_accesses|.
//
// The counter starts off at the max level, so that at the beginning, the number
// of successful accesses that can be immediately attempted is
// `max_num_accesses`.
//
// For example, suppose max_num_accesses=3 and recharge_period=100 ms. The
// following sequence of events could happen:
// - AttemptAccess() immediately gets called 3 times. It returns true 3 times.
// - Any further access within the next 100 ms will return false.
// - After 100 ms, call AttemptAccess() again. It will return true.
// - Any further access within the next 100 ms will return false.
// - After 200 ms, call AttemptAccess() again. It will return true. It can be
//   called successfully one more time at this point, but we won't call it yet.
// - After 100 ms, call AttemptAccess() twice. It will return true both times,
//   because the meter will have charged enough for two more accessess.
//
// The counter can be charged partially. e.g. it can accrue 50 ms from one
// attempted access and 50 ms from the next, to get a full 100 ms and thus one
// more successful access.
//
// See the unit tests for more info about the behavior.
class AccessRateLimiter {
 public:
  AccessRateLimiter(size_t max_num_accesses,
                    const base::TimeDelta& recharge_period,
                    const base::TickClock* tick_clock);

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

  ~AccessRateLimiter();

  // Attempt to access a resource. Will update `counter_` based on how much time
  // has elapsed since the last access attempt.
  // - Deducts `recharge_period_` from `counter_` if possible and returns true
  //   to indicate a successful access attempt.
  // - If `counter_` < `recharge_period_`, returns false to indicate a failed
  //   access attempt.
  bool AttemptAccess();

 private:
  // Can only accrue up to this many available accesses.
  const size_t max_num_accesses_;

  // Time when AttemptAccess() was last called.
  base::TimeTicks last_access_time_;

  // The time it takes to accumulate one extra available access. If this is set
  // 0, accesses is allowed.
  const base::TimeDelta recharge_period_;

  // Keeps track of how many available accesses there are. There is one for each
  // unit of `recharge_period_`.
  base::TimeDelta counter_;

  // Points to a timer clock implementation for keeping track of access times.
  raw_ptr<const base::TickClock> tick_clock_;
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_FEEDBACK_PRIVATE_ACCESS_RATE_LIMITER_H_