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.
-
#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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/onstomp/failover/client.rb', line 37 def initialize(uris, ={}) if uris.is_a?(Array) uris = "failover:(#{uris.map { |u| u.to_s }.join(',')})" end @client_mutex = Mutex.new @uri = URI.parse(uris) configure_configurable create_client_pool @active_client = nil @connection = nil @frame_buffer = buffer.new self @disconnecting = false @client_ready = false 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 |
#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
67 68 69 70 71 72 73 |
# File 'lib/onstomp/failover/client.rb', line 67 def connect @disconnecting = false unless reconnect raise OnStomp::Failover::MaximumRetriesExceededError end self end |
#connected? ⇒ true, ...
Returns true if there is an #active_client and it is connected.
55 56 57 |
# File 'lib/onstomp/failover/client.rb', line 55 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
77 78 79 80 81 82 |
# File 'lib/onstomp/failover/client.rb', line 77 def disconnect *args, &block return unless active_client @disconnecting = true Thread.pass until @client_ready active_client.disconnect *args, &block 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.
61 62 63 |
# File 'lib/onstomp/failover/client.rb', line 61 def transmit frame, cbs={} active_client && active_client.transmit(frame, cbs) end |