Class: Stoplight::Domain::Light
- Inherits:
-
Object
- Object
- Stoplight::Domain::Light
- Defined in:
- lib/stoplight/domain/light.rb,
lib/stoplight/domain/light/configuration_builder_interface.rb
Defined Under Namespace
Modules: ConfigurationBuilderInterface
Instance Attribute Summary collapse
- #factory ⇒ Object readonly
- #green_run_strategy ⇒ Object readonly
- #name ⇒ Object readonly
- #red_run_strategy ⇒ Object readonly
- #state_store ⇒ Object readonly
- #yellow_run_strategy ⇒ Object readonly
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two lights considered equal if they have the same configuration.
-
#color ⇒ Object
Returns current color: *
Stoplight::Color::GREEN– circuit breaker is closed *Stoplight::Color::RED– circuit breaker is open *Stoplight::Color::YELLOW– circuit breaker is half-open. -
#initialize(name, green_run_strategy:, yellow_run_strategy:, red_run_strategy:, factory:, state_store:) ⇒ Light
constructor
A new instance of Light.
-
#lock(color) ⇒ Object
Locks light in either
State::LOCKED_REDorState::LOCKED_GREEN. -
#run(fallback = nil, &code) ⇒ Object
Runs the given block of code with this circuit breaker.
-
#state ⇒ Object
Returns the current state of the light: *
Stoplight::State::LOCKED_GREEN– light is locked green and allows all traffic *Stoplight::State::LOCKED_RED– light is locked red and blocks all traffic *Stoplight::State::UNLOCKED– light is not locked and follow the configured rules. -
#unlock ⇒ Object
Unlocks light and sets its state to State::UNLOCKED.
- #with(**settings) ⇒ Stoplight::Light deprecated Deprecated.
Methods included from ConfigurationBuilderInterface
#with_cool_off_time, #with_data_store, #with_error_notifier, #with_notifiers, #with_skipped_errors, #with_threshold, #with_tracked_errors, #with_window_size
Methods included from Common::Deprecations
Constructor Details
#initialize(name, green_run_strategy:, yellow_run_strategy:, red_run_strategy:, factory:, state_store:) ⇒ Light
Returns a new instance of Light.
19 20 21 22 23 24 25 26 |
# File 'lib/stoplight/domain/light.rb', line 19 def initialize(name, green_run_strategy:, yellow_run_strategy:, red_run_strategy:, factory:, state_store:) @name = name @green_run_strategy = green_run_strategy @yellow_run_strategy = yellow_run_strategy @red_run_strategy = red_run_strategy @factory = factory @state_store = state_store end |
Instance Attribute Details
#factory ⇒ Object (readonly)
16 17 18 |
# File 'lib/stoplight/domain/light.rb', line 16 def factory @factory end |
#green_run_strategy ⇒ Object (readonly)
13 14 15 |
# File 'lib/stoplight/domain/light.rb', line 13 def green_run_strategy @green_run_strategy end |
#name ⇒ Object (readonly)
11 12 13 |
# File 'lib/stoplight/domain/light.rb', line 11 def name @name end |
#red_run_strategy ⇒ Object (readonly)
15 16 17 |
# File 'lib/stoplight/domain/light.rb', line 15 def red_run_strategy @red_run_strategy end |
#state_store ⇒ Object (readonly)
17 18 19 |
# File 'lib/stoplight/domain/light.rb', line 17 def state_store @state_store end |
#yellow_run_strategy ⇒ Object (readonly)
14 15 16 |
# File 'lib/stoplight/domain/light.rb', line 14 def yellow_run_strategy @yellow_run_strategy end |
Instance Method Details
#==(other) ⇒ Object
Two lights considered equal if they have the same configuration.
102 103 104 |
# File 'lib/stoplight/domain/light.rb', line 102 def ==(other) other.is_a?(self.class) && factory == other.factory end |
#color ⇒ Object
Returns current color:
* +Stoplight::Color::GREEN+ -- circuit breaker is closed
* +Stoplight::Color::RED+ -- circuit breaker is open
* +Stoplight::Color::YELLOW+ -- circuit breaker is half-open
44 |
# File 'lib/stoplight/domain/light.rb', line 44 def color = state_snapshot.color |
#lock(color) ⇒ Object
Locks light in either State::LOCKED_RED or State::LOCKED_GREEN
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/stoplight/domain/light.rb', line 75 def lock(color) state = case color when Color::RED then State::LOCKED_RED when Color::GREEN then State::LOCKED_GREEN else raise Error::IncorrectColor end state_store.set_state(state) self end |
#run(fallback = nil, &code) ⇒ Object
Runs the given block of code with this circuit breaker
58 59 60 61 62 63 64 65 |
# File 'lib/stoplight/domain/light.rb', line 58 def run(fallback = nil, &code) raise ArgumentError, "nothing to run. Please, pass a block into `Light#run`" unless block_given? state_snapshot.then do |state_snapshot| strategy = state_strategy_factory(state_snapshot.color) strategy.execute(fallback, state_snapshot:, &code) end end |
#state ⇒ Object
Returns the current state of the light:
* +Stoplight::State::LOCKED_GREEN+ -- light is locked green and allows all traffic
* +Stoplight::State::LOCKED_RED+ -- light is locked red and blocks all traffic
* +Stoplight::State::UNLOCKED+ -- light is not locked and follow the configured rules
33 |
# File 'lib/stoplight/domain/light.rb', line 33 def state = state_snapshot.locked_state |
#unlock ⇒ Object
Unlocks light and sets its state to State::UNLOCKED
95 96 97 98 99 |
# File 'lib/stoplight/domain/light.rb', line 95 def unlock state_store.set_state(State::UNLOCKED) self end |
#with(**settings) ⇒ Stoplight::Light
Reconfigures the light with updated settings and returns a new instance.
This method allows you to modify the configuration of a Stoplight::Light object by providing a hash of settings. The original light remains unchanged, and a new light instance with the updated configuration is returned.
steep:ignore:start
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/stoplight/domain/light.rb', line 139 def with(**settings) deprecate(<<~MSG) Light#with is deprecated and will be removed in v6.0.0. Circuit breakers should be configured once at creation, not cloned with modifications. Instead of: light = Stoplight('api-call', threshold: 5) modified = light.with(threshold: 10) Configure correctly from the start: Stoplight('api-call', threshold: 10) MSG with_without_warning(**settings) end |