Class: Excon::Middleware::Idempotent

Inherits:
Base
  • Object
show all
Defined in:
lib/excon/middlewares/idempotent.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #response_call

Constructor Details

This class inherits a constructor from Excon::Middleware::Base

Class Method Details

.valid_parameter_keysObject



7
8
9
10
11
12
13
14
15
# File 'lib/excon/middlewares/idempotent.rb', line 7

def self.valid_parameter_keys
  [
    :idempotent,
    :retries_remaining,
    :retry_errors,
    :retry_interval,
    :retry_limit
  ]
end

Instance Method Details

#error_call(datum) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/excon/middlewares/idempotent.rb', line 22

def error_call(datum)
  if datum[:idempotent]
    if datum.has_key?(:request_block)
      if datum[:request_block].respond_to?(:rewind)
        datum[:request_block].rewind
      else
        Excon.display_warning('Excon requests with a :request_block must implement #rewind in order to be :idempotent.')
        datum[:idempotent] = false
      end
    end
    if datum.has_key?(:response_block) && datum[:response_block].respond_to?(:rewind)
      datum[:response_block].rewind
    end
    if datum.has_key?(:pipeline)
      Excon.display_warning("Excon requests can not be :idempotent when pipelining.")
      datum[:idempotent] = false
    end
  end

  if datum[:idempotent] && datum[:retry_errors].any? {|ex| datum[:error].kind_of?(ex) } && datum[:retries_remaining] > 1

    sleep(datum[:retry_interval]) if datum[:retry_interval]

    # reduces remaining retries, reset connection, and restart request_call
    datum[:retries_remaining] -= 1
    connection = datum.delete(:connection)
    valid_keys = Set.new(connection.valid_request_keys(datum[:middlewares]))
    datum.select! {|key, _| valid_keys.include?(key) }
    connection.request(datum)
  else
    @stack.error_call(datum)
  end
end

#request_call(datum) ⇒ Object



17
18
19
20
# File 'lib/excon/middlewares/idempotent.rb', line 17

def request_call(datum)
  datum[:retries_remaining] ||= datum[:retry_limit]
  @stack.request_call(datum)
end