Class: TestRail::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/files/testrail_apiclient_retry.rb

Instance Method Summary collapse

Instance Method Details

#send_post_retry(uri, data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/files/testrail_apiclient_retry.rb', line 11

def send_post_retry(uri, data)
  # Gurock api's often deadlocks with errors like this:
  #   TestRail API returned HTTP 500 ("Deadlock found when trying to get lock; try restarting transaction")
  # So if they say to retry, then that's what we will do

  # Please note that the API is rate limited on TestRail Hosted and may throttle requests.
  # TestRail might also return a 429 Too Many Requests response which you are expected to handle.
  # Such a response also includes a Retry-After header indicating how many seconds to wait
  # before you are allowed to submit the next request.
  # http://docs.gurock.com/testrail-api2/introduction #Rate Limit
  response = nil
  # todo: use header [Retry-After] secs
  # for HTTPTooManyRequests 429 error, retry post after either 10s, 30s, 90s or 270s.
  exponential_backoff_seconds = 10

  4.times do
    begin
      response = send_post(uri, data)
      break
    rescue TestRail::APIError => e
      if e.message && e.message.match("HTTP 500.*Deadlock")
        sleep 1
      elsif e.message && e.message.match("HTTP 429")
        puts "TestRail rate limited. retrying in #{exponential_backoff_seconds}"
        sleep exponential_backoff_seconds
        exponential_backoff_seconds *= 3
      elsif e.message && e.message.match("Net::OpenTimeout")
        sleep 1
      else
        # Don't retry it, let the exception propagate
        raise
      end
    end
  end
  response
end