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



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

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

#read_with_retry {|server| ... } ⇒ Result

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:

  • block (Proc)

    The block to execute.

Yield Parameters:

  • server (Server)

    The server to which the write should be sent.

Returns:

  • (Result)

    The result of the operation.

Since:

  • 2.1.0



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

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_retry(session, write_concern, &block) ⇒ Result

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



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

def write_with_retry(session, write_concern, &block)
  unless retry_write_allowed?(session, write_concern)
    return legacy_write_with_retry(&block)
  end

  server = cluster.next_primary
  unless server.retry_writes?
    return legacy_write_with_retry(server, &block)
  end

  begin
    txn_num = session.next_txn_num
    yield(server, txn_num)
  rescue Error::SocketError, Error::SocketTimeoutError => e
    retry_write(e, txn_num, &block)
  rescue Error::OperationFailure => e
    raise e unless e.write_retryable?
    retry_write(e, txn_num, &block)
  end
end