Class: OnStomp::Failover::Client

Inherits:
Object
  • Object
show all
Includes:
FailoverConfigurable, FailoverEvents, Interfaces::FrameMethods
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)

Instance Method Summary (collapse)

Methods included from Interfaces::FrameMethods

#abort, #ack, #beat, #begin, #commit, #nack, #send, #subscribe, #unsubscribe

Methods included from FailoverEvents

#after_failover_retry, #before_failover_retry, #bind_client_event, create_client_event_method, #failover_connect_failure, #failover_connected, #failover_lost, #failover_retries_exceeded, #failover_retry, #on_connection_closed, #on_connection_died, #on_connection_established, #on_connection_terminated, #on_failover_connect_failure, #on_failover_connected, #on_failover_lost, #on_failover_retries_exceeded, #trigger_failover_event, #trigger_failover_retry

Methods included from Interfaces::EventManager

#bind_event, #event_callbacks, included, #trigger_event

Methods included from FailoverConfigurable

included

Constructor Details

- (Client) initialize(uris, options = {})

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, options={})
  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 options
  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

- (Object) active_client (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

- (Class) buffer

The class to use when instantiating a new frame buffer. Defaults to Buffers::Written

Returns:

  • (Class)


20
# File 'lib/onstomp/failover/client.rb', line 20

attr_configurable_buffer :buffer

- (Object) client_pool (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

- (Object) connection (readonly)

Returns the value of attribute connection



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def connection
  @connection
end

- (Object) frame_buffer (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

- (Object) hosts (readonly)

Returns the value of attribute hosts



35
36
37
# File 'lib/onstomp/failover/client.rb', line 35

def hosts
  @hosts
end

- (Class) pool

The class to use when instantiating a new #client_pool. Defaults to Pools::RoundRobin

Returns:

  • (Class)


16
# File 'lib/onstomp/failover/client.rb', line 16

attr_configurable_pool :pool

- (true, false) randomize

Whether or not to randomize the #client_pool before connecting through any of its clients. Defaults to false

Returns:

  • (true, false)


33
# File 'lib/onstomp/failover/client.rb', line 33

attr_configurable_bool :randomize, :default => false

- (Fixnum) retry_attempts

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

Returns:

  • (Fixnum)


29
# File 'lib/onstomp/failover/client.rb', line 29

attr_configurable_int :retry_attempts, :default => 0

- (Fixnum) retry_delay

The delay in seconds to wait between connection retries. Defaults to 10

Returns:

  • (Fixnum)


24
# File 'lib/onstomp/failover/client.rb', line 24

attr_configurable_int :retry_delay, :default => 10

- (Object) uri (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

- (self) connect

Connects to one of the clients in the #client_pool

Returns:

  • (self)


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

- (true, ...) connected?

Returns true if there is an #active_client and it is connected.

Returns:

  • (true, false, nil)


69
70
71
# File 'lib/onstomp/failover/client.rb', line 69

def connected?
  active_client && active_client.connected?
end

- (Object) disconnect(*args, &block)

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

- (OnStomp::Components::Frame?) transmit(frame, cbs = {})

Transmits a frame to the #active_client if one exists.

Returns:



75
76
77
# File 'lib/onstomp/failover/client.rb', line 75

def transmit frame, cbs={}
  active_client && active_client.transmit(frame, cbs)
end