stoplight 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/stoplight/admin/views/layout.erb +3 -3
  4. data/lib/stoplight/admin.rb +4 -4
  5. data/lib/stoplight/domain/color.rb +11 -0
  6. data/lib/stoplight/{config → domain}/compatibility_result.rb +1 -1
  7. data/lib/stoplight/domain/config.rb +55 -0
  8. data/lib/stoplight/{data_store/base.rb → domain/data_store.rb} +17 -15
  9. data/lib/stoplight/domain/error.rb +42 -0
  10. data/lib/stoplight/domain/failure.rb +42 -0
  11. data/lib/stoplight/domain/light/configuration_builder_interface.rb +130 -0
  12. data/lib/stoplight/domain/light.rb +198 -0
  13. data/lib/stoplight/domain/light_factory.rb +75 -0
  14. data/lib/stoplight/domain/metadata.rb +65 -0
  15. data/lib/stoplight/domain/state.rb +11 -0
  16. data/lib/stoplight/{notifier/base.rb → domain/state_transition_notifier.rb} +5 -4
  17. data/lib/stoplight/domain/strategies/green_run_strategy.rb +69 -0
  18. data/lib/stoplight/domain/strategies/red_run_strategy.rb +41 -0
  19. data/lib/stoplight/domain/strategies/run_strategy.rb +27 -0
  20. data/lib/stoplight/domain/strategies/yellow_run_strategy.rb +98 -0
  21. data/lib/stoplight/domain/tracker/base.rb +41 -0
  22. data/lib/stoplight/domain/tracker/recovery_probe.rb +72 -0
  23. data/lib/stoplight/domain/tracker/request.rb +67 -0
  24. data/lib/stoplight/domain/traffic_control/base.rb +74 -0
  25. data/lib/stoplight/domain/traffic_control/consecutive_errors.rb +57 -0
  26. data/lib/stoplight/domain/traffic_control/error_rate.rb +51 -0
  27. data/lib/stoplight/domain/traffic_recovery/base.rb +79 -0
  28. data/lib/stoplight/domain/traffic_recovery/consecutive_successes.rb +70 -0
  29. data/lib/stoplight/domain/traffic_recovery.rb +13 -0
  30. data/lib/stoplight/infrastructure/data_store/memory/sliding_window.rb +79 -0
  31. data/lib/stoplight/infrastructure/data_store/memory.rb +307 -0
  32. data/lib/stoplight/infrastructure/data_store/redis/lua.rb +25 -0
  33. data/lib/stoplight/infrastructure/data_store/redis.rb +478 -0
  34. data/lib/stoplight/infrastructure/dependency_injection/container.rb +249 -0
  35. data/lib/stoplight/infrastructure/dependency_injection/unresolved_dependency_error.rb +13 -0
  36. data/lib/stoplight/infrastructure/notifier/generic.rb +90 -0
  37. data/lib/stoplight/infrastructure/notifier/io.rb +23 -0
  38. data/lib/stoplight/infrastructure/notifier/logger.rb +21 -0
  39. data/lib/stoplight/rspec/generic_notifier.rb +1 -1
  40. data/lib/stoplight/version.rb +1 -1
  41. data/lib/stoplight/wiring/container.rb +80 -0
  42. data/lib/stoplight/wiring/default.rb +28 -0
  43. data/lib/stoplight/{config/user_default_config.rb → wiring/default_configuration.rb} +24 -31
  44. data/lib/stoplight/wiring/default_factory_builder.rb +25 -0
  45. data/lib/stoplight/{data_store/fail_safe.rb → wiring/fail_safe_data_store.rb} +22 -11
  46. data/lib/stoplight/{notifier/fail_safe.rb → wiring/fail_safe_notifier.rb} +22 -13
  47. data/lib/stoplight/wiring/light/default_config.rb +18 -0
  48. data/lib/stoplight/wiring/light/system_config.rb +11 -0
  49. data/lib/stoplight/wiring/light_factory.rb +188 -0
  50. data/lib/stoplight/wiring/public_api.rb +28 -0
  51. data/lib/stoplight/wiring/system_container.rb +9 -0
  52. data/lib/stoplight/wiring/system_light_factory.rb +17 -0
  53. data/lib/stoplight.rb +38 -28
  54. metadata +53 -43
  55. data/lib/stoplight/color.rb +0 -9
  56. data/lib/stoplight/config/dsl.rb +0 -97
  57. data/lib/stoplight/config/library_default_config.rb +0 -21
  58. data/lib/stoplight/config/system_config.rb +0 -10
  59. data/lib/stoplight/data_store/memory/sliding_window.rb +0 -77
  60. data/lib/stoplight/data_store/memory.rb +0 -285
  61. data/lib/stoplight/data_store/redis/lua.rb +0 -23
  62. data/lib/stoplight/data_store/redis.rb +0 -446
  63. data/lib/stoplight/data_store.rb +0 -6
  64. data/lib/stoplight/default.rb +0 -30
  65. data/lib/stoplight/error.rb +0 -39
  66. data/lib/stoplight/failure.rb +0 -71
  67. data/lib/stoplight/light/config.rb +0 -112
  68. data/lib/stoplight/light/configuration_builder_interface.rb +0 -128
  69. data/lib/stoplight/light/green_run_strategy.rb +0 -54
  70. data/lib/stoplight/light/red_run_strategy.rb +0 -31
  71. data/lib/stoplight/light/run_strategy.rb +0 -32
  72. data/lib/stoplight/light/yellow_run_strategy.rb +0 -94
  73. data/lib/stoplight/light.rb +0 -191
  74. data/lib/stoplight/metadata.rb +0 -99
  75. data/lib/stoplight/notifier/generic.rb +0 -79
  76. data/lib/stoplight/notifier/io.rb +0 -21
  77. data/lib/stoplight/notifier/logger.rb +0 -19
  78. data/lib/stoplight/state.rb +0 -9
  79. data/lib/stoplight/traffic_control/base.rb +0 -70
  80. data/lib/stoplight/traffic_control/consecutive_errors.rb +0 -55
  81. data/lib/stoplight/traffic_control/error_rate.rb +0 -49
  82. data/lib/stoplight/traffic_recovery/base.rb +0 -75
  83. data/lib/stoplight/traffic_recovery/consecutive_successes.rb +0 -68
  84. data/lib/stoplight/traffic_recovery.rb +0 -11
  85. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/get_metadata.lua +0 -0
  86. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/record_failure.lua +0 -0
  87. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/record_success.lua +0 -0
  88. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/transition_to_green.lua +0 -0
  89. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/transition_to_red.lua +0 -0
  90. /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/transition_to_yellow.lua +0 -0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stoplight
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.0
4
+ version: 5.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Desautels
@@ -57,51 +57,61 @@ files:
57
57
  - lib/stoplight/admin/views/_card.erb
58
58
  - lib/stoplight/admin/views/index.erb
59
59
  - lib/stoplight/admin/views/layout.erb
60
- - lib/stoplight/color.rb
61
- - lib/stoplight/config/compatibility_result.rb
62
- - lib/stoplight/config/dsl.rb
63
- - lib/stoplight/config/library_default_config.rb
64
- - lib/stoplight/config/system_config.rb
65
- - lib/stoplight/config/user_default_config.rb
66
- - lib/stoplight/data_store.rb
67
- - lib/stoplight/data_store/base.rb
68
- - lib/stoplight/data_store/fail_safe.rb
69
- - lib/stoplight/data_store/memory.rb
70
- - lib/stoplight/data_store/memory/sliding_window.rb
71
- - lib/stoplight/data_store/redis.rb
72
- - lib/stoplight/data_store/redis/get_metadata.lua
73
- - lib/stoplight/data_store/redis/lua.rb
74
- - lib/stoplight/data_store/redis/record_failure.lua
75
- - lib/stoplight/data_store/redis/record_success.lua
76
- - lib/stoplight/data_store/redis/transition_to_green.lua
77
- - lib/stoplight/data_store/redis/transition_to_red.lua
78
- - lib/stoplight/data_store/redis/transition_to_yellow.lua
79
- - lib/stoplight/default.rb
80
- - lib/stoplight/error.rb
81
- - lib/stoplight/failure.rb
82
- - lib/stoplight/light.rb
83
- - lib/stoplight/light/config.rb
84
- - lib/stoplight/light/configuration_builder_interface.rb
85
- - lib/stoplight/light/green_run_strategy.rb
86
- - lib/stoplight/light/red_run_strategy.rb
87
- - lib/stoplight/light/run_strategy.rb
88
- - lib/stoplight/light/yellow_run_strategy.rb
89
- - lib/stoplight/metadata.rb
90
- - lib/stoplight/notifier/base.rb
91
- - lib/stoplight/notifier/fail_safe.rb
92
- - lib/stoplight/notifier/generic.rb
93
- - lib/stoplight/notifier/io.rb
94
- - lib/stoplight/notifier/logger.rb
60
+ - lib/stoplight/domain/color.rb
61
+ - lib/stoplight/domain/compatibility_result.rb
62
+ - lib/stoplight/domain/config.rb
63
+ - lib/stoplight/domain/data_store.rb
64
+ - lib/stoplight/domain/error.rb
65
+ - lib/stoplight/domain/failure.rb
66
+ - lib/stoplight/domain/light.rb
67
+ - lib/stoplight/domain/light/configuration_builder_interface.rb
68
+ - lib/stoplight/domain/light_factory.rb
69
+ - lib/stoplight/domain/metadata.rb
70
+ - lib/stoplight/domain/state.rb
71
+ - lib/stoplight/domain/state_transition_notifier.rb
72
+ - lib/stoplight/domain/strategies/green_run_strategy.rb
73
+ - lib/stoplight/domain/strategies/red_run_strategy.rb
74
+ - lib/stoplight/domain/strategies/run_strategy.rb
75
+ - lib/stoplight/domain/strategies/yellow_run_strategy.rb
76
+ - lib/stoplight/domain/tracker/base.rb
77
+ - lib/stoplight/domain/tracker/recovery_probe.rb
78
+ - lib/stoplight/domain/tracker/request.rb
79
+ - lib/stoplight/domain/traffic_control/base.rb
80
+ - lib/stoplight/domain/traffic_control/consecutive_errors.rb
81
+ - lib/stoplight/domain/traffic_control/error_rate.rb
82
+ - lib/stoplight/domain/traffic_recovery.rb
83
+ - lib/stoplight/domain/traffic_recovery/base.rb
84
+ - lib/stoplight/domain/traffic_recovery/consecutive_successes.rb
85
+ - lib/stoplight/infrastructure/data_store/memory.rb
86
+ - lib/stoplight/infrastructure/data_store/memory/sliding_window.rb
87
+ - lib/stoplight/infrastructure/data_store/redis.rb
88
+ - lib/stoplight/infrastructure/data_store/redis/get_metadata.lua
89
+ - lib/stoplight/infrastructure/data_store/redis/lua.rb
90
+ - lib/stoplight/infrastructure/data_store/redis/record_failure.lua
91
+ - lib/stoplight/infrastructure/data_store/redis/record_success.lua
92
+ - lib/stoplight/infrastructure/data_store/redis/transition_to_green.lua
93
+ - lib/stoplight/infrastructure/data_store/redis/transition_to_red.lua
94
+ - lib/stoplight/infrastructure/data_store/redis/transition_to_yellow.lua
95
+ - lib/stoplight/infrastructure/dependency_injection/container.rb
96
+ - lib/stoplight/infrastructure/dependency_injection/unresolved_dependency_error.rb
97
+ - lib/stoplight/infrastructure/notifier/generic.rb
98
+ - lib/stoplight/infrastructure/notifier/io.rb
99
+ - lib/stoplight/infrastructure/notifier/logger.rb
95
100
  - lib/stoplight/rspec.rb
96
101
  - lib/stoplight/rspec/generic_notifier.rb
97
- - lib/stoplight/state.rb
98
- - lib/stoplight/traffic_control/base.rb
99
- - lib/stoplight/traffic_control/consecutive_errors.rb
100
- - lib/stoplight/traffic_control/error_rate.rb
101
- - lib/stoplight/traffic_recovery.rb
102
- - lib/stoplight/traffic_recovery/base.rb
103
- - lib/stoplight/traffic_recovery/consecutive_successes.rb
104
102
  - lib/stoplight/version.rb
103
+ - lib/stoplight/wiring/container.rb
104
+ - lib/stoplight/wiring/default.rb
105
+ - lib/stoplight/wiring/default_configuration.rb
106
+ - lib/stoplight/wiring/default_factory_builder.rb
107
+ - lib/stoplight/wiring/fail_safe_data_store.rb
108
+ - lib/stoplight/wiring/fail_safe_notifier.rb
109
+ - lib/stoplight/wiring/light/default_config.rb
110
+ - lib/stoplight/wiring/light/system_config.rb
111
+ - lib/stoplight/wiring/light_factory.rb
112
+ - lib/stoplight/wiring/public_api.rb
113
+ - lib/stoplight/wiring/system_container.rb
114
+ - lib/stoplight/wiring/system_light_factory.rb
105
115
  homepage: https://github.com/bolshakov/stoplight
106
116
  licenses:
107
117
  - MIT
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module Color
5
- GREEN = "green"
6
- YELLOW = "yellow"
7
- RED = "red"
8
- end
9
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module Config
5
- # This is a DSL for configuring Stoplight settings. It is responsible for
6
- # transforming the provided settings into a format that can be used by Stoplight.
7
- #
8
- # @api private
9
- class DSL
10
- def transform(settings)
11
- if settings.has_key?(:data_store)
12
- settings[:data_store] = build_data_store(settings[:data_store])
13
- end
14
-
15
- if settings.has_key?(:notifiers)
16
- settings[:notifiers] = build_notifiers(settings[:notifiers])
17
- end
18
-
19
- if settings.has_key?(:tracked_errors)
20
- settings[:tracked_errors] = build_tracked_errors(settings[:tracked_errors])
21
- end
22
-
23
- if settings.has_key?(:skipped_errors)
24
- settings[:skipped_errors] = build_skipped_errors(settings[:skipped_errors])
25
- end
26
-
27
- if settings.has_key?(:cool_off_time)
28
- settings[:cool_off_time] = build_cool_off_time(settings[:cool_off_time])
29
- end
30
-
31
- if settings.has_key?(:traffic_control)
32
- settings[:traffic_control] = build_traffic_control(settings[:traffic_control])
33
- end
34
-
35
- if settings.has_key?(:traffic_recovery)
36
- settings[:traffic_recovery] = build_traffic_recovery(settings[:traffic_recovery])
37
- end
38
- settings
39
- end
40
-
41
- private
42
-
43
- def build_data_store(data_store)
44
- DataStore::FailSafe.wrap(data_store)
45
- end
46
-
47
- def build_notifiers(notifiers)
48
- notifiers.map { |notifier| Notifier::FailSafe.wrap(notifier) }
49
- end
50
-
51
- def build_tracked_errors(tracked_error)
52
- Array(tracked_error)
53
- end
54
-
55
- def build_skipped_errors(skipped_errors)
56
- Array(skipped_errors)
57
- end
58
-
59
- def build_cool_off_time(cool_off_time)
60
- cool_off_time.to_i
61
- end
62
-
63
- def build_traffic_control(traffic_control)
64
- case traffic_control
65
- in Stoplight::TrafficControl::Base
66
- traffic_control
67
- in :consecutive_errors
68
- Stoplight::TrafficControl::ConsecutiveErrors.new
69
- in :error_rate
70
- Stoplight::TrafficControl::ErrorRate.new
71
- in {error_rate: error_rate_settings}
72
- Stoplight::TrafficControl::ErrorRate.new(**error_rate_settings)
73
- else
74
- raise Error::ConfigurationError, <<~ERROR
75
- unsupported traffic_control strategy provided (`#{traffic_control}`). Supported options:
76
- * Stoplight::TrafficControl::ConsecutiveErrors
77
- * Stoplight::TrafficControl::ErrorRate
78
- ERROR
79
- end
80
- end
81
-
82
- def build_traffic_recovery(traffic_recovery)
83
- case traffic_recovery
84
- in Stoplight::TrafficRecovery::Base
85
- traffic_recovery
86
- in :consecutive_successes
87
- Stoplight::TrafficRecovery::ConsecutiveSuccesses.new
88
- else
89
- raise Error::ConfigurationError, <<~ERROR
90
- unsupported traffic_recovery strategy provided (`#{traffic_recovery}`). Supported options:
91
- * Stoplight::TrafficRecovery::ConsecutiveSuccesses
92
- ERROR
93
- end
94
- end
95
- end
96
- end
97
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module Config
5
- # Provides default settings for the Stoplight library.
6
- # @api private
7
- LibraryDefaultConfig = Light::Config.empty.with(
8
- cool_off_time: Stoplight::Default::COOL_OFF_TIME,
9
- data_store: Stoplight::Default::DATA_STORE,
10
- error_notifier: Stoplight::Default::ERROR_NOTIFIER,
11
- notifiers: Stoplight::Default::NOTIFIERS,
12
- threshold: Stoplight::Default::THRESHOLD,
13
- recovery_threshold: Stoplight::Default::RECOVERY_THRESHOLD,
14
- window_size: Stoplight::Default::WINDOW_SIZE,
15
- tracked_errors: Stoplight::Default::TRACKED_ERRORS,
16
- skipped_errors: Stoplight::Default::SKIPPED_ERRORS,
17
- traffic_control: Stoplight::Default::TRAFFIC_CONTROL,
18
- traffic_recovery: Stoplight::Default::TRAFFIC_RECOVERY
19
- )
20
- end
21
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module Config
5
- SystemConfig = LibraryDefaultConfig.with(
6
- traffic_recovery: :consecutive_successes,
7
- recovery_threshold: 3
8
- )
9
- end
10
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module DataStore
5
- class Memory < Base
6
- # Hash-based sliding window for O(1) amortized operations.
7
- #
8
- # Maintains a running sum and stores per-second counts in a Hash. Ruby's Hash
9
- # preserves insertion order (FIFO), allowing efficient removal of expired
10
- # buckets from the front via +Hash#shift+, with their counts subtracted from
11
- # the running sum.
12
- #
13
- # Performance: O(1) amortized for both reads and writes
14
- # Memory: Bounded to the number of buckets
15
- #
16
- # @note Not thread-safe; synchronization must be handled externally
17
- # @api private
18
- class SlidingWindow
19
- # @!attribute buckets
20
- # @return [Hash<Integer, Integer>] A hash mapping time buckets to their counts
21
- private attr_reader :buckets
22
-
23
- # @!attribute running_sum
24
- # @return [Integer] The running sum of all increments in the current window
25
- private attr_accessor :running_sum
26
-
27
- def initialize
28
- @buckets = Hash.new { |buckets, bucket| buckets[bucket] = 0 }
29
- @running_sum = 0
30
- end
31
-
32
- # Increment the count at a given timestamp
33
- def increment
34
- buckets[current_bucket] += 1
35
- self.running_sum += 1
36
- end
37
-
38
- # @param window_start [Time]
39
- # @return [Integer]
40
- def sum_in_window(window_start)
41
- slide_window!(window_start)
42
- self.running_sum
43
- end
44
-
45
- private def slide_window!(window_start)
46
- window_start_ts = window_start.to_i
47
-
48
- loop do
49
- timestamp, sum = buckets.first
50
- if timestamp.nil? || timestamp >= window_start_ts
51
- break
52
- else
53
- self.running_sum -= sum
54
- buckets.shift
55
- end
56
- end
57
- end
58
-
59
- private def current_bucket
60
- bucket_for_time(current_time)
61
- end
62
-
63
- private def bucket_for_time(time)
64
- time.to_i
65
- end
66
-
67
- private def current_time
68
- Time.now
69
- end
70
-
71
- def inspect
72
- "#<#{self.class.name} #{buckets}>"
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,285 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "monitor"
4
-
5
- module Stoplight
6
- module DataStore
7
- # @see Base
8
- class Memory < Base
9
- include MonitorMixin
10
-
11
- KEY_SEPARATOR = ":"
12
-
13
- def initialize
14
- @errors = Hash.new { |errors, light_name| errors[light_name] = SlidingWindow.new }
15
- @successes = Hash.new { |successes, light_name| successes[light_name] = SlidingWindow.new }
16
-
17
- @recovery_probe_errors = Hash.new { |recovery_probe_errors, light_name| recovery_probe_errors[light_name] = SlidingWindow.new }
18
- @recovery_probe_successes = Hash.new { |recovery_probe_successes, light_name| recovery_probe_successes[light_name] = SlidingWindow.new }
19
-
20
- @metadata = Hash.new { |h, k| h[k] = Metadata.new }
21
- super # MonitorMixin
22
- end
23
-
24
- # @return [Array<String>]
25
- def names
26
- synchronize { @metadata.keys }
27
- end
28
-
29
- # @param config [Stoplight::Light::Config]
30
- # @return [Stoplight::Metadata]
31
- def get_metadata(config)
32
- light_name = config.name
33
-
34
- synchronize do
35
- current_time = self.current_time
36
- recovery_window_start = (current_time - config.cool_off_time)
37
- recovered_at = @metadata[light_name].recovered_at
38
- window_start = if config.window_size
39
- [recovered_at, (current_time - config.window_size)].compact.max
40
- else
41
- current_time
42
- end
43
-
44
- @metadata[light_name].with(
45
- current_time:,
46
- errors: @errors[config.name].sum_in_window(window_start),
47
- successes: @successes[config.name].sum_in_window(window_start),
48
- recovery_probe_errors: @recovery_probe_errors[config.name].sum_in_window(recovery_window_start),
49
- recovery_probe_successes: @recovery_probe_successes[config.name].sum_in_window(recovery_window_start)
50
- )
51
- end
52
- end
53
-
54
- # @param config [Stoplight::Light::Config]
55
- # @param failure [Stoplight::Failure]
56
- # @return [Stoplight::Metadata]
57
- def record_failure(config, failure)
58
- current_time = self.current_time
59
- light_name = config.name
60
-
61
- synchronize do
62
- @errors[light_name].increment if config.window_size
63
-
64
- metadata = @metadata[light_name]
65
- @metadata[light_name] = if metadata.last_error_at.nil? || current_time > metadata.last_error_at
66
- metadata.with(
67
- last_error_at: current_time,
68
- last_error: failure,
69
- consecutive_errors: metadata.consecutive_errors.succ,
70
- consecutive_successes: 0
71
- )
72
- else
73
- metadata.with(
74
- consecutive_errors: metadata.consecutive_errors.succ,
75
- consecutive_successes: 0
76
- )
77
- end
78
- get_metadata(config)
79
- end
80
- end
81
-
82
- # @param config [Stoplight::Light::Config]
83
- # @return [void]
84
- def record_success(config)
85
- light_name = config.name
86
- current_time = self.current_time
87
-
88
- synchronize do
89
- @successes[light_name].increment if config.window_size
90
-
91
- metadata = @metadata[light_name]
92
- @metadata[light_name] = if metadata.last_success_at.nil? || current_time > metadata.last_success_at
93
- metadata.with(
94
- last_success_at: current_time,
95
- consecutive_errors: 0,
96
- consecutive_successes: metadata.consecutive_successes.succ
97
- )
98
- else
99
- metadata.with(
100
- consecutive_errors: 0,
101
- consecutive_successes: metadata.consecutive_successes.succ
102
- )
103
- end
104
- end
105
- end
106
-
107
- # @param config [Stoplight::Light::Config]
108
- # @param failure [Stoplight::Failure]
109
- # @return [Stoplight::Metadata]
110
- def record_recovery_probe_failure(config, failure)
111
- light_name = config.name
112
- current_time = self.current_time
113
-
114
- synchronize do
115
- @recovery_probe_errors[light_name].increment
116
-
117
- metadata = @metadata[light_name]
118
- @metadata[light_name] = if metadata.last_error_at.nil? || current_time > metadata.last_error_at
119
- metadata.with(
120
- last_error_at: current_time,
121
- last_error: failure,
122
- consecutive_errors: metadata.consecutive_errors.succ,
123
- consecutive_successes: 0
124
- )
125
- else
126
- metadata.with(
127
- consecutive_errors: metadata.consecutive_errors.succ,
128
- consecutive_successes: 0
129
- )
130
- end
131
- get_metadata(config)
132
- end
133
- end
134
-
135
- # @param config [Stoplight::Light::Config]
136
- # @return [Stoplight::Metadata]
137
- def record_recovery_probe_success(config)
138
- light_name = config.name
139
- current_time = self.current_time
140
-
141
- synchronize do
142
- @recovery_probe_successes[light_name].increment
143
-
144
- metadata = @metadata[light_name]
145
- @metadata[light_name] = if metadata.last_success_at.nil? || current_time > metadata.last_success_at
146
- metadata.with(
147
- last_success_at: current_time,
148
- consecutive_errors: 0,
149
- consecutive_successes: metadata.consecutive_successes.succ
150
- )
151
- else
152
- metadata.with(
153
- consecutive_errors: 0,
154
- consecutive_successes: metadata.consecutive_successes.succ
155
- )
156
- end
157
- get_metadata(config)
158
- end
159
- end
160
-
161
- # @param config [Stoplight::Light::Config]
162
- # @param state [String]
163
- # @return [String]
164
- def set_state(config, state)
165
- light_name = config.name
166
-
167
- synchronize do
168
- metadata = @metadata[light_name]
169
- @metadata[light_name] = metadata.with(locked_state: state)
170
- end
171
- state
172
- end
173
-
174
- # @return [String]
175
- def inspect
176
- "#<#{self.class.name}>"
177
- end
178
-
179
- # Combined method that performs the state transition based on color
180
- #
181
- # @param config [Stoplight::Light::Config] The light configuration
182
- # @param color [String] The color to transition to ("GREEN", "YELLOW", or "RED")
183
- # @return [Boolean] true if this is the first instance to detect this transition
184
- def transition_to_color(config, color)
185
- case color
186
- when Color::GREEN
187
- transition_to_green(config)
188
- when Color::YELLOW
189
- transition_to_yellow(config)
190
- when Color::RED
191
- transition_to_red(config)
192
- else
193
- raise ArgumentError, "Invalid color: #{color}"
194
- end
195
- end
196
-
197
- # Transitions to GREEN state and ensures only one notification
198
- #
199
- # @param config [Stoplight::Light::Config] The light configuration
200
- # @return [Boolean] true if this is the first instance to detect this transition
201
- private def transition_to_green(config)
202
- light_name = config.name
203
- current_time = self.current_time
204
-
205
- synchronize do
206
- metadata = @metadata[light_name]
207
- if metadata.recovered_at
208
- false
209
- else
210
- @metadata[light_name] = metadata.with(
211
- recovered_at: current_time,
212
- recovery_started_at: nil,
213
- breached_at: nil,
214
- recovery_scheduled_after: nil
215
- )
216
- true
217
- end
218
- end
219
- end
220
-
221
- # Transitions to YELLOW (recovery) state and ensures only one notification
222
- #
223
- # @param config [Stoplight::Light::Config] The light configuration
224
- # @return [Boolean] true if this is the first instance to detect this transition
225
- private def transition_to_yellow(config)
226
- light_name = config.name
227
- current_time = self.current_time
228
-
229
- synchronize do
230
- metadata = @metadata[light_name]
231
- if metadata.recovery_started_at.nil?
232
- @metadata[light_name] = metadata.with(
233
- recovery_started_at: current_time,
234
- recovery_scheduled_after: nil,
235
- recovered_at: nil,
236
- breached_at: nil
237
- )
238
- true
239
- else
240
- @metadata[light_name] = metadata.with(
241
- recovery_scheduled_after: nil,
242
- recovered_at: nil,
243
- breached_at: nil
244
- )
245
- false
246
- end
247
- end
248
- end
249
-
250
- # Transitions to RED state and ensures only one notification
251
- #
252
- # @param config [Stoplight::Light::Config] The light configuration
253
- # @return [Boolean] true if this is the first instance to detect this transition
254
- private def transition_to_red(config)
255
- light_name = config.name
256
- current_time = self.current_time
257
- recovery_scheduled_after = current_time + config.cool_off_time
258
-
259
- synchronize do
260
- metadata = @metadata[light_name]
261
- if metadata.breached_at
262
- @metadata[light_name] = metadata.with(
263
- recovery_scheduled_after: recovery_scheduled_after,
264
- recovery_started_at: nil,
265
- recovered_at: nil
266
- )
267
- false
268
- else
269
- @metadata[light_name] = metadata.with(
270
- breached_at: current_time,
271
- recovery_scheduled_after: recovery_scheduled_after,
272
- recovery_started_at: nil,
273
- recovered_at: nil
274
- )
275
- true
276
- end
277
- end
278
- end
279
-
280
- private def current_time
281
- Time.now
282
- end
283
- end
284
- end
285
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stoplight
4
- module DataStore
5
- class Redis
6
- # @api private
7
- module Lua
8
- class << self
9
- def read_lua_file(name_without_extension)
10
- File.read(File.join(__dir__, "#{name_without_extension}.lua"))
11
- end
12
- end
13
-
14
- RECORD_FAILURE = read_lua_file("record_failure")
15
- RECORD_SUCCESS = read_lua_file("record_success")
16
- GET_METADATA = read_lua_file("get_metadata")
17
- TRANSITION_TO_YELLOW = read_lua_file("transition_to_yellow")
18
- TRANSITION_TO_RED = read_lua_file("transition_to_red")
19
- TRANSITION_TO_GREEN = read_lua_file("transition_to_green")
20
- end
21
- end
22
- end
23
- end