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

#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

included

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, 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, options
  @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_clientObject (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_poolObject (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

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#frame_bufferObject (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

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#uriObject (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

#bufferClass

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

#connectself

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

#connected?true, ...

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

#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

#poolClass

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

#randomizetrue, false

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

#retry_attemptsFixnum

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

#retry_delayFixnum

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

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

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