Sha256: 65fce66c5a0e809ae7ce5f90042897dc065bc52c88ba0de8449336fedf734050

Contents?: true

Size: 766 Bytes

Versions: 3

Compression:

Stored size: 766 Bytes

Contents

class Retry
  MAX_SLEEP_SEC = 10
  BASE_SLEEP_SEC = 0.5

  # GRPC Generally handles timeouts for us
  # but if the connection is broken we want to retry up until the timeout
  def self.it(stub_factory, rpc, req, timeout, reset)
    attempts = 0
    start_time = Time.now

    begin
      attempts += 1
      return stub_factory.call.send(rpc, req)
    rescue => exception

      if Time.now - start_time > timeout
        raise exception
      end
      sleep_seconds = [BASE_SLEEP_SEC * (2 ** (attempts - 1)), MAX_SLEEP_SEC].min
      sleep_seconds = sleep_seconds * (0.5 * (1 + rand()))
      sleep_seconds = [BASE_SLEEP_SEC, sleep_seconds].max
      puts "Sleep #{sleep_seconds} and Reset"
      sleep sleep_seconds
      reset.call
      retry
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prefab-cloud-ruby-0.0.13 lib/prefab/retry.rb
prefab-cloud-ruby-0.0.12 lib/prefab/retry.rb
prefab-cloud-ruby-0.0.11 lib/prefab/retry.rb