Class: Dnsruby::QueryControl

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

Overview

A deadline and cancellation signal shared by related lookups. This carries no DNSSEC policy; the caller decides how a stopped operation is reported.

Defined Under Namespace

Classes: Cancelled, DeadlineExceeded, Queue, Stopped

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deadline: nil, parent: nil) ⇒ QueryControl

Returns a new instance of QueryControl.



80
81
82
83
84
85
86
# File 'lib/dnsruby/query_control.rb', line 80

def initialize(deadline: nil, parent: nil)
  @parent = parent
  @deadline = [deadline, parent&.deadline].compact.min
  @cancelled = false
  @mutex = Mutex.new
  @waiters = []
end

Instance Attribute Details

#deadline ⇒ Object (readonly)

Returns the value of attribute deadline.



63
64
65
# File 'lib/dnsruby/query_control.rb', line 63

def deadline
  @deadline
end

Class Method Details

.current ⇒ Object



65
66
67
# File 'lib/dnsruby/query_control.rb', line 65

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

.with(control) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/dnsruby/query_control.rb', line 69

def self.with(control)
  previous = current
  Thread.current[:dnsruby_query_control] = control
  control.check!
  result = yield
  control.check!
  result
ensure
  Thread.current[:dnsruby_query_control] = previous
end

Instance Method Details

#add_waiter(queue) ⇒ Object



104
105
106
107
# File 'lib/dnsruby/query_control.rb', line 104

def add_waiter(queue)
  @parent&.add_waiter(queue)
  @mutex.synchronize { @waiters << queue }
end

#cancel ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/dnsruby/query_control.rb', line 88

def cancel
  waiters = @mutex.synchronize do
    @cancelled = true
    @waiters.dup
  end
  waiters.each(&:wake_waiters)
  true
end

#check! ⇒ Object

Raises:



97
98
99
100
101
102
# File 'lib/dnsruby/query_control.rb', line 97

def check!
  @parent&.check!
  raise Cancelled, 'Query cancelled' if @mutex.synchronize { @cancelled }
  raise DeadlineExceeded, 'Query deadline exceeded' if @deadline && TransportClock.now >= @deadline
  true
end

#pop(queue) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dnsruby/query_control.rb', line 117

def pop(queue)
  return queue.pop_with_control(self) if queue.is_a?(Queue)

  # Preserve support for caller-supplied ordinary queues, whose producers
  # cannot notify this control. Internal lookups use the queue above.
  loop do
    check!
    begin
      return queue.pop(true)
    rescue ThreadError
      sleep(@deadline ? (@deadline - TransportClock.now).clamp(0, 0.01) : 0.01)
    end
  end
end

#remove_waiter(queue) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/dnsruby/query_control.rb', line 109

def remove_waiter(queue)
  @mutex.synchronize do
    index = @waiters.index(queue)
    @waiters.delete_at(index) if index
  end
  @parent&.remove_waiter(queue)
end