Class: Dnsruby::SelectThread::TransmissionBudget

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

Overview

RFC 9520 section 3.1 limits one resolution to three attempts per server address and transport, including protocol-driven retries.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ TransmissionBudget

Returns a new instance of TransmissionBudget.



236
237
238
239
240
241
# File 'lib/dnsruby/select_thread.rb', line 236

def initialize
  @counts = {}
  @contexts = {}
  @total = 0
  @mutex = Mutex.new
end

Class Method Details

.current ⇒ Object



224
225
226
# File 'lib/dnsruby/select_thread.rb', line 224

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

.with(budget) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/dnsruby/select_thread.rb', line 228

def self.with(budget)
  previous = current
  Thread.current[:dnsruby_transmission_budget] = budget
  yield
ensure
  Thread.current[:dnsruby_transmission_budget] = previous
end

Instance Method Details

#admit(server, port, tcp, question = nil) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/dnsruby/select_thread.rb', line 255

def admit(server, port, tcp, question = nil)
  identity = question && [question.qname.canonical, question.qtype.code, question.qclass.code]
  key = [server.to_s.dup.freeze, port, !!tcp, identity]
  @mutex.synchronize do
    count = @counts[key] || 0
    return false if count >= 3
    if @maximum && @total >= @maximum
      raise @limit_error_class, 'Recursive query transmission limit exceeded'
    end
    @total += 1
    @counts[key] = count + 1
    true
  end
end

#context(key) ⇒ Object

Supporting queries and validator workers already share this object.



244
245
246
# File 'lib/dnsruby/select_thread.rb', line 244

def context(key)
  @mutex.synchronize { @contexts[key] ||= yield }
end

#limit_total!(maximum, error_class) ⇒ Object



248
249
250
251
252
253
# File 'lib/dnsruby/select_thread.rb', line 248

def limit_total!(maximum, error_class)
  @mutex.synchronize do
    @maximum = [@maximum, maximum].compact.min
    @limit_error_class = error_class
  end
end