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.



51
52
53
# File 'lib/consul/async/consul_template.rb', line 51

def initialize(endpoints_manager)
  @endp_manager = endpoints_manager
end

Instance Method Details

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

Return the coordinates of datacenters



56
57
58
59
60
61
62
63
# File 'lib/consul/async/consul_template.rb', line 56

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



66
67
68
69
70
71
72
73
# File 'lib/consul/async/consul_template.rb', line 66

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/consul/async/consul_template.rb', line 76

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