Skip to content

Instantly share code, notes, and snippets.

@yagihiro
Last active February 4, 2020 09:48
Show Gist options
  • Save yagihiro/d96588fa5c5188d46bcc20fb2b9ece63 to your computer and use it in GitHub Desktop.
Save yagihiro/d96588fa5c5188d46bcc20fb2b9ece63 to your computer and use it in GitHub Desktop.
再送する時などに使用する Exponential Backoff の Ruby 実装です
# gem install retryable
require 'retryable'
# see: https://aws.typepad.com/sajp/2015/03/backoff.html
# ↑の full jitter back-off algorithm です
def with_retry
base = 1.0
cap = 12.0
Retryable.retryable(tries: 5, sleep: ->(n) { rand(base..([cap, base * (2 ** n)].min)) }) { yield }
end
# response = with_retry { @api.post_xxx }
# puts response.body
# => { result: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment