Last active
February 24, 2022 09:39
-
-
Save mfittko/7dbde1d1a326a54438a0a14e1c6ad8d5 to your computer and use it in GitHub Desktop.
Ruby: Retry API calls
This file contains 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
def validate_billing_agreement(billing_agreement_id) | |
with_retry do | |
handle_response(client.validate_billing_agreement(billing_agreement_id))&.data | |
end | |
end | |
def with_retry(initial_delay: 0.1, max_seconds: 10) | |
seconds ||= initial_delay | |
yield | |
rescue RuntimeError => e | |
raise e if seconds > max_seconds | |
sleep seconds | |
seconds *= 2 | |
retry | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment