Class: Async::Service::Policy
- Inherits:
-
Container::Policy
- Object
- Container::Policy
- Async::Service::Policy
- Defined in:
- lib/async/service/policy.rb
Overview
A service-level policy that extends the base container policy with failure rate monitoring. This policy will stop the container if the failure rate exceeds a threshold.
Constant Summary collapse
- DEFAULT =
The default service policy instance.
self.new.freeze
Instance Attribute Summary collapse
-
#failure_rate_threshold ⇒ Object
The failure rate threshold in failures per second.
-
#maximum_failures ⇒ Object
readonly
The maximum number of failures allowed within the window.
-
#window ⇒ Object
readonly
The time window in seconds for statistics tracking.
Instance Method Summary collapse
-
#child_exit(container, child, status, name:, key:, **options) ⇒ Object
Called when a child exits.
-
#initialize(maximum_failures: 6, window: 60) ⇒ Policy
constructor
Initialize the policy.
-
#make_statistics ⇒ Object
Create statistics for a container with the configured window.
Constructor Details
#initialize(maximum_failures: 6, window: 60) ⇒ Policy
Initialize the policy.
16 17 18 19 20 21 |
# File 'lib/async/service/policy.rb', line 16 def initialize(maximum_failures: 6, window: 60) @maximum_failures = maximum_failures @window = window @failure_rate_threshold = maximum_failures.to_f / window end |
Instance Attribute Details
#failure_rate_threshold ⇒ Object
The failure rate threshold in failures per second.
33 34 35 |
# File 'lib/async/service/policy.rb', line 33 def failure_rate_threshold @failure_rate_threshold end |
#maximum_failures ⇒ Object (readonly)
The maximum number of failures allowed within the window.
25 26 27 |
# File 'lib/async/service/policy.rb', line 25 def maximum_failures @maximum_failures end |
#window ⇒ Object (readonly)
The time window in seconds for statistics tracking.
29 30 31 |
# File 'lib/async/service/policy.rb', line 29 def window @window end |
Instance Method Details
#child_exit(container, child, status, name:, key:, **options) ⇒ Object
Called when a child exits. Monitors failure rate and stops the container if threshold is exceeded.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/async/service/policy.rb', line 48 def child_exit(container, child, status, name:, key:, **) unless success?(status) # Check failure rate after this failure is recorded rate = container.statistics.failure_rate.per_second if rate > @failure_rate_threshold # Only stop if container is not already stopping unless container.stopping? Console.error(self, "Failure rate exceeded threshold, stopping container!", rate: rate, threshold: @failure_rate_threshold ) container.stop(true) end end end end |
#make_statistics ⇒ Object
Create statistics for a container with the configured window.
37 38 39 |
# File 'lib/async/service/policy.rb', line 37 def make_statistics Async::Container::Statistics.new(window: @window) end |