Module: OnStomp::Connections::Heartbeating

Included in:
Stomp_1_1
Defined in:
lib/onstomp/connections/heartbeating.rb

Overview

Mixin for connections to include heartbeating functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heartbeating[Fixnum, Fixnum] (readonly)

A pair of integers indicating the maximum number of milliseconds the client and broker can go without transmitting data respectively. If either value is 0, the respective heartbeating is not enabled.

Returns:

  • ([Fixnum, Fixnum])


9
10
11
# File 'lib/onstomp/connections/heartbeating.rb', line 9

def heartbeating
  @heartbeating
end

Instance Method Details

#broker_pulse?true, false

Returns true if broker-side heartbeating is disabled, or #duration_since_received has not exceeded #heartbeat_broker_limit

Returns:

  • (true, false)


79
80
81
# File 'lib/onstomp/connections/heartbeating.rb', line 79

def broker_pulse?
  heartbeat_broker_limit == 0 || duration_since_received <= heartbeat_broker_limit
end

#client_pulse?true, false

Returns true if client-side heartbeating is disabled, or #duration_since_transmitted has not exceeded #heartbeat_client_limit

Returns:

  • (true, false)


72
73
74
# File 'lib/onstomp/connections/heartbeating.rb', line 72

def client_pulse?
  heartbeat_client_limit == 0 || duration_since_transmitted <= heartbeat_client_limit
end

#configure_heartbeating(client_beats, broker_beats) ⇒ Object

Configures heartbeating strategy by taking the maximum timeout for clients and brokers. If either pair contains a zero, the respective beating is disabled.

Parameters:

  • client_beats ([Fixnum, Fixnum])
  • broker_beats ([Fixnum, Fixnum])


16
17
18
19
20
21
# File 'lib/onstomp/connections/heartbeating.rb', line 16

def configure_heartbeating client_beats, broker_beats
  c_x, c_y = client_beats
  s_x, s_y = broker_beats
  @heartbeating = [ (c_x == 0||s_y == 0 ? 0 : [c_x,s_y].max), 
    (c_y == 0||s_x == 0 ? 0 : [c_y,s_x].max) ]
end

#duration_since_receivedFixnum?

Number of milliseconds since data was last received from the broker or nil if no data has been received when the method is called.

Returns:

  • (Fixnum, nil)


65
66
67
# File 'lib/onstomp/connections/heartbeating.rb', line 65

def duration_since_received
  last_received_at && ((Time.now - last_received_at)*1000).to_i
end

#duration_since_transmittedFixnum?

Number of milliseconds since data was last transmitted to the broker or nil if no data has been transmitted when the method is called.

Returns:

  • (Fixnum, nil)


58
59
60
# File 'lib/onstomp/connections/heartbeating.rb', line 58

def duration_since_transmitted
  last_transmitted_at && ((Time.now - last_transmitted_at)*1000).to_i
end

#heartbeat_broker_limitFixnum

Maximum number of milliseconds allowed between bytes being sent from the broker, or 0 if there is no limit. This method will add a 10% margin of error to the timeout determined from heartbeat negotiation to allow a little slack before a connection is deemed dead.

Returns:

  • (Fixnum)


48
49
50
51
52
53
# File 'lib/onstomp/connections/heartbeating.rb', line 48

def heartbeat_broker_limit
  unless defined?(@heartbeat_broker_limit)
    @heartbeat_broker_limit = heartbeating[1] > 0 ? (1.1 * heartbeating[1]) : 0
  end
  @heartbeat_broker_limit
end

#heartbeat_client_limitFixnum

Maximum number of milliseconds allowed between bytes being sent by the client, or 0 if there is no limit. This method will add a 10% margin of error to the timeout determined from heartbeat negotiation to allow a little slack before a connection is deemed dead.

Returns:

  • (Fixnum)


36
37
38
39
40
41
# File 'lib/onstomp/connections/heartbeating.rb', line 36

def heartbeat_client_limit
  unless defined?(@heartbeat_client_limit)
    @heartbeat_client_limit = heartbeating[0] > 0 ? (1.1 * heartbeating[0]) : 0
  end
  @heartbeat_client_limit
end

#pulse?true, false

Returns true if both the client and broker are transmitting data in accordance with the heartbeating strategy. If this method returns false, the connection is effectively dead and should be closed

Returns:

  • (true, false)


27
28
29
# File 'lib/onstomp/connections/heartbeating.rb', line 27

def pulse?
  client_pulse? && broker_pulse?
end