class Etcdv3::ConnectionWrapper

Attributes

connection[RW]
endpoints[RW]
password[RW]
timeout[RW]
token[RW]
user[RW]

Public Class Methods

new(timeout, *endpoints, namespace, allow_reconnect) click to toggle source
# File lib/etcdv3/connection_wrapper.rb, line 6
def initialize(timeout, *endpoints, namespace, allow_reconnect)
  @user, @password, @token = nil, nil, nil
  @timeout = timeout
  @namespace = namespace
  @endpoints = endpoints.map{|endpoint| Etcdv3::Connection.new(endpoint, @timeout, @namespace) }
  @allow_reconnect = allow_reconnect
  @connection = @endpoints.first
end

Public Instance Methods

authenticate(user, password) click to toggle source

Authenticate using specified user and password..

# File lib/etcdv3/connection_wrapper.rb, line 51
def authenticate(user, password)
  @token = handle(:auth, 'generate_token', [user, password])
  @user, @password = user, password
  @connection.refresh_metadata(token: @token)
end
clear_authentication() click to toggle source
# File lib/etcdv3/connection_wrapper.rb, line 45
def clear_authentication
  @user, @password, @token = nil, nil, nil
  @connection.refresh_metadata({})
end
handle(stub, method, method_args=[], retries: 1) click to toggle source
# File lib/etcdv3/connection_wrapper.rb, line 23
def handle(stub, method, method_args=[], retries: 1)
  @connection.call(stub, method, method_args)

rescue GRPC::Unavailable, GRPC::Core::CallError
  $stderr.puts("Failed to connect to endpoint '#{@connection.hostname}'")
  if @endpoints.size > 1
    rotate_connection_endpoint
    return retry_or_raise(stub, method, method_args)
  else
    return retry_or_raise(stub, method, method_args)
  end
rescue GRPC::Unauthenticated => exception
  # Regenerate token in the event it expires.
  if exception.details == 'etcdserver: invalid auth token'
    if retries > 0
      authenticate(@user, @password)
      return retry_or_raise(stub, method, method_args, retries: retries - 1)
    end
  end
  raise exception
end
rotate_connection_endpoint() click to toggle source

Simple failover mechanism that rotates the connection endpoints in an attempt to recover connectivity.

# File lib/etcdv3/connection_wrapper.rb, line 59
def rotate_connection_endpoint
  @endpoints.rotate!
  @connection = @endpoints.first
  @connection.refresh_metadata(token: @token) if @token
end

Private Instance Methods

retry_or_raise(*args) click to toggle source
# File lib/etcdv3/connection_wrapper.rb, line 15
        def retry_or_raise(*args)
  if @allow_reconnect
    handle(*args)
  else
    raise
  end
end