Class: Sequence::HttpWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/sequence/http_wrapper.rb

Constant Summary collapse

RETRY_BASE_DELAY_MS =

Parameters to the retry exponential backoff function.

40
RETRY_MAX_DELAY_MS =
20_000
RETRY_TIMEOUT_SECS =

2 minutes

120
NETWORK_ERRORS =
[
  InvalidRequestIDError,
  SocketError,
  EOFError,
  IOError,
  Timeout::Error,
  Errno::ECONNABORTED,
  Errno::ECONNRESET,
  Errno::ETIMEDOUT,
  Errno::EHOSTUNREACH,
  Errno::ECONNREFUSED,
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url, credential, opts = {}) ⇒ HttpWrapper

Returns a new instance of HttpWrapper.



29
30
31
32
33
34
35
# File 'lib/sequence/http_wrapper.rb', line 29

def initialize(base_url, credential, opts = {})
  @mutex = Mutex.new
  @base_url = URI(base_url)
  @credential = credential
  @opts = opts
  @connection = setup_connection
end

Instance Method Details

#post(id, url, body) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sequence/http_wrapper.rb', line 37

def post(id, url, body)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  attempts = 0
  idempotency_key = SecureRandom.uuid
  begin
    attempts += 1
    # If this is a retry and not the first attempt, sleep before making the
    # retry request.
    sleep(backoff_delay(attempts)) if attempts > 1

    attempt_id = "#{id}/#{attempts}"
    @mutex.synchronize do
      req = Net::HTTP::Post.new(url)
      req.body = JSON.dump(body)
      req['Accept'] = 'application/json'
      req['Content-Type'] = 'application/json'
      req['Id'] = attempt_id
      req['Idempotency-Key'] = idempotency_key
      req['Name-Set'] = 'snake'
      req['User-Agent'] = 'sequence-sdk-ruby/' + Sequence::VERSION
      req['Credential'] = @credential
      if !@opts[:user].nil? && !@opts[:pass].nil?
        req.basic_auth(@opts[:user], @opts[:pass])
      end
      unless @connection.started?
        @connection.start
      end
      response = @connection.request(req)

      if block_given?
        yield response
      end

      # We must parse any APIErrors here so that
      # the retry logic can handle them.
      status = Integer(response.code)
      parsed_body = nil
      if status != 204 # No Content
        begin
          parsed_body = JSON.parse(response.body)
        rescue JSON::JSONError
          raise JSONError.new(attempt_id, response)
        end
      end
      if status / 100 != 2
        status == 401 ? klass = UnauthorizedError : klass = APIError
        raise klass.new(parsed_body, response)
      end

      { parsed_body: parsed_body, response: response }
    end
  rescue *NETWORK_ERRORS => e
    raise e if elapsed_secs(start_time) > RETRY_TIMEOUT_SECS
    retry
  rescue APIError => e
    raise e unless e.retriable?
    raise e if elapsed_secs(start_time) > RETRY_TIMEOUT_SECS
    retry
  end
end