Class: Fluent::Plugin::JsonSizeLimit::ProcessingGuard

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/json_size_limit/processing_guard.rb

Overview

Cooperative per-record limits. Clock checks are sampled to keep the hot path cheap, while explicit checkpoints surround potentially large work.

Constant Summary collapse

MONOTONIC_TIME =
-> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
CHECK_INTERVAL =
256

Instance Method Summary collapse

Constructor Details

#initialize(timeout:, max_nodes:, max_input_bytes:, max_nesting:, clock: MONOTONIC_TIME, check_interval: CHECK_INTERVAL) ⇒ ProcessingGuard

Returns a new instance of ProcessingGuard.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 14

def initialize(timeout:, max_nodes:, max_input_bytes:, max_nesting:, clock: MONOTONIC_TIME,
  check_interval: CHECK_INTERVAL)
  validate_options!(timeout, max_nodes, max_input_bytes, max_nesting, clock, check_interval)

  @clock = clock
  @deadline = clock.call + timeout
  @max_nodes = max_nodes
  @max_input_bytes = max_input_bytes
  @max_nesting = max_nesting
  @check_interval = check_interval
  reset_phase!
end

Instance Method Details

#account_input_bytes!(bytes) ⇒ Object



37
38
39
40
41
42
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 37

def (bytes)
  return unless @track_input

  @input_bytes += bytes
  raise ProcessingLimitError, :max_input_size if @input_bytes > @max_input_bytes
end

#checkpoint!Object



48
49
50
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 48

def checkpoint!
  raise ProcessingLimitError, :processing_timeout if @clock.call > @deadline
end

#enforce_input_bytes!(bytes) ⇒ Object



44
45
46
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 44

def enforce_input_bytes!(bytes)
  raise ProcessingLimitError, :max_input_size if bytes > @max_input_bytes
end

#reset_phase!(track_input: true) ⇒ Object



52
53
54
55
56
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 52

def reset_phase!(track_input: true)
  @nodes = 0
  @input_bytes = 0
  @track_input = track_input
end

#visit!(depth, input_bytes = 0) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/fluent/plugin/json_size_limit/processing_guard.rb', line 27

def visit!(depth, input_bytes = 0)
  raise ProcessingLimitError, :max_nesting if depth > @max_nesting

  @nodes += 1
  raise ProcessingLimitError, :max_record_nodes if @nodes > @max_nodes

  (input_bytes)
  checkpoint! if (@nodes % @check_interval).zero?
end