class Consul::Async::Coordinate

Encapsulation of endpoints to get coordinates

Public Class Methods

new(endpoints_manager) click to toggle source
# File lib/consul/async/consul_template.rb, line 47
def initialize(endpoints_manager)
  @endp_manager = endpoints_manager
end

Public Instance Methods

datacenters(dc: nil, agent: nil) click to toggle source

Return the coordinates of datacenters

# File lib/consul/async/consul_template.rb, line 52
def datacenters(dc: nil, agent: nil)
  path = '/v1/coordinate/datacenters'
  query_params = {}
  query_params[:dc] = dc if dc
  @endp_manager.create_if_missing(path, query_params, agent: agent) do
    ConsulTemplateNodes.new(ConsulEndpoint.new(@endp_manager.consul_conf, path, true, query_params, '[]', agent))
  end
end
nodes(dc: nil, agent: nil) click to toggle source

Returns the coordinates for all nodes of DC

# File lib/consul/async/consul_template.rb, line 62
def nodes(dc: nil, agent: nil)
  path = '/v1/coordinate/nodes'
  query_params = {}
  query_params[:dc] = dc if dc
  @endp_manager.create_if_missing(path, query_params, agent: agent) do
    ConsulTemplateNodes.new(ConsulEndpoint.new(@endp_manager.consul_conf, path, true, query_params, '[]', agent))
  end
end
rtt(a, b) click to toggle source

Computes the RTT between 2 nodes

# File lib/consul/async/consul_template.rb, line 72
def rtt(a, b)
  # Calculate the Euclidean distance plus the heights.
  a_vec = a['Vec']
  b_vec = b['Vec']
  sumsq = 0.0
  a_vec.count.times do |i|
    diff = a_vec[i] - b_vec[i]
    sumsq += diff * diff
  end
  rtt = Math.sqrt(sumsq) + a['Height'] + b['Height']

  adjusted = rtt + a['Adjustment'] + b['Adjustment']
  rtt = adjusted if adjusted.positive?
  rtt
end