Class: Toq::Client::Handler
- Includes:
- Protocol
- Defined in:
- lib/toq/client/handler.rb
Overview
Constant Summary collapse
- DEFAULT_TRIES =
Default amount of tries for failed requests.
9
Instance Attribute Summary collapse
- #error ⇒ Exceptions::ConnectionError readonly
-
#status ⇒ Symbol
readonly
-
‘:idle` – Just initialized.
-
Instance Method Summary collapse
-
#close_without_retry ⇒ Object
Closes the connection without triggering a retry operation and sets #status to ‘:closed`.
-
#done? ⇒ Boolean
‘true` when the connection is done, `false` otherwise.
-
#initialize(opts) ⇒ Handler
constructor
Prepares an RPC connection and sets #status to ‘:idle`.
-
#on_close(reason) ⇒ Object
Handles closed connections, cleans up the SSL session, retries (if necessary) and sets #status to ‘:closed`.
-
#receive_response(res) ⇒ Object
Handles responses to RPC requests, calls its callback and sets #status to ‘:done`.
-
#send_request(req) ⇒ Object
Sends an RPC request (i.e. performs an RPC call) and sets #status to ‘:pending`.
Methods included from Protocol
#on_connect, #on_read, #send_message
Constructor Details
#initialize(opts) ⇒ Handler
Prepares an RPC connection and sets #status to ‘:idle`.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/toq/client/handler.rb', line 42 def initialize( opts ) @opts = opts.dup @max_retries = @opts[:max_retries] || DEFAULT_TRIES @client = @opts[:client] @opts[:tries] ||= 0 @tries ||= @opts[:tries] @status = :idle @request = nil end |
Instance Attribute Details
#error ⇒ Exceptions::ConnectionError (readonly)
31 32 33 |
# File 'lib/toq/client/handler.rb', line 31 def error @error end |
#status ⇒ Symbol (readonly)
-
‘:idle` – Just initialized.
-
‘:ready` – A connection has been established.
-
‘:pending` – Sending request and awaiting response.
-
‘:done` – Response received and callback invoked – ready to be reused.
-
‘:closed` – Connection closed.
28 29 30 |
# File 'lib/toq/client/handler.rb', line 28 def status @status end |
Instance Method Details
#close_without_retry ⇒ Object
Closes the connection without triggering a retry operation and sets #status to ‘:closed`.
123 124 125 126 127 |
# File 'lib/toq/client/handler.rb', line 123 def close_without_retry @request = nil @status = :closed close_without_callback end |
#done? ⇒ Boolean
If ‘true`, the connection can be re-used.
Returns ‘true` when the connection is done, `false` otherwise.
117 118 119 |
# File 'lib/toq/client/handler.rb', line 117 def done? @status == :done end |
#on_close(reason) ⇒ Object
Handles closed connections, cleans up the SSL session, retries (if necessary) and sets #status to ‘:closed`.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/toq/client/handler.rb', line 90 def on_close( reason ) if @request # If there is a request and a callback and the callback hasn't yet be # called (i.e. not done) then we got here by error so retry. if @request && @request.callback && !done? if retry? retry_request else @error = e = Exceptions::ConnectionError.new( "Connection closed [#{reason}]" ) @request.callback.call( e ) @client.connection_failed self end return end else @error = reason @client.connection_failed self end close_without_retry end |
#receive_response(res) ⇒ Object
Pushes itself to the client’s connection pool to be re-used.
Handles responses to RPC requests, calls its callback and sets #status to ‘:done`.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/toq/client/handler.rb', line 71 def receive_response( res ) if res.exception? res.obj = Exceptions.from_response( res ) end @request.callback.call( res.obj ) if @request.callback ensure @request = nil # Help the GC out. @error = nil # Help the GC out. @status = :done @opts[:tries] = @tries = 0 @client.push_connection self end |