Class: Stoplight::Domain::TrafficControl::ErrorRate Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stoplight/domain/traffic_control/error_rate.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A strategy that stops the traffic based on error rate.

Will switch to red if 60% error rate reached within the 5-minute (300 seconds) sliding window. By default this traffic control strategy starts evaluating only after 10 requests have been made. You can adjust this by passing a different value for min_requests when initializing the strategy.

traffic_control = Stoplight::Domain::TrafficControl::ErrorRate.new(min_requests: 100)

Examples:

traffic_control = Stoplight::Domain::TrafficControl::ErrorRate.new
config = Stoplight::Domain::Config.new(threshold: 0.6, window_size: 300, traffic_control:)

Instance Method Summary collapse

Constructor Details

#initialize(min_requests: 10) ⇒ ErrorRate

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ErrorRate.

Parameters:

  • min_requests (defaults to: 10)

    Minimum number of requests before traffic control is applied. until this number of requests is reached, the error rate will not be considered.



22
23
24
# File 'lib/stoplight/domain/traffic_control/error_rate.rb', line 22

def initialize(min_requests: 10)
  @min_requests = min_requests
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/stoplight/domain/traffic_control/error_rate.rb', line 45

def ==(other)
  other.is_a?(self.class) && min_requests == other.min_requests
end

#check_compatibility(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
31
32
33
34
# File 'lib/stoplight/domain/traffic_control/error_rate.rb', line 26

def check_compatibility(config)
  if config.window_size.nil?
    CompatibilityResult.incompatible("`window_size` should be set")
  elsif config.threshold < 0 || config.threshold > 1
    CompatibilityResult.incompatible("`threshold` should be between 0 and 1")
  else
    CompatibilityResult.compatible
  end
end

#stop_traffic?(config, metrics) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
# File 'lib/stoplight/domain/traffic_control/error_rate.rb', line 36

def stop_traffic?(config, metrics)
  error_rate = metrics.error_rate
  requests = metrics.requests

  raise ArgumentError, "accepts only windowed metrics" if error_rate.nil? || requests.nil?

  requests >= min_requests && error_rate >= config.threshold
end