Class: Retriable::Config
- Inherits:
-
Object
- Object
- Retriable::Config
- Includes:
- Validation
- Defined in:
- lib/retriable/config.rb,
sig/retriable.rbs
Constant Summary collapse
- ATTRIBUTES =
(ExponentialBackoff::ATTRIBUTES + %i[ sleep_disabled max_elapsed_time intervals on retry_if on_retry on_give_up contexts ]).freeze
Instance Attribute Summary collapse
-
#base_interval ⇒ Numeric
Returns the value of attribute base_interval.
-
#contexts ⇒ Hash[Symbol, untyped]
Returns the value of attribute contexts.
-
#intervals ⇒ Array[Numeric]?
Returns the value of attribute intervals.
-
#max_elapsed_time ⇒ Numeric?
Returns the value of attribute max_elapsed_time.
-
#max_interval ⇒ Numeric
Returns the value of attribute max_interval.
-
#multiplier ⇒ Numeric
Returns the value of attribute multiplier.
-
#on ⇒ Object
Returns the value of attribute on.
-
#on_give_up ⇒ Object
Returns the value of attribute on_give_up.
-
#on_retry ⇒ Object
Returns the value of attribute on_retry.
-
#rand_factor ⇒ Numeric
Returns the value of attribute rand_factor.
-
#retry_if ⇒ Object
Returns the value of attribute retry_if.
-
#sleep_disabled ⇒ Boolean
Returns the value of attribute sleep_disabled.
-
#tries ⇒ Numeric
Returns the value of attribute tries.
Instance Method Summary collapse
-
#freeze ⇒ Object
Deep-freezes the containers this Config owns, then itself.
-
#initialize(opts = {}) ⇒ Config
constructor
A new instance of Config.
- #to_h ⇒ Hash[Symbol, untyped]
- #validate! ⇒ void
Methods included from Validation
Constructor Details
#initialize(opts = {}) ⇒ Config
Returns a new instance of Config.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/retriable/config.rb', line 29 def initialize(opts = {}) defaults = ExponentialBackoff::DEFAULTS @tries = defaults[:tries] @base_interval = defaults[:base_interval] @max_interval = defaults[:max_interval] @rand_factor = defaults[:rand_factor] @multiplier = defaults[:multiplier] @sleep_disabled = false @max_elapsed_time = 900 # 15 min @intervals = nil @on = [StandardError] @retry_if = nil @on_retry = nil @on_give_up = nil @contexts = {} opts.each do |k, v| raise ArgumentError, "#{k} is not a valid option" if !ATTRIBUTES.include?(k) instance_variable_set(:"@#{k}", v) end validate! end |
Instance Attribute Details
#base_interval ⇒ Numeric
Returns the value of attribute base_interval.
15 16 17 |
# File 'sig/retriable.rbs', line 15 def base_interval @base_interval end |
#contexts ⇒ Hash[Symbol, untyped]
Returns the value of attribute contexts.
26 27 28 |
# File 'sig/retriable.rbs', line 26 def contexts @contexts end |
#intervals ⇒ Array[Numeric]?
Returns the value of attribute intervals.
21 22 23 |
# File 'sig/retriable.rbs', line 21 def intervals @intervals end |
#max_elapsed_time ⇒ Numeric?
Returns the value of attribute max_elapsed_time.
20 21 22 |
# File 'sig/retriable.rbs', line 20 def max_elapsed_time @max_elapsed_time end |
#max_interval ⇒ Numeric
Returns the value of attribute max_interval.
16 17 18 |
# File 'sig/retriable.rbs', line 16 def max_interval @max_interval end |
#multiplier ⇒ Numeric
Returns the value of attribute multiplier.
18 19 20 |
# File 'sig/retriable.rbs', line 18 def multiplier @multiplier end |
#on ⇒ Object
Returns the value of attribute on.
22 23 24 |
# File 'sig/retriable.rbs', line 22 def on @on end |
#on_give_up ⇒ Object
Returns the value of attribute on_give_up.
25 26 27 |
# File 'sig/retriable.rbs', line 25 def on_give_up @on_give_up end |
#on_retry ⇒ Object
Returns the value of attribute on_retry.
24 25 26 |
# File 'sig/retriable.rbs', line 24 def on_retry @on_retry end |
#rand_factor ⇒ Numeric
Returns the value of attribute rand_factor.
17 18 19 |
# File 'sig/retriable.rbs', line 17 def rand_factor @rand_factor end |
#retry_if ⇒ Object
Returns the value of attribute retry_if.
23 24 25 |
# File 'sig/retriable.rbs', line 23 def retry_if @retry_if end |
#sleep_disabled ⇒ Boolean
Returns the value of attribute sleep_disabled.
19 20 21 |
# File 'sig/retriable.rbs', line 19 def sleep_disabled @sleep_disabled end |
#tries ⇒ Numeric
Returns the value of attribute tries.
14 15 16 |
# File 'sig/retriable.rbs', line 14 def tries @tries end |
Instance Method Details
#freeze ⇒ Object
Deep-freezes the containers this Config owns, then itself. Without the deep
part a "frozen" Config stays mutable one level down
(config.contexts[:api][:tries] = 1), which is precisely the corruption a
published snapshot exists to rule out. Leaves — procs, exception classes,
regexps, scalars — are shared by reference and left untouched.
Retriable only ever freezes a #dup it produced itself, so this never freezes a container the caller still holds.
86 87 88 89 90 91 92 93 |
# File 'lib/retriable/config.rb', line 86 def freeze return self if frozen? OWNED_CONTAINER_ATTRIBUTES.each do |attribute| deep_freeze(instance_variable_get(:"@#{attribute}")) end super end |
#to_h ⇒ Hash[Symbol, untyped]
55 56 57 |
# File 'lib/retriable/config.rb', line 55 def to_h ATTRIBUTES.to_h { |key| [key, public_send(key)] } end |
#validate! ⇒ void
This method returns an undefined value.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/retriable/config.rb', line 59 def validate! validate_contexts validate_callable(:retry_if, retry_if) validate_callable(:on_retry, on_retry) validate_callable(:on_give_up, on_give_up) validate_on(on) validate_intervals if unbounded_tries?(tries) validate_unbounded_tries else validate_optional_non_negative_number(:max_elapsed_time, max_elapsed_time) return if intervals validate_positive_integer(:tries, tries) end end |