Class: Mongo::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/retry.rb

Constant Summary collapse

DEFAULT_RETRY_SLEEPS =
[1, 5, 10].freeze
DEFAULT_RETRYABLE_EXCEPTIONS =
[
  ::Mongo::ConnectionError,
  ::Mongo::ConnectionTimeoutError,
  ::Mongo::ConnectionFailure,
  ::Mongo::OperationTimeout
]
DEFAULT_OPTIONS =
{
  delayer: Kernel.method(:sleep),
  retries: DEFAULT_RETRY_SLEEPS,
  exceptions: DEFAULT_RETRYABLE_EXCEPTIONS,
  retry_exceptions: DEFAULT_RETRYABLE_EXCEPTIONS,
  logger: nil
}

Instance Method Summary collapse

Constructor Details

#initialize(connection, options = {}) ⇒ Retry

Returns a new instance of Retry.



21
22
23
24
# File 'lib/mongo/retry.rb', line 21

def initialize(connection, options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @connection = connection
end

Instance Method Details

#connection_guard(retries = @options[:retries].dup) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongo/retry.rb', line 26

def connection_guard(retries = @options[:retries].dup)
  yield
rescue *@options[:exceptions] => e
  if retry_timeout = retries.pop
    log(:retry, e)
    @options[:delayer].call(retry_timeout)
    refresh!
    retry
  else
    log(:fail, e)
    raise e
  end
end