lib/consul/async/consul_template.rb in consul-templaterb-1.16.0 vs lib/consul/async/consul_template.rb in consul-templaterb-1.17.0
- old
+ new
@@ -19,12 +19,52 @@
def initialize(cause)
@cause = cause
end
end
+ # Encapsulation of endpoints to get coordinates
+ class Coordinate
+ def initialize(endpoints_manager)
+ @endp_manager = endpoints_manager
+ end
+
+ # Return the coordinates of datacenters
+ def datacenters(dc: nil)
+ path = '/v1/coordinate/datacenters'
+ query_params = {}
+ query_params[:dc] = dc if dc
+ @endp_manager.create_if_missing(path, query_params) { ConsulTemplateNodes.new(ConsulEndpoint.new(@endp_manager.consul_conf, path, true, query_params, '[]')) }
+ end
+
+ # Returns the coordinates for all nodes of DC
+ def nodes(dc: nil)
+ path = '/v1/coordinate/nodes'
+ query_params = {}
+ query_params[:dc] = dc if dc
+ @endp_manager.create_if_missing(path, query_params) { ConsulTemplateNodes.new(ConsulEndpoint.new(@endp_manager.consul_conf, path, true, query_params, '[]')) }
+ end
+
+ # Computes the RTT between 2 nodes
+ 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
+ end
+
class EndPointsManager
- attr_reader :consul_conf, :vault_conf, :net_info, :start_time
+ attr_reader :consul_conf, :vault_conf, :net_info, :start_time, :coordinate
def initialize(consul_configuration, vault_configuration, trim_mode = nil)
@consul_conf = consul_configuration
@vault_conf = vault_configuration
@trim_mode = trim_mode
@endpoints = {}
@@ -46,10 +86,11 @@
'destination' => nil,
'was_rendered_once' => false
},
params: {}
}
+ @coordinate = Coordinate.new(self)
# Setup token renewal
vault_setup_token_renew unless @vault_conf.token.nil? || !@vault_conf.token_renew
end
@@ -244,13 +285,17 @@
if last_result != data
::Consul::Async::Debug.print_info "Write #{Utilities.bytes_to_h data.bytesize} bytes to #{file}, "\
"netinfo=#{@net_info} aka "\
"#{Utilities.bytes_to_h((net_info[:network_bytes] / (Time.now.utc - @start_time)).round(1))}/s ...\r"
tmp_file = "#{file}.tmp"
- File.open(tmp_file, 'w') do |f|
- f.write data
+ begin
+ File.open(tmp_file, 'w') do |f|
+ f.write data
+ end
+ File.rename(tmp_file, file)
+ rescue StandardError => e
+ ::Consul::Async::Debug.puts_error "Failed writting #{Utilities.bytes_to_h data.bytesize} bytes to #{file}: #{e.class}, message: #{e.inspect}"
end
- File.rename(tmp_file, file)
end
[true, data != last_result, data]
end
def terminate