Class: Etcdv3::ConnectionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, *endpoints, namespace, allow_reconnect) ⇒ ConnectionWrapper

Returns a new instance of ConnectionWrapper.



6
7
8
9
10
11
12
13
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def connection
  @connection
end

#endpointsObject

Returns the value of attribute endpoints.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def endpoints
  @endpoints
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def password
  @password
end

#timeoutObject

Returns the value of attribute timeout.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def token
  @token
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 4

def user
  @user
end

Instance Method Details

#authenticate(user, password) ⇒ Object

Authenticate using specified user and password..



57
58
59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 57

def authenticate(user, password)
  @token = handle(:auth, 'generate_token', [user, password])
  @user, @password = user, password
  @connection.(token: @token)
end

#clear_authenticationObject



51
52
53
54
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 51

def clear_authentication
  @user, @password, @token = nil, nil, nil
  @connection.({})
end

#handle(stub, method, method_args = [], retries: 1) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 29

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_endpointObject

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



65
66
67
68
69
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/etcdv3-0.11.5/lib/etcdv3/connection_wrapper.rb', line 65

def rotate_connection_endpoint
  @endpoints.rotate!
  @connection = @endpoints.first
  @connection.(token: @token) if @token
end