Ruby Retry Made Better

Introducing Faraday Loop for Enhanced Code Block Retries and Exception Handling

Ever need a simple way to retry a block of code? Maybe you need a maximum number of retries? Exponential backoff? Different handling for different exceptions?

Faraday has one!

But, while Faraday is one nice piece of kit, you might not be in the market for an http client like Faraday and all of its Middelware.

So we took the Faraday Retry Middleware, and just simply cloned it into its own utility: Faraday Loop.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add faraday_loop

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install faraday_loop

Usage

You'll first need to require it:

require 'faraday_loop'

Then, create a new instance of the retry utility, passing any options as needed:

retry_utility = FaradayLoop::Retry.new(
  max: 3,
  interval: 1,
  max_interval: 10,
  interval_randomness: 0.5,
  backoff_factor: 2,
  exceptions: [MyCustomException],
  retry_if: ->(e) { e.is_a?(MyCustomException) },
  retry_block: ->(retries, e) { puts "Retry #{retries}: #{e.message}" }
)
  • max: The maximum number of retries (default: 2)
  • interval: The initial interval between retries in seconds (default: 0)
  • max_interval: The maximum interval between retries in seconds (default: Float::MAX)
  • interval_randomness: A factor to add randomness to the retry intervals (default: 0)
  • backoff_factor: The factor to multiply the interval by for each retry (default: 1)
  • exceptions: An array of exception classes to retry (default: Faraday::RetriableExceptions)
  • retry_if: A lambda that returns true if the exception should be retried (default: checks if the exception is in the exceptions array)
  • retry_block: An optional lambda that is called before each retry, passing in the current retry count and the exception raised

Finally, wrap the operation you want to retry in a block:

retry_utility.call do
  # Your operation that might fail and require retries
end

You can also shortcut this with:

retry_utility = FaradayLoop::Retry(
  options
).retry do
    # Your operation that might fail and require retries
end

License

The gem is available as open source under the terms of the MIT License.

Feedback

Source code available on Github. Feedback and pull requests are greatly appreciated. Let us know if we can improve this.

From

👋 The folks at lostisland really made this. We at Census just hoisted it up.

P.S. We’re hiring. Come say hi!

Nate Kontny

Nate is a hands-on developer building products and running companies. His previous experience as the CEO of Highrise and CTO of Inkling helped him hone his skills as a software designer, engineer, founder, writer & vlogger. You should follow him on YouTube: here.

http://youtube.com/nathankontny
Previous
Previous

User Story Mapping: A Hackathon Game-Changer

Next
Next

NoSQL Isn't Dead, But It Left a Lasting Legacy