Class: Labkit::RateLimit::BlockSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/rate_limit/block_summary.rb

Overview

Counts blocks per (endpoint_id, rule) in this process and logs the counts every interval. A line per block grows with an attack, so lines here grow only with the number of distinct pairs this process blocked, and never past max_pairs + 1.

One line per pair rather than one line holding a map: map keys become index fields, one per endpoint name. The fields are rate_limit_rule and block_count, not rule and count, so they cannot clash with another line's type for a common name in a shared index.

The caller owns the thread: run #call on one, and #stop it on shutdown so the last counts are flushed.

Constant Summary collapse

MESSAGE =
"rate_limit_block_summary"
OTHER =
"other"
OTHER_KEY =
[OTHER, OTHER].freeze
DEFAULT_INTERVAL_SECONDS =
300
DEFAULT_MAX_PAIRS =
10

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, interval: DEFAULT_INTERVAL_SECONDS, max_pairs: DEFAULT_MAX_PAIRS) ⇒ BlockSummary

Returns a new instance of BlockSummary.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/labkit/rate_limit/block_summary.rb', line 24

def initialize(logger: nil, interval: DEFAULT_INTERVAL_SECONDS, max_pairs: DEFAULT_MAX_PAIRS)
  # A zero interval would make #call spin without waiting.
  raise ArgumentError, "interval must be a positive Numeric, got #{interval.inspect}" unless interval.is_a?(Numeric) && interval.positive?

  raise ArgumentError, "max_pairs must be a positive Integer, got #{max_pairs.inspect}" unless max_pairs.is_a?(Integer) && max_pairs.positive?

  @logger = logger || RateLimit.config.logger || Labkit::Logging::JsonLogger.new($stdout)
  @interval = interval
  @max_pairs = max_pairs
  @counts = {}
  @mutex = Mutex.new
  @stop_signal = Thread::Queue.new
end

Instance Method Details

#add(endpoint_id:, rule:, feature_category: nil) ⇒ Object

Keyed on the pair alone, so feature_category never adds a line. The first non-nil one passed for a pair is kept. Capped here rather than at flush, so the hash cannot grow during an attack.



41
42
43
44
45
46
47
48
49
50
# File 'lib/labkit/rate_limit/block_summary.rb', line 41

def add(endpoint_id:, rule:, feature_category: nil)
  key = [endpoint_id, rule]

  @mutex.synchronize do
    key = OTHER_KEY if !@counts.key?(key) && pairs_full?
    entry = (@counts[key] ||= [0, nil])
    entry[0] += 1
    entry[1] ||= feature_category unless key == OTHER_KEY
  end
end

#call ⇒ Object

Waits on a queue rather than sleeping, so a stop that lands mid-flush is still seen and the final flush is never skipped. The first wait is random, so workers booted together do not all flush at once.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/labkit/rate_limit/block_summary.rb', line 55

def call
  timeout = rand * @interval

  loop do
    stopping = @stop_signal.pop(timeout: timeout)
    flush
    break if stopping

    timeout = @interval
  end
end

#flush ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/labkit/rate_limit/block_summary.rb', line 73

def flush
  drained = {}
  logged = 0

  drained = @mutex.synchronize do
    counts = @counts
    @counts = {}
    counts
  end

  drained.each do |(endpoint_id, rule), (count, feature_category)|
    @logger.info(line(endpoint_id, rule, feature_category, count))
    logged += 1
  end
rescue StandardError => e
  log_failure(e, drained.size - logged)
end

#stop ⇒ Object

Only signals #call, which runs the final flush. Without a thread in #call, nothing is flushed.



69
70
71
# File 'lib/labkit/rate_limit/block_summary.rb', line 69

def stop
  @stop_signal << true
end