Class: Hystrix::Circuit

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/hystrix/circuit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCircuit

Returns a new instance of Circuit.



8
9
10
11
12
13
# File 'lib/hystrix/circuit.rb', line 8

def initialize
	self.lock = Mutex.new
	self.recent_latency_errors = []
	self.health = 0
	self.last_health_check_time = nil
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



6
7
8
# File 'lib/hystrix/circuit.rb', line 6

def health
  @health
end

#last_health_check_timeObject

Returns the value of attribute last_health_check_time.



6
7
8
# File 'lib/hystrix/circuit.rb', line 6

def last_health_check_time
  @last_health_check_time
end

#lockObject

Returns the value of attribute lock.



6
7
8
# File 'lib/hystrix/circuit.rb', line 6

def lock
  @lock
end

#recent_latency_errorsObject

Returns the value of attribute recent_latency_errors.



6
7
8
# File 'lib/hystrix/circuit.rb', line 6

def recent_latency_errors
  @recent_latency_errors
end

Instance Method Details

#add_latency_error(duration) ⇒ Object



22
23
24
25
26
27
# File 'lib/hystrix/circuit.rb', line 22

def add_latency_error(duration)
	self.lock.synchronize do
		self.recent_latency_errors << {duration: duration, timestamp: Time.now.to_i}
	end
	async.calculate_health
end

#calculate_healthObject



29
30
31
32
33
34
35
36
# File 'lib/hystrix/circuit.rb', line 29

def calculate_health
	now = Time.now.to_i
	self.lock.synchronize do
		self.recent_latency_errors = self.recent_latency_errors.reject{|error| error[:timestamp] < now - 10}
		self.health = self.recent_latency_errors.size * 0.2
		self.last_health_check_time = now.to_f
	end
end

#is_closed?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/hystrix/circuit.rb', line 15

def is_closed?
	async.calculate_health if self.last_health_check_time == nil or self.last_health_check_time < Time.now.to_f - 10.0

	return false unless self.is_healthy?
	return true
end

#is_healthy?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/hystrix/circuit.rb', line 38

def is_healthy?
	self.health < 1
end