Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add a generic object wrapper Retriable.with_retries() #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ Retriable.retriable base_interval: (200/1000.0), timeout: (500/1000.0) do
end
```

### Using an object wrapper

Instead of using a block, you can also wrap an arbitrary object with retries. Any method call on that
object is going to be wrapped with `Retriable.retriable` if you use this functionality.

```ruby
network_interface = API.new(unreliable_server: 'https://api-server.com')
stubborn_interface = Retriable.with_retries(network_interface, on: Net::TimeoutError)
stubborn_interface.create(id: 123, name: 'Some object that gets created')
```

`Retriable.with_retries` accepts the same options as `Retriable.retriable`.

The wrapped object can then be used just like the object it wraps (all the call return results will
be maintained and returned as-is). If the object returns other objects of the same type, they are _not_
going to be wrapped with a `Retriable::Wrapper` anymore.

### Custom Interval Array

You can also bypass the built-in interval generation and provide your own array of intervals. Supplying your own intervals overrides the `tries`, `base_interval`, `max_interval`, `rand_factor`, and `multiplier` parameters.
Expand Down
13 changes: 9 additions & 4 deletions lib/retriable.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "timeout"
require "retriable/config"
require "retriable/exponential_backoff"
require "retriable/version"
require_relative "retriable/config"
require_relative "retriable/exponential_backoff"
require_relative "retriable/version"
require_relative "retriable/wrapper"

module Retriable
extend self
Expand All @@ -15,7 +16,11 @@ def self.configure
def config
@config ||= Config.new
end


def with_retries(object, retriable_options = {})
Wrapper.new(object, retriable_options)
end

def retriable(opts = {})
tries = opts[:tries] || config.tries
base_interval = opts[:base_interval] || config.base_interval
Expand Down
44 changes: 44 additions & 0 deletions lib/retriable/wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Retriable::Wrapper

# Creates a new Wrapper that will wrap the messages to the wrapped object with
# a +retriable+ block.
#
# If the :methods option is passed, only the methods in the given array will be
# subjected to retries.
def initialize(with_object, options_for_retriable = {})
@o = with_object
@methods = options_for_retriable.delete(:methods)
@retriable_options = options_for_retriable
end

# Returns the wrapped object
def __getobj__
@o
end

# Assists in supporting method_missing
def respond_to_missing?(*a)
@o.respond_to?(*a)
end

# Forwards all methods not defined on the Wrapper to the wrapped object.
def method_missing(*a)
method_name = a[0]
if block_given?
__retrying(method_name) { @o.public_send(*a){|*ba| yield(*ba)} }
else
__retrying(method_name) { @o.public_send(*a) }
end
end

private

# Executes a block within Retriable setup with @retriable_options
def __retrying(method_name_on_delegate)
if @methods.nil? || @methods.include?(method_name_on_delegate)
Retriable.retriable(@retriable_options) { yield }
else
yield
end
end
end
20 changes: 19 additions & 1 deletion spec/retriable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ class TestError < Exception; end
before do
srand 0
end


describe '.with_retries' do
it 'wraps an object with a Wrapper that forwards method calls with arguments and blocks' do
str = 'This is a string'
retriable_string = subject.with_retries(str)

retriable_string.must_be_kind_of(Retriable::Wrapper)
retriable_string.must_respond_to(:gsub)
retriable_string.must_respond_to(:length)

retriable_string.length.must_equal 16
gsubbed = retriable_string.gsub(/s/) do | matched |
matched.must_equal "s"
't'
end
gsubbed.must_equal "Thit it a ttring"
end
end

describe "with sleep disabled" do
before do
Retriable.configure do |c|
Expand Down
57 changes: 57 additions & 0 deletions spec/wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require_relative "spec_helper"

describe Retriable::Wrapper do
subject do
Retriable::Wrapper
end

it 'returns the wrapped object via __setobj__' do
item = :foo
wrapped = subject.new(item)
wrapped.must_be_kind_of subject
unwrapped = wrapped.__getobj__
unwrapped.must_be_kind_of Symbol
end

it 'supports setting retrying methods explicitly' do
raising_class = Class.new do
def self.perform_failing_operation
@tries_failing ||= 0
@tries_failing += 1
raise StandardError.new if @tries_failing < 3
"success"
end

def self.perform_non_retrying_operation
@tries_non_retrying ||= 0
@tries_non_retrying += 1
raise StandardError.new if @tries_non_retrying < 2
end
end

wrapped_raising_class = subject.new(raising_class, methods: [:perform_failing_operation])
wrapped_raising_class.perform_failing_operation.must_equal "success"

expect do
wrapped_raising_class.perform_non_retrying_operation
end.must_raise StandardError
end

it "makes 3 tries when retrying block of code raising StandardError with no arguments" do
$tries = 0

raising_class = Class.new do
def self.perform_failing_operation
$tries += 1
raise StandardError.new
end
end

expect do
wrapped_raising_class = subject.new(raising_class, {})
wrapped_raising_class.perform_failing_operation
end.must_raise StandardError

expect($tries).must_equal 3
end
end