Sha256: 1a5e153550b31a133c9b9797dcfcc8a3ee276e345baa649402d9704ee307b640

Contents?: true

Size: 1.38 KB

Versions: 30

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

class ReeStd::Retry
  contract(
    Kwargs[
      max: Integer,
    ],
    Ksplat[
      interval?: Integer,
      max_interval?: Integer,
      backoff_factor?: Integer,
      exceptions?: ArrayOf[SubclassOf[StandardError]],
      retry_block?: Proc,
      retry_if?: Proc,
    ] => Any
  )
  def initialize(max:, **opts)
    @max = max
    @current_attempt = 0

    @interval = opts.fetch(:interval, 1)
    @max_interval = opts.fetch(:max_interval, Float::INFINITY)
    @backoff_factor = opts.fetch(:backoff_factor, 1)

    @exceptions = opts.fetch(:exceptions) { [StandardError].freeze }
    @retry_block = opts.fetch(:retry_block, Proc.new {})
    @retry_if = opts.fetch(:retry_if, Proc.new { true })
  end

  contract(
    Block => Any
  )
  def call(&block)
    block.call
  rescue => e
    raise unless match_error?(e)
    raise unless has_attempts?

    @retry_block.call(@current_attempt, e)

    Kernel.sleep(calculate_retry_interval)

    increment_attemt!
    retry
  end

  private

  def has_attempts?
    @current_attempt < @max
  end

  def increment_attemt!
    @current_attempt += 1
  end

  def calculate_retry_interval
    current_interval = @interval * (@backoff_factor ** @current_attempt)

    [@max_interval, current_interval].min
  end

  def match_error?(e)
    puts @retry_if.call(e)
    @retry_if.call(e) && @exceptions.any? { e.is_a? _1 }
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
ree_lib-1.0.79 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.64 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_std/package/ree_std/retry.rb