Class: EM::Voldemort::Connection
- Inherits:
-
Object
- Object
- EM::Voldemort::Connection
- Defined in:
- lib/em-voldemort/connection.rb
Overview
TCP connection to one Voldemort node. The connection can be used to access multiple stores. Automatically reconnects if the connection is lost, but does not automatically retry failed requests (that is the cluster’s job).
Defined Under Namespace
Modules: Handler Classes: FailHandler
Constant Summary collapse
- DEFAULT_PROTOCOL =
Voldemort’s protobuf-based protocol
'pb0'- STATUS_CHECK_PERIOD =
Every 5 seconds, check on the health of the connection
5- REQUEST_TIMEOUT =
If a request takes longer than 5 seconds, close the connection
5
Instance Attribute Summary collapse
-
#health ⇒ Object
readonly
Returns the value of attribute health.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#node_id ⇒ Object
readonly
Returns the value of attribute node_id.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
-
#close ⇒ Object
Waits for the outstanding request (if any) to complete, then gracefully shuts down the connection.
-
#connect ⇒ Object
Establishes a connection to the node.
-
#connection_closed(handler, reason = nil) ⇒ Object
Called by the connection handler when the connection is closed for any reason (closed by us, closed by peer, rejected, timeout etc).
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#send_request(request) ⇒ Object
Sends a request to the node, given as a binary string (not including the request size prefix).
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 18 19 |
# File 'lib/em-voldemort/connection.rb', line 12 def initialize(={}) @host = [:host] or raise ArgumentError, "#{self.class.name} requires :host" @port = [:port] or raise ArgumentError, "#{self.class.name} requires :port" @node_id = [:node_id] @protocol = [:protocol] || DEFAULT_PROTOCOL @logger = [:logger] || Logger.new($stdout) @health = :good end |
Instance Attribute Details
#health ⇒ Object (readonly)
Returns the value of attribute health.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def health @health end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def logger @logger end |
#node_id ⇒ Object (readonly)
Returns the value of attribute node_id.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def node_id @node_id end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def port @port end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
6 7 8 |
# File 'lib/em-voldemort/connection.rb', line 6 def protocol @protocol end |
Instance Method Details
#close ⇒ Object
Waits for the outstanding request (if any) to complete, then gracefully shuts down the connection. Returns a deferrable that succeeds once the connection is closed (never fails).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/em-voldemort/connection.rb', line 38 def close return @closing_deferrable if @closing_deferrable @closing_deferrable = EM::DefaultDeferrable.new @timer.cancel if @handler @handler.close_gracefully else @closing_deferrable.succeed end @handler = FailHandler.new(self) @health = :bad @closing_deferrable end |
#connect ⇒ Object
Establishes a connection to the node. Calling #connect is optional, since it also happens automatically when you start making requests.
23 24 25 |
# File 'lib/em-voldemort/connection.rb', line 23 def connect force_connect unless @handler end |
#connection_closed(handler, reason = nil) ⇒ Object
Called by the connection handler when the connection is closed for any reason (closed by us, closed by peer, rejected, timeout etc). Do not call from application code.
56 57 58 59 60 61 |
# File 'lib/em-voldemort/connection.rb', line 56 def connection_closed(handler, reason=nil) logger.info ["Connection to Voldemort node #{host}:#{port} closed", reason].compact.join(': ') @handler = FailHandler.new(self) if handler.equal? @handler @health = :bad @closing_deferrable.succeed if @closing_deferrable end |
#send_request(request) ⇒ Object
Sends a request to the node, given as a binary string (not including the request size prefix). Establishes a connection if necessary. If a request is already in progress, this request is queued up. Returns a deferrable that succeeds with the node’s response (again without the size prefix), or fails if there was a network-level error.
31 32 33 34 |
# File 'lib/em-voldemort/connection.rb', line 31 def send_request(request) connect @handler.enqueue_request(request) end |