lib/resque/plugins/exponential_backoff.rb in resque-retry-1.1.1 vs lib/resque/plugins/exponential_backoff.rb in resque-retry-1.1.4
- old
+ new
@@ -35,10 +35,38 @@
# end
#
module ExponentialBackoff
include Resque::Plugins::Retry
+ # Raised if the min/max retry-delay multiplicand configuration is invalid
+ #
+ # @api public
+ class InvalidRetryDelayMultiplicandConfigurationException < StandardError; end
+
+ # Constants
+ #
+ # @api public
+ DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN = 1.0
+ DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX = 1.0
+
+ # Fail fast, when extended, if the "receiver" is misconfigured
+ #
+ # @api private
+ def self.extended(receiver)
+ retry_delay_multiplicand_min = \
+ receiver.instance_variable_get("@retry_delay_multiplicand_min") || \
+ DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN
+ retry_delay_multiplicand_max = \
+ receiver.instance_variable_get("@retry_delay_multiplicand_max") || \
+ DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX
+ if retry_delay_multiplicand_min > retry_delay_multiplicand_max
+ raise InvalidRetryDelayMultiplicandConfigurationException.new(
+ %{"@retry_delay_multiplicand_min" must be less than or equal to "@retry_delay_multiplicand_max"}
+ )
+ end
+ end
+
# Defaults to the number of delays in the backoff strategy
#
# @return [Number] maximum number of retries
#
# @api private
@@ -50,14 +78,39 @@
#
# @return [Number] seconds to delay until the next retry.
#
# @api private
def retry_delay
- backoff_strategy[retry_attempt] || backoff_strategy.last
+ delay = backoff_strategy[retry_attempt] || backoff_strategy.last
+ delay_multiplicand = \
+ rand(retry_delay_multiplicand_min..retry_delay_multiplicand_max)
+ (delay * delay_multiplicand).to_i
end
# @abstract
+ # The minimum value (lower-bound) for the range that is is used in
+ # calculating the retry-delay product
+ #
+ # @return [Float]
+ #
+ # @api public
+ def retry_delay_multiplicand_min
+ @retry_delay_multiplicand_min ||= DEFAULT_RETRY_DELAY_MULTIPLICAND_MIN
+ end
+
+ # @abstract
+ # The maximum value (upper-bound) for the range that is is used in
+ # calculating the retry-delay product
+ #
+ # @return [Float]
+ #
+ # @api public
+ def retry_delay_multiplicand_max
+ @retry_delay_multiplicand_max ||= DEFAULT_RETRY_DELAY_MULTIPLICAND_MAX
+ end
+
+ # @abstract
# The backoff strategy is used to vary the delay between retry attempts
#
# @return [Array] array of delays. index = retry attempt
#
# @api public
@@ -65,6 +118,6 @@
@backoff_strategy ||= [0, 60, 600, 3600, 10_800, 21_600]
end
end
end
-end
\ No newline at end of file
+end