Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Retry 3 times with a 3 second backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Dec 15, 2016
1 parent 7ce76c6 commit ddbc81f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/timber/log_devices/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class HTTP
https.open_timeout = 10
end
DELIVERY_FREQUENCY_SECONDS = 2.freeze
RETRY_LIMIT = 3.freeze
BACKOFF_RATE_SECONDS = 3.freeze


# Instantiates a new HTTP log device.
Expand Down Expand Up @@ -74,21 +76,25 @@ def close
private
def deliver(body)
Thread.new do
request = Net::HTTP::Post.new(API_URI.request_uri).tap do |req|
req['Authorization'] = authorization_payload
req['Connection'] = CONNECTION_HEADER
req['Content-Type'] = CONTENT_TYPE
req['User-Agent'] = USER_AGENT
req.body = body
end
RETRY_LIMIT.times do |try_index|
request = Net::HTTP::Post.new(API_URI.request_uri).tap do |req|
req['Authorization'] = authorization_payload
req['Connection'] = CONNECTION_HEADER
req['Content-Type'] = CONTENT_TYPE
req['User-Agent'] = USER_AGENT
req.body = body
end

HTTPS.request(request).tap do |res|
res = HTTPS.request(request)
code = res.code.to_i
if code < 200 || code >= 300
raise DeliveryError.new("Timber HTTP delivery failed - #{res.code}: #{res.body}")
Config.instance.logger.debug("Timber HTTP delivery failed - #{res.code}: #{res.body}")
sleep((try_index + 1) * BACKOFF_RATE_SECONDS)
else
@buffer.remove(body)
Config.instance.logger.debug("Timber HTTP delivery successful - #{code}")
break # exit the loop
end
@buffer.remove(body)
Config.instance.logger.debug("Timber HTTP delivery successful - #{code}")
end
end
end
Expand Down

0 comments on commit ddbc81f

Please sign in to comment.