Class: Dnsruby::DSCache

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

Overview

Learned DS/DLV RRsets are evicted as a whole. The record limit bounds both retained records and index entries, even when an RRset contains many DSes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_records:) ⇒ DSCache

:nodoc: all



175
176
177
178
179
180
181
182
183
184
# File 'lib/dnsruby/key_cache.rb', line 175

def initialize(max_records:)
  if max_records && (!max_records.is_a?(Integer) || max_records <= 0)
    raise ArgumentError, 'max_records must be a positive integer'
  end
  @max_records = max_records
  @record_count = 0
  @sets = {}
  @candidate_index = {}
  @name_index = {}
end

Class Method Details

.candidate_identity(record) ⇒ Object



190
191
192
193
194
# File 'lib/dnsruby/key_cache.rb', line 190

def self.candidate_identity(record)
  # DLV owners belong to a lookaside registry, not the DNSKEY's zone.
  [record.type == Types.DLV ? nil : record.name.canonical.freeze,
    record.klass.code, record.algorithm.code, record.key_tag].freeze
end

.set_identity(rrset) ⇒ Object



186
187
188
# File 'lib/dnsruby/key_cache.rb', line 186

def self.set_identity(rrset)
  [rrset.name.canonical.freeze, rrset.klass.code, rrset.type.code].freeze
end

Instance Method Details

#add(rrset, expiration) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dnsruby/key_cache.rb', line 196

def add(rrset, expiration)
  identity = self.class.set_identity(rrset)
  delete(identity)
  count = rrset.rrs.length + rrset.sigs.length
  return if expiration <= Time.now.to_i || (@max_records && count > @max_records)
  remove_expired if @max_records && @record_count + count > @max_records
  delete(@sets.each_key.first) while @max_records && @record_count + count > @max_records
  candidates = rrset.rrs.map { |record| self.class.candidate_identity(record) }.uniq
  @sets[identity] = [expiration, rrset, count, candidates]
  @record_count += count
  (@name_index[identity.first] ||= {})[identity] = true
  candidates.each { |candidate| (@candidate_index[candidate] ||= {})[identity] = true }
end

#matching_rrsets(keys) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dnsruby/key_cache.rb', line 210

def matching_rrsets(keys)
  identities = keys.flat_map do |key|
    [key.name.canonical, nil].flat_map do |owner|
      (@candidate_index[[owner, key.klass.code, key.algorithm.code, key.key_tag]] || {}).keys
    end
  end.uniq
  now = Time.now.to_i
  identities.filter_map do |identity|
    expiration, rrset = @sets.fetch(identity)
    if expiration <= now
      delete(identity)
      nil
    else
      rrset
    end
  end
end

#rrsets ⇒ Object



241
242
243
244
# File 'lib/dnsruby/key_cache.rb', line 241

def rrsets
  remove_expired
  @sets.values.map { |entry| entry[1] }
end

#rrsets_for_name(name) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dnsruby/key_cache.rb', line 228

def rrsets_for_name(name)
  now = Time.now.to_i
  (@name_index[name.canonical] || {}).keys.filter_map do |identity|
    expiration, rrset = @sets.fetch(identity)
    if expiration <= now
      delete(identity)
      nil
    else
      rrset
    end
  end
end