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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/stoplight/admin/views/layout.erb +3 -3
- data/lib/stoplight/admin.rb +4 -4
- data/lib/stoplight/domain/color.rb +11 -0
- data/lib/stoplight/{config → domain}/compatibility_result.rb +1 -1
- data/lib/stoplight/domain/config.rb +55 -0
- data/lib/stoplight/{data_store/base.rb → domain/data_store.rb} +17 -15
- data/lib/stoplight/domain/error.rb +42 -0
- data/lib/stoplight/domain/failure.rb +42 -0
- data/lib/stoplight/domain/light/configuration_builder_interface.rb +130 -0
- data/lib/stoplight/domain/light.rb +198 -0
- data/lib/stoplight/domain/light_factory.rb +75 -0
- data/lib/stoplight/domain/metadata.rb +65 -0
- data/lib/stoplight/domain/state.rb +11 -0
- data/lib/stoplight/{notifier/base.rb → domain/state_transition_notifier.rb} +5 -4
- data/lib/stoplight/domain/strategies/green_run_strategy.rb +69 -0
- data/lib/stoplight/domain/strategies/red_run_strategy.rb +41 -0
- data/lib/stoplight/domain/strategies/run_strategy.rb +27 -0
- data/lib/stoplight/domain/strategies/yellow_run_strategy.rb +98 -0
- data/lib/stoplight/domain/tracker/base.rb +41 -0
- data/lib/stoplight/domain/tracker/recovery_probe.rb +72 -0
- data/lib/stoplight/domain/tracker/request.rb +67 -0
- data/lib/stoplight/domain/traffic_control/base.rb +74 -0
- data/lib/stoplight/domain/traffic_control/consecutive_errors.rb +57 -0
- data/lib/stoplight/domain/traffic_control/error_rate.rb +51 -0
- data/lib/stoplight/domain/traffic_recovery/base.rb +79 -0
- data/lib/stoplight/domain/traffic_recovery/consecutive_successes.rb +70 -0
- data/lib/stoplight/domain/traffic_recovery.rb +13 -0
- data/lib/stoplight/infrastructure/data_store/memory/sliding_window.rb +79 -0
- data/lib/stoplight/infrastructure/data_store/memory.rb +307 -0
- data/lib/stoplight/infrastructure/data_store/redis/lua.rb +25 -0
- data/lib/stoplight/infrastructure/data_store/redis.rb +478 -0
- data/lib/stoplight/infrastructure/dependency_injection/container.rb +249 -0
- data/lib/stoplight/infrastructure/dependency_injection/unresolved_dependency_error.rb +13 -0
- data/lib/stoplight/infrastructure/notifier/generic.rb +90 -0
- data/lib/stoplight/infrastructure/notifier/io.rb +23 -0
- data/lib/stoplight/infrastructure/notifier/logger.rb +21 -0
- data/lib/stoplight/rspec/generic_notifier.rb +1 -1
- data/lib/stoplight/version.rb +1 -1
- data/lib/stoplight/wiring/container.rb +80 -0
- data/lib/stoplight/wiring/default.rb +28 -0
- data/lib/stoplight/{config/user_default_config.rb → wiring/default_configuration.rb} +24 -31
- data/lib/stoplight/wiring/default_factory_builder.rb +25 -0
- data/lib/stoplight/{data_store/fail_safe.rb → wiring/fail_safe_data_store.rb} +22 -11
- data/lib/stoplight/{notifier/fail_safe.rb → wiring/fail_safe_notifier.rb} +22 -13
- data/lib/stoplight/wiring/light/default_config.rb +18 -0
- data/lib/stoplight/wiring/light/system_config.rb +11 -0
- data/lib/stoplight/wiring/light_factory.rb +188 -0
- data/lib/stoplight/wiring/public_api.rb +28 -0
- data/lib/stoplight/wiring/system_container.rb +9 -0
- data/lib/stoplight/wiring/system_light_factory.rb +17 -0
- data/lib/stoplight.rb +38 -28
- metadata +53 -43
- data/lib/stoplight/color.rb +0 -9
- data/lib/stoplight/config/dsl.rb +0 -97
- data/lib/stoplight/config/library_default_config.rb +0 -21
- data/lib/stoplight/config/system_config.rb +0 -10
- data/lib/stoplight/data_store/memory/sliding_window.rb +0 -77
- data/lib/stoplight/data_store/memory.rb +0 -285
- data/lib/stoplight/data_store/redis/lua.rb +0 -23
- data/lib/stoplight/data_store/redis.rb +0 -446
- data/lib/stoplight/data_store.rb +0 -6
- data/lib/stoplight/default.rb +0 -30
- data/lib/stoplight/error.rb +0 -39
- data/lib/stoplight/failure.rb +0 -71
- data/lib/stoplight/light/config.rb +0 -112
- data/lib/stoplight/light/configuration_builder_interface.rb +0 -128
- data/lib/stoplight/light/green_run_strategy.rb +0 -54
- data/lib/stoplight/light/red_run_strategy.rb +0 -31
- data/lib/stoplight/light/run_strategy.rb +0 -32
- data/lib/stoplight/light/yellow_run_strategy.rb +0 -94
- data/lib/stoplight/light.rb +0 -191
- data/lib/stoplight/metadata.rb +0 -99
- data/lib/stoplight/notifier/generic.rb +0 -79
- data/lib/stoplight/notifier/io.rb +0 -21
- data/lib/stoplight/notifier/logger.rb +0 -19
- data/lib/stoplight/state.rb +0 -9
- data/lib/stoplight/traffic_control/base.rb +0 -70
- data/lib/stoplight/traffic_control/consecutive_errors.rb +0 -55
- data/lib/stoplight/traffic_control/error_rate.rb +0 -49
- data/lib/stoplight/traffic_recovery/base.rb +0 -75
- data/lib/stoplight/traffic_recovery/consecutive_successes.rb +0 -68
- data/lib/stoplight/traffic_recovery.rb +0 -11
- /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/get_metadata.lua +0 -0
- /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/record_failure.lua +0 -0
- /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/record_success.lua +0 -0
- /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/transition_to_green.lua +0 -0
- /data/lib/stoplight/{data_store → infrastructure/data_store}/redis/transition_to_red.lua +0 -0
- /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
|
+
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/
|
|
62
|
-
- lib/stoplight/config
|
|
63
|
-
- lib/stoplight/
|
|
64
|
-
- lib/stoplight/
|
|
65
|
-
- lib/stoplight/
|
|
66
|
-
- lib/stoplight/
|
|
67
|
-
- lib/stoplight/
|
|
68
|
-
- lib/stoplight/
|
|
69
|
-
- lib/stoplight/
|
|
70
|
-
- lib/stoplight/
|
|
71
|
-
- lib/stoplight/
|
|
72
|
-
- lib/stoplight/
|
|
73
|
-
- lib/stoplight/
|
|
74
|
-
- lib/stoplight/
|
|
75
|
-
- lib/stoplight/
|
|
76
|
-
- lib/stoplight/
|
|
77
|
-
- lib/stoplight/
|
|
78
|
-
- lib/stoplight/
|
|
79
|
-
- lib/stoplight/
|
|
80
|
-
- lib/stoplight/
|
|
81
|
-
- lib/stoplight/
|
|
82
|
-
- lib/stoplight/
|
|
83
|
-
- lib/stoplight/
|
|
84
|
-
- lib/stoplight/
|
|
85
|
-
- lib/stoplight/
|
|
86
|
-
- lib/stoplight/
|
|
87
|
-
- lib/stoplight/
|
|
88
|
-
- lib/stoplight/
|
|
89
|
-
- lib/stoplight/
|
|
90
|
-
- lib/stoplight/
|
|
91
|
-
- lib/stoplight/
|
|
92
|
-
- lib/stoplight/
|
|
93
|
-
- lib/stoplight/
|
|
94
|
-
- lib/stoplight/
|
|
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
|
data/lib/stoplight/color.rb
DELETED
data/lib/stoplight/config/dsl.rb
DELETED
|
@@ -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,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
|