class Policy
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.
Definitions
def initialize(maximum_failures: 6, window: 60)
Initialize the policy.
Signature
-
parameter
maximum_failuresInteger The maximum number of failures allowed within the window.
-
parameter
windowInteger The time window in seconds for counting failures.
Implementation
def initialize(maximum_failures: 6, window: 60)
@maximum_failures = maximum_failures
@window = window
@failure_rate_threshold = maximum_failures.to_f / window
end
attr :maximum_failures
The maximum number of failures allowed within the window.
Signature
-
attribute
Integer
attr :window
The time window in seconds for statistics tracking.
Signature
-
attribute
Integer
attr :failure_rate_threshold
The failure rate threshold in failures per second.
Signature
-
attribute
Float
def make_statistics
Create statistics for a container with the configured window.
Signature
-
returns
Async::Container::Statistics A new statistics instance.
Implementation
def make_statistics
Async::Container::Statistics.new(window: @window)
end
def child_exit(container, child, status, name:, key:, **options)
Called when a child exits. Monitors failure rate and stops the container if threshold is exceeded.
Signature
-
parameter
containerAsync::Container::Generic The container.
-
parameter
childChild The child process.
-
parameter
statusProcess::Status The exit status.
-
parameter
nameString The name of the child.
-
parameter
keySymbol An optional key for the child.
-
parameter
optionsHash Additional options for future extensibility.
Implementation
def child_exit(container, child, status, name:, key:, **options)
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
DEFAULT = self.new.freeze
The default service policy instance.