Skip to content

Commit

Permalink
remove lambda from perform_http_call method
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 26, 2024
1 parent 127e505 commit 82207ac
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions lib/datadog/ci/transport/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,7 @@ def request(
private

def perform_http_call(path:, payload:, headers:, verb:, retries: MAX_RETRIES, backoff: INITIAL_BACKOFF)
# retry logic as lambda to avoid duplication
retry_request = ->(previous_response, current_backoff) do
if retries.positive? && backoff <= MAX_BACKOFF
sleep(backoff)

perform_http_call(
path: path,
payload: payload,
headers: headers,
verb: verb,
retries: retries - 1,
backoff: current_backoff * 2
)
else
Datadog.logger.error(
"Failed to send request after #{MAX_RETRIES - retries} retries (current backoff value #{backoff})"
)

previous_response
end
end
response = nil

begin
response = adapter.call(
Expand All @@ -113,19 +93,34 @@ def perform_http_call(path:, payload:, headers:, verb:, retries: MAX_RETRIES, ba
Datadog.logger.debug do
"Received rate limit response, retrying in #{backoff} seconds from X-RateLimit-Reset header"
end

retry_request.call(response, backoff)
elsif response.server_error?
Datadog.logger.debug { "Received server error response, retrying in #{backoff} seconds" }

retry_request.call(response, backoff)
else
response
return response
end
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, SocketError, Net::HTTPBadResponse => e
Datadog.logger.debug { "Failed to send request with #{e} (#{e.message})" }

retry_request.call(ErrorResponse.new(e), backoff)
response = ErrorResponse.new(e)
end

if retries.positive? && backoff <= MAX_BACKOFF
sleep(backoff)

perform_http_call(
path: path,
payload: payload,
headers: headers,
verb: verb,
retries: retries - 1,
backoff: backoff * 2
)
else
Datadog.logger.error(
"Failed to send request after #{MAX_RETRIES - retries} retries (current backoff value #{backoff})"
)

response
end
end

Expand Down

0 comments on commit 82207ac

Please sign in to comment.