Class: Srvy::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/srvy/result.rb

Constant Summary collapse

EMPTY_RESULT_TTL =
10

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(created_at, hosts) ⇒ Result

Returns a new instance of Result.



15
16
17
18
# File 'lib/srvy/result.rb', line 15

def initialize(created_at, hosts)
  @created_at = created_at
  @hosts      = hosts
end

Class Method Details

.from_dns(dns_result) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/srvy/result.rb', line 5

def self.from_dns(dns_result)
  # for each SRV record
  srvs = dns_result.answer.select{|rr| rr.is_a?(Net::DNS::RR::SRV) }
  hosts = srvs.map do |srv|
    Srvy::Host.new(srv.host, srv.port, srv.weight, srv.priority, srv.ttl)
  end

  new(Time.now, hosts)
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/srvy/result.rb', line 40

def expired?
  now = Time.now
  min_ttl = @hosts.map(&:ttl).min
  min_ttl ||= EMPTY_RESULT_TTL # in case there are no hosts
  @created_at + min_ttl < now
end

#get_allObject



36
37
38
# File 'lib/srvy/result.rb', line 36

def get_all
  @hosts
end

#get_manyObject



32
33
34
# File 'lib/srvy/result.rb', line 32

def get_many
  best_priority_hosts_by_weight
end

#get_singleObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/srvy/result.rb', line 20

def get_single
  return nil if @hosts.empty?

  roll = rand(best_priority_cumulative_weight)
  acc = 0

  best_priority_hosts_by_weight.each do |host|
    acc += host.weight
    return host if roll < acc
  end
end