Module: Tins::Attempt
- Included in:
- Object
- Defined in:
- lib/tins/attempt.rb
Overview
A module that provides functionality for attempting operations with error handling and retry logic.
Instance Method Summary collapse
-
#attempt(opts = {}) {|count, prev_exception| ... } ⇒ Boolean?
Executes the block up to a configured number of attempts, with optional sleeping between retries and exception-based success detection.
-
#compute_duration_base(sleep, attempts) ⇒ Float
private
Computes the base for exponential backoff that results in a specific total sleep duration.
-
#interpret_sleep(sleep, attempts) ⇒ Proc?
private
The interpret_sleep method determines the sleep behavior for retry attempts.
-
#sleep_duration(duration, count) ⇒ Object
private
The sleep_duration method handles sleeping for a specified duration or based on a proc call.
Instance Method Details
#attempt(opts = {}) {|count, prev_exception| ... } ⇒ Boolean?
Executes the block up to a configured number of attempts, with optional sleeping between retries and exception-based success detection.
When :exception_class is set (default StandardError), the block
succeeds if it returns without raising. When it is nil, the block's
truthy return value determines success instead.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tins/attempt.rb', line 54 def attempt(opts = {}, &block) sleep = nil exception_class = StandardError prev_exception = nil if Numeric === opts attempts = opts else attempts = opts[:attempts] || 1 attempts >= 1 or raise ArgumentError, 'at least one attempt is required' exception_class = opts[:exception_class] if opts.key?(:exception_class) sleep = interpret_sleep(opts[:sleep], attempts) reraise = opts[:reraise] end return if attempts <= 0 count = 0 if exception_class.nil? begin count += 1 if block.call(count, prev_exception) return true elsif count < attempts sleep_duration(sleep, count) end end until count == attempts false else begin count += 1 block.call(count, prev_exception) true rescue *exception_class if count < attempts prev_exception = $! sleep_duration(sleep, count) retry end case reraise when Proc reraise.($!) when Exception.class raise reraise, "reraised: #{$!.}" when true raise $!, "reraised: #{$!.}" else false end end end end |
#compute_duration_base(sleep, attempts) ⇒ Float (private)
Computes the base for exponential backoff that results in a specific total sleep duration.
This method solves for the base x in the geometric series:
x^0 + x^1 + x^2 + ... + x^(attempts-1) = sleep
The solution ensures that when using exponential delays with base x,
the total time spent across all attempts equals approximately the
specified sleep duration. This method of computation is used if a
negative number of secondes was requested in the attempt method.
all attempts invalid iterations
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tins/attempt.rb', line 140 def compute_duration_base(sleep, attempts) x1, x2 = 1, sleep attempts <= sleep or raise ArgumentError, "need less or equal number of attempts than sleep duration #{sleep}" x1 >= x2 and raise ArgumentError, "invalid sleep argument: #{sleep.inspect}" function = -> x { (0...attempts).inject { |s, i| s + x ** i } - sleep } f, fmid = function[x1], function[x2] f * fmid >= 0 and raise ArgumentError, "invalid sleep argument: #{sleep.inspect}" n = 1 << 16 epsilon = 1E-16 root = if f < 0 dx = x2 - x1 x1 else dx = x1 - x2 x2 end n.times do fmid = function[xmid = root + (dx *= 0.5)] fmid < 0 and root = xmid dx.abs < epsilon or fmid == 0 and return root end raise ArgumentError, "too many iterations (#{n})" result end |
#interpret_sleep(sleep, attempts) ⇒ Proc? (private)
The interpret_sleep method determines the sleep behavior for retry attempts.
between retries, nil if no sleep was requested no sleep is needed
than 3 attempts
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/tins/attempt.rb', line 178 def interpret_sleep(sleep, attempts) case sleep when nil when Numeric if sleep < 0 if attempts > 2 sleep = -sleep duration_base = compute_duration_base sleep, attempts sleep = lambda { |i| duration_base ** i } else raise ArgumentError, "require > 2 attempts for negative sleep value" end end sleep when Proc sleep else raise TypeError, "require Proc or Numeric sleep argument" end end |
#sleep_duration(duration, count) ⇒ Object (private)
The sleep_duration method handles sleeping for a specified duration or based on a proc call.
returns the duration
111 112 113 114 115 116 117 118 |
# File 'lib/tins/attempt.rb', line 111 def sleep_duration(duration, count) case duration when Numeric sleep duration when Proc sleep duration.call(count) end end |