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::Error::SocketError,
  ::Mongo::Error::SocketTimeoutError,
  ::Mongo::Error::NeedPrimaryServer
]
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.



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

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

Instance Method Details

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



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

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)
    reconnect!
    retry
  else
    log(:fail, e)
    raise e
  end
end