Last active
February 4, 2020 09:48
-
-
Save yagihiro/d96588fa5c5188d46bcc20fb2b9ece63 to your computer and use it in GitHub Desktop.
再送する時などに使用する Exponential Backoff の Ruby 実装です
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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