Module: Pandoru::Transport::Retries

Defined in:
lib/pandoru/transport.rb

Overview

Function decorator implementing retrying logic for handling connection errors

Class Method Summary collapse

Class Method Details

.delay_exponential(base, growth_factor, attempts) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/pandoru/transport.rb', line 45

def self.delay_exponential(base, growth_factor, attempts)
  if base == "rand"
    base = rand
  elsif base <= 0
    raise ArgumentError, "Base must be greater than 0"
  end
  
  base * (growth_factor ** (attempts - 1))
end

.retry_on_error(max_tries = 3, exceptions = [StandardError]) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pandoru/transport.rb', line 26

def self.retry_on_error(max_tries = 3, exceptions = [StandardError])
  lambda do |method|
    lambda do |*args, &block|
      attempts = 0
      begin
        attempts += 1
        method.call(*args, &block)
      rescue *exceptions => e
        if attempts < max_tries
          sleep(delay_exponential(0.5, 2, attempts))
          retry
        else
          raise e
        end
      end
    end
  end
end