Class: Awspec::Helper::ClientWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/awspec/helper/client_wrap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(real_client = nil) ⇒ ClientWrap

Returns a new instance of ClientWrap.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/awspec/helper/client_wrap.rb', line 6

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.
  @symbol1 = real_client.class.to_s.split('::').shift(2).push('Errors', 'RequestLimitExceeded').join('::').to_sym
  @symbol2 = real_client.class.to_s.split('::').shift(2).push('Errors', 'Throttling').join('::').to_sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/awspec/helper/client_wrap.rb', line 28

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 == symbol1.to_s || e.class.to_s == symbol2.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

Instance Attribute Details

#backoffObject (readonly)

Returns the value of attribute backoff.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def backoff
  @backoff
end

#backoff_limitObject (readonly)

Returns the value of attribute backoff_limit.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def backoff_limit
  @backoff_limit
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def client
  @client
end

#iterationObject (readonly)

Returns the value of attribute iteration.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def iteration
  @iteration
end

#symbol1Object (readonly)

Returns the value of attribute symbol1.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def symbol1
  @symbol1
end

#symbol2Object (readonly)

Returns the value of attribute symbol2.



5
6
7
# File 'lib/awspec/helper/client_wrap.rb', line 5

def symbol2
  @symbol2
end