Module: Mongo::Retryable

Overview

Defines basic behaviour around retrying operations.

Since:

  • 2.1.0

Instance Method Summary collapse

Instance Method Details

#read_with_one_retryResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This only retries read operations on socket errors.

Execute a read operation with a single retry.

Examples:

Execute the read.

read_with_one_retry do
  ...
end

Parameters:

  • block (Proc)

    The block to execute.

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.2.6



77
78
79
80
81
# File 'lib/mongo/retryable.rb', line 77

def read_with_one_retry
  yield
rescue Error::SocketError, Error::SocketTimeoutError
  yield
end

#read_with_retryResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This only retries read operations on socket errors.

Execute a read operation with a retry.

Examples:

Execute the read.

read_with_retry do
  ...
end

Parameters:

  • attempt (Integer)

    The retry attempt count - for internal use.

  • block (Proc)

    The block to execute.

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.1.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo/retryable.rb', line 39

def read_with_retry
  attempt = 0
  begin
    attempt += 1
    yield
  rescue Error::SocketError, Error::SocketTimeoutError => e
    raise(e) if attempt > cluster.max_read_retries
    log_retry(e)
    cluster.scan!
    retry
  rescue Error::OperationFailure => e
    if cluster.sharded? && e.retryable?
      raise(e) if attempt > cluster.max_read_retries
      log_retry(e)
      sleep(cluster.read_retry_interval)
      retry
    else
      raise e
    end
  end
end

#write_with_retryResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This only retries operations on not master failures, since it is the only case we can be sure a partial write did not already occur.

Execute a write operation with a retry.

Examples:

Execute the write.

write_with_retry do
  ...
end

Parameters:

  • block (Proc)

    The block to execute.

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.1.0



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mongo/retryable.rb', line 100

def write_with_retry
  attempt = 0
  begin
    attempt += 1
    yield
  rescue Error::OperationFailure => e
    raise(e) if attempt > Cluster::MAX_WRITE_RETRIES
    if e.write_retryable?
      log_retry(e)
      cluster.scan!
      retry
    else
      raise(e)
    end
  end
end