Class: Etcdv3::ConnectionWrapper
- 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
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#endpoints ⇒ Object
Returns the value of attribute endpoints.
-
#password ⇒ Object
Returns the value of attribute password.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#authenticate(user, password) ⇒ Object
Authenticate using specified user and password..
- #clear_authentication ⇒ Object
- #handle(stub, method, method_args = [], retries: 1) ⇒ Object
-
#initialize(timeout, *endpoints, namespace, allow_reconnect) ⇒ ConnectionWrapper
constructor
A new instance of ConnectionWrapper.
-
#rotate_connection_endpoint ⇒ Object
Simple failover mechanism that rotates the connection endpoints in an attempt to recover connectivity.
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
#connection ⇒ Object
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 |
#endpoints ⇒ Object
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 |
#password ⇒ Object
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 |
#timeout ⇒ Object
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 |
#token ⇒ Object
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 |
#user ⇒ Object
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_authentication ⇒ Object
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_endpoint ⇒ Object
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 |