Class: OnStomp::Failover::Client
- Inherits:
-
Object
- Object
- OnStomp::Failover::Client
- Defined in:
- lib/onstomp/failover/client.rb
Overview
A failover client that wraps multiple clients and maintains a connection to one of these clients. Frames are sent to the currently connected client. If the connection is lost, a failover client will automatically reconnect to another client in the pool, re-transmit any necessary frames and resume operation.
Instance Attribute Summary collapse
-
#active_client ⇒ Object
readonly
Returns the value of attribute active_client.
-
#client_pool ⇒ Object
readonly
Returns the value of attribute client_pool.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#frame_buffer ⇒ Object
readonly
Returns the value of attribute frame_buffer.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#buffer ⇒ Class
The class to use when instantiating a new frame buffer.
-
#connect ⇒ self
Connects to one of the clients in the #client_pool.
-
#connected? ⇒ true, ...
Returns true if there is an #active_client and it is connected.
-
#disconnect(*args, &block) ⇒ Object
Ensures that a connection is properly established, then invokes disconnect on the #active_client.
-
#initialize(uris, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#pool ⇒ Class
The class to use when instantiating a new #client_pool.
-
#randomize ⇒ true, false
Whether or not to randomize the #client_pool before connecting through any of its clients.
-
#retry_attempts ⇒ Fixnum
The maximum number of times to retry connecting during a reconnect loop.
-
#retry_delay ⇒ Fixnum
The delay in seconds to wait between connection retries.
-
#transmit(frame, cbs = {}) ⇒ OnStomp::Components::Frame?
Transmits a frame to the #active_client if one exists.
Methods included from Interfaces::FrameMethods
#abort, #ack, #beat, #begin, #commit, #nack, #send, #subscribe, #unsubscribe
Methods included from FailoverEvents
#bind_client_event, create_client_event_method, #on_connection_closed, #on_connection_died, #on_connection_established, #on_connection_terminated, #trigger_failover_event, #trigger_failover_retry
Methods included from Interfaces::EventManager
#bind_event, #event_callbacks, included, #trigger_event
Methods included from FailoverConfigurable
Constructor Details
#initialize(uris, options = {}) ⇒ Client
Returns a new instance of Client.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/onstomp/failover/client.rb', line 38 def initialize(uris, ={}) if uris.is_a? Array @uri = OnStomp::Failover::URI::FAILOVER.new [], nil @hosts = uris else @uri = OnStomp::Failover::URI::FAILOVER.parse uris @hosts = @uri.failover_uris end @client_mutex = Mutex.new configure_configurable create_client_pool hosts, @active_client = nil @connection = nil @frame_buffer = buffer.new self @disconnecting = false retry_ready = false @retry_thread = Thread.new do until @disconnecting retry_ready = true Thread.stop @client_mutex.synchronize { reconnect unless @disconnecting } end end Thread.pass until retry_ready && @retry_thread.stop? end |
Instance Attribute Details
#active_client ⇒ Object (readonly)
Returns the value of attribute active_client.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def active_client @active_client end |
#client_pool ⇒ Object (readonly)
Returns the value of attribute client_pool.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def client_pool @client_pool end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def connection @connection end |
#frame_buffer ⇒ Object (readonly)
Returns the value of attribute frame_buffer.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def frame_buffer @frame_buffer end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def hosts @hosts end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
35 36 37 |
# File 'lib/onstomp/failover/client.rb', line 35 def uri @uri end |
Instance Method Details
#buffer ⇒ Class
The class to use when instantiating a new frame buffer. Defaults to Buffers::Written
20 |
# File 'lib/onstomp/failover/client.rb', line 20 attr_configurable_buffer :buffer |
#connect ⇒ self
Connects to one of the clients in the #client_pool
81 82 83 84 85 86 87 |
# File 'lib/onstomp/failover/client.rb', line 81 def connect @disconnecting = false unless @client_mutex.synchronize { reconnect } raise OnStomp::Failover::MaximumRetriesExceededError end self end |
#connected? ⇒ true, ...
Returns true if there is an #active_client and it is connected.
69 70 71 |
# File 'lib/onstomp/failover/client.rb', line 69 def connected? active_client && active_client.connected? end |
#disconnect(*args, &block) ⇒ Object
Ensures that a connection is properly established, then invokes disconnect on the #active_client
91 92 93 94 95 96 97 98 99 |
# File 'lib/onstomp/failover/client.rb', line 91 def disconnect *args, &block if active_client Thread.pass until connected? || @failed @client_mutex.synchronize do @disconnecting = true active_client.disconnect *args, &block if connected? end end end |
#pool ⇒ Class
The class to use when instantiating a new #client_pool. Defaults to Pools::RoundRobin
16 |
# File 'lib/onstomp/failover/client.rb', line 16 attr_configurable_pool :pool |
#randomize ⇒ true, false
Whether or not to randomize the #client_pool before connecting through
any of its clients. Defaults to false
33 |
# File 'lib/onstomp/failover/client.rb', line 33 attr_configurable_bool :randomize, :default => false |
#retry_attempts ⇒ Fixnum
The maximum number of times to retry connecting during a reconnect
loop. A non-positive number will force the failover client to try to
reconnect indefinitely. Defaults to 0
29 |
# File 'lib/onstomp/failover/client.rb', line 29 attr_configurable_int :retry_attempts, :default => 0 |
#retry_delay ⇒ Fixnum
The delay in seconds to wait between connection retries.
Defaults to 10
24 |
# File 'lib/onstomp/failover/client.rb', line 24 attr_configurable_int :retry_delay, :default => 10 |
#transmit(frame, cbs = {}) ⇒ OnStomp::Components::Frame?
Transmits a frame to the #active_client if one exists.
75 76 77 |
# File 'lib/onstomp/failover/client.rb', line 75 def transmit frame, cbs={} active_client && active_client.transmit(frame, cbs) end |