Class: DNSLoadBalancer

Inherits:
Object
  • Object
show all
Defined in:
lib/dns-loadbalancer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, port, verbose: true) ⇒ DNSLoadBalancer

Returns a new instance of DNSLoadBalancer.



8
9
10
11
12
# File 'lib/dns-loadbalancer.rb', line 8

def initialize(hosts, port, verbose: true)
  @hosts = hosts.split(',')
  @port = port
  @verbose = verbose
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



6
7
8
# File 'lib/dns-loadbalancer.rb', line 6

def hosts
  @hosts
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/dns-loadbalancer.rb', line 6

def port
  @port
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dns-loadbalancer.rb', line 14

def run
  start = Time.now
  ip_addresses = hosts.flat_map do |h|
    result = resolve(h)
    log "Resolved #{h} to IP addresses: #{result}"
    result
  end
  result = find_closest(ip_addresses)
  log "Found fastest IP #{result} in #{(Time.now - start).round(2)}s"
  result
end