Class: Consul::Async::Coordinate

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/async/consul_template.rb

Overview

Encapsulation of endpoints to get coordinates

Instance Method Summary collapse

Constructor Details

#initialize(endpoints_manager) ⇒ Coordinate

Returns a new instance of Coordinate.



47
48
49
# File 'lib/consul/async/consul_template.rb', line 47

def initialize(endpoints_manager)
  @endp_manager = endpoints_manager
end

Instance Method Details

#datacenters(dc: nil, agent: nil) ⇒ Object

Return the coordinates of datacenters



52
53
54
55
56
57
58
59
# 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) ⇒ Object

Returns the coordinates for all nodes of DC



62
63
64
65
66
67
68
69
# 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) ⇒ Object

Computes the RTT between 2 nodes



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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