Class: Dnsruby::ValidationBudget

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsruby/validation_budget.rb

Overview

One budget counts DNSSEC work across nested validation calls. Supporting lookups share its deadline and cancellation signal through QueryControl.

Constant Summary collapse

DEFAULT_LIMITS =
{
  max_signature_verifications: 64,
  max_work: 100_000,
  timeout: 5.0
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deadline: nil, limits: Dnssec.validation_limits, parent: nil) ⇒ ValidationBudget

Returns a new instance of ValidationBudget.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dnsruby/validation_budget.rb', line 61

def initialize(deadline: nil, limits: Dnssec.validation_limits, parent: nil)
  # A plain token can be retained with learned records without retaining
  # the budget, resolver control, or the rest of a validation operation.
  if parent
    @transaction = parent.transaction
    @limits = parent.limits
    @state = parent.state
    @control = QueryControl.new(deadline: deadline, parent: parent.control)
  else
    @transaction = Object.new.freeze
    @limits = limits
    deadline = [deadline, TransportClock.now + limits[:timeout]].compact.min
    @control = QueryControl.new(deadline: deadline, parent: QueryControl.current)
    @state = { mutex: Mutex.new, signature_verifications: 0, work: 0 }
  end
end

Instance Attribute Details

#control ⇒ Object (readonly)

Returns the value of attribute control.



31
32
33
# File 'lib/dnsruby/validation_budget.rb', line 31

def control
  @control
end

#transaction ⇒ Object (readonly)

Returns the value of attribute transaction.



31
32
33
# File 'lib/dnsruby/validation_budget.rb', line 31

def transaction
  @transaction
end

Class Method Details

.abort?(error) ⇒ Boolean

Supporting lookup configuration has legacy catch-all fallbacks. None may convert an enclosing operation's exhaustion into an ordinary failure.

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/dnsruby/validation_budget.rb', line 39

def self.abort?(error)
  error.is_a?(Abort) || error.is_a?(ValidationLimitError) ||
    error.is_a?(QueryControl::Stopped) || error.is_a?(RecursionLimitError)
end

.current ⇒ Object



33
34
35
# File 'lib/dnsruby/validation_budget.rb', line 33

def self.current
  Thread.current[:dnsruby_validation_budget]
end

.public_error(error) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/dnsruby/validation_budget.rb', line 104

def self.public_error(error)
  public_error = case error
  when Abort then error.public_error
  when QueryControl::Cancelled then ValidationCancelled.new('DNSSEC validation cancelled')
  else ValidationLimitError.new('DNSSEC validation deadline exceeded')
  end
  public_error.set_backtrace(error.backtrace)
  public_error
end

.with(budget = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dnsruby/validation_budget.rb', line 44

def self.with(budget = nil)
  previous = current
  budget = previous || budget || new
  Thread.current[:dnsruby_validation_budget] = budget
  QueryControl.with(budget.control) do
    budget.check!
    result = yield budget
    budget.check!
    result
  end
rescue Abort, QueryControl::Stopped => error
  raise if previous
  raise public_error(error), cause: error
ensure
  Thread.current[:dnsruby_validation_budget] = previous
end

Instance Method Details

#cancel ⇒ Object



88
89
90
# File 'lib/dnsruby/validation_budget.rb', line 88

def cancel
  @control.cancel
end

#check!(work = 0) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dnsruby/validation_budget.rb', line 92

def check!(work = 0)
  @control.check!
  @state[:mutex].synchronize do
    @state[:work] += work
    check_counts!
  end
  true
rescue QueryControl::Stopped => error
  raise if self.class.current
  raise self.class.public_error(error), cause: error
end

#child(deadline: nil) ⇒ Object

A selector cancels its completed hop. Keep that signal local while all hops and parallel address workers count against the lookup's one budget.



80
81
82
# File 'lib/dnsruby/validation_budget.rb', line 80

def child(deadline: nil)
  self.class.new(deadline: deadline, parent: self)
end

#deadline ⇒ Object



84
85
86
# File 'lib/dnsruby/validation_budget.rb', line 84

def deadline
  @control.deadline
end

#signature! ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/dnsruby/validation_budget.rb', line 130

def signature!
  check!
  @state[:mutex].synchronize do
    @state[:signature_verifications] += 1
    check_counts!
  end
  true
end