Class: Fleck::HostRating

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/fleck/utilities/host_rating.rb

Overview

‘HostRating` class allows to test host latency on a regular basis and to compare the latency with other hosts.

Constant Summary collapse

CONN_TIMEOUT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_error, #logger

Constructor Details

#initialize(host: 'localhost', port: 5672, refresh_rate: 30_000, period: 300_000) ⇒ HostRating

Returns a new instance of HostRating.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fleck/utilities/host_rating.rb', line 16

def initialize(host: 'localhost', port: 5672, refresh_rate: 30_000, period: 300_000)
  @host         = host
  @port         = port
  @refresh_rate = refresh_rate
  @period       = period

  # metrics
  @reachable  = false
  @avg        = 0
  @updated_at = nil
  @history    = []

  refresh!
  @timer = Ztimer.every(@refresh_rate) { refresh! }
end

Instance Attribute Details

#avgObject (readonly)

Returns the value of attribute avg.



14
15
16
# File 'lib/fleck/utilities/host_rating.rb', line 14

def avg
  @avg
end

#historyObject (readonly)

Returns the value of attribute history.



14
15
16
# File 'lib/fleck/utilities/host_rating.rb', line 14

def history
  @history
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/fleck/utilities/host_rating.rb', line 14

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/fleck/utilities/host_rating.rb', line 14

def port
  @port
end

Instance Method Details

#<=>(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fleck/utilities/host_rating.rb', line 40

def <=>(other)
  # the other host is reachable, so it comes first
  return 1 if !reachable? && other.reachable?

  # both host are unreachable, so they have the same priority
  return 0 unless reachable? || other.reachable?

  # the current host comes first, because it's reachable, while the other host is unreachable
  return -1 if reachable? && !other.reachable?

  # when both hosts are reachable, use avg latency to order them
  avg <=> other.avg
end

#closeObject



36
37
38
# File 'lib/fleck/utilities/host_rating.rb', line 36

def close
  @timer.cancel!
end

#reachable?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/fleck/utilities/host_rating.rb', line 32

def reachable?
  @reachable
end