Sha256: 6a989d8ae5bc378bb93111267f4989df11d460166a95380d8efdab0d329e1753

Contents?: true

Size: 1.61 KB

Versions: 13

Compression:

Stored size: 1.61 KB

Contents

require 'awspec/config'

module Awspec::Helper
  class ClientWrap
    attr_reader :client, :backoff, :iteration, :backoff_limit, :symbol
    def initialize(real_client = nil)
      raise ArgumentError, 'Client can not be nil' if real_client.nil?
      config = Awspec::Config.instance
      @client        = real_client
      @backoff       = config[:client_backoff]
      @orig_backoff  = @backoff
      @iteration     = config[:client_iteration]
      @orig_iter     = @iteration
      @backoff_limit = config[:client_backoff_limit]
      # build the symbol we'll use to compare to any errors caught in method_missing
      # below.
      @symbol = real_client.class.to_s.split('::').shift(2).push('Errors', 'RequestLimitExceeded').join('::').to_sym
    end

    protected

    # used to capture only the "RequestLimitExceeded" error from an aws
    # client api call.  In the case of matching it we want to try again,
    # backing off successively each time, until the backoff_limit is reached or
    # exceeded, in which case, the error will be re-raised and it should fail
    # as expected.
    def method_missing(m, *args, &block)
      begin
        results = client.send(m, *args, &block)
      rescue Exception => e # rubocop:disable Lint/RescueException
        raise unless e.class.to_s == symbol.to_s && backoff < backoff_limit

        @backoff = backoff + (iteration * iteration * 0.5)
        @iteration += 1
        sleep backoff
        results = self.send(m, *args, &block)
      end

      reset_backoff

      results
    end

    def reset_backoff
      @backoff   = @orig_backoff
      @iteration = @orig_iter
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
awspec-1.19.0 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.2.4 lib/awspec/helper/client_wrap.rb
awspec-api_gateway_extended-1.2.4 lib/awspec/helper/client_wrap.rb
awspec-api_gateway_extended-1.2.3 lib/awspec/helper/client_wrap.rb
awspec-1.18.6 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.2.2 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.2.1 lib/awspec/helper/client_wrap.rb
awspec-1.18.5 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.2.0 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.1.0 lib/awspec/helper/client_wrap.rb
cthiesfork-awspec-1.0.0 lib/awspec/helper/client_wrap.rb
awspec-1.18.4 lib/awspec/helper/client_wrap.rb
awspec-1.18.3 lib/awspec/helper/client_wrap.rb