Class: Retriable::Config

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/retriable/config.rb,
sig/retriable.rbs

Constant Summary collapse

ATTRIBUTES =

Returns:

  • (Array[Symbol])
(ExponentialBackoff::ATTRIBUTES + %i[
  sleep_disabled
  max_elapsed_time
  intervals
  on
  retry_if
  on_retry
  on_give_up
  contexts
]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

unbounded_tries?

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • opts (Hash[Symbol, untyped]) (defaults to: {})


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_intervalNumeric

Returns the value of attribute base_interval.

Returns:

  • (Numeric)


15
16
17
# File 'sig/retriable.rbs', line 15

def base_interval
  @base_interval
end

#contextsHash[Symbol, untyped]

Returns the value of attribute contexts.

Returns:

  • (Hash[Symbol, untyped])


26
27
28
# File 'sig/retriable.rbs', line 26

def contexts
  @contexts
end

#intervalsArray[Numeric]?

Returns the value of attribute intervals.

Returns:

  • (Array[Numeric], nil)


21
22
23
# File 'sig/retriable.rbs', line 21

def intervals
  @intervals
end

#max_elapsed_timeNumeric?

Returns the value of attribute max_elapsed_time.

Returns:

  • (Numeric, nil)


20
21
22
# File 'sig/retriable.rbs', line 20

def max_elapsed_time
  @max_elapsed_time
end

#max_intervalNumeric

Returns the value of attribute max_interval.

Returns:

  • (Numeric)


16
17
18
# File 'sig/retriable.rbs', line 16

def max_interval
  @max_interval
end

#multiplierNumeric

Returns the value of attribute multiplier.

Returns:

  • (Numeric)


18
19
20
# File 'sig/retriable.rbs', line 18

def multiplier
  @multiplier
end

#onObject

Returns the value of attribute on.

Returns:

  • (Object)


22
23
24
# File 'sig/retriable.rbs', line 22

def on
  @on
end

#on_give_upObject

Returns the value of attribute on_give_up.

Returns:

  • (Object)


25
26
27
# File 'sig/retriable.rbs', line 25

def on_give_up
  @on_give_up
end

#on_retryObject

Returns the value of attribute on_retry.

Returns:

  • (Object)


24
25
26
# File 'sig/retriable.rbs', line 24

def on_retry
  @on_retry
end

#rand_factorNumeric

Returns the value of attribute rand_factor.

Returns:

  • (Numeric)


17
18
19
# File 'sig/retriable.rbs', line 17

def rand_factor
  @rand_factor
end

#retry_ifObject

Returns the value of attribute retry_if.

Returns:

  • (Object)


23
24
25
# File 'sig/retriable.rbs', line 23

def retry_if
  @retry_if
end

#sleep_disabledBoolean

Returns the value of attribute sleep_disabled.

Returns:

  • (Boolean)


19
20
21
# File 'sig/retriable.rbs', line 19

def sleep_disabled
  @sleep_disabled
end

#triesNumeric

Returns the value of attribute tries.

Returns:

  • (Numeric)


14
15
16
# File 'sig/retriable.rbs', line 14

def tries
  @tries
end

Instance Method Details

#freezeObject

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_hHash[Symbol, untyped]

Returns:

  • (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

  validate_backoff_options
end