Class: Spandx::Core::Circuit

Inherits:
Object
  • Object
show all
Defined in:
lib/spandx/core/circuit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, state: :closed, logger: Spandx.logger) ⇒ Circuit

Returns a new instance of Circuit.



8
9
10
11
12
# File 'lib/spandx/core/circuit.rb', line 8

def initialize(name, state: :closed, logger: Spandx.logger)
  @name = name
  @state = state
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#attemptObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/spandx/core/circuit.rb', line 14

def attempt
  return if open?

  open!
  result = yield
  close!
  result
ensure
  logger.debug("#{name} #{state}")
end

#close!Object



29
30
31
# File 'lib/spandx/core/circuit.rb', line 29

def close!
  @state = :closed
end

#open!Object



25
26
27
# File 'lib/spandx/core/circuit.rb', line 25

def open!
  @state = :open
end

#open?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/spandx/core/circuit.rb', line 33

def open?
  state == :open
end