Module: Mongo::Retryable
- Included in:
- BulkWrite, Cluster::CursorReaper, Collection, Collection::View, Collection::View::Aggregation, Collection::View::MapReduce, Cursor, Server::Connection, Server::Monitor::Connection, Session
- Defined in:
- lib/mongo/retryable.rb
Overview
Defines basic behaviour around retrying operations.
Instance Method Summary collapse
-
#read_with_one_retry ⇒ Result
private
Execute a read operation with a single retry.
-
#read_with_retry(session = nil) {|server| ... } ⇒ Result
private
Execute a read operation with a retry.
-
#write_with_retry(session, write_concern, ending_transaction = false, &block) ⇒ Result
private
Execute a write operation with a retry.
Instance Method Details
#read_with_one_retry ⇒ 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.
This only retries read operations on socket errors.
Execute a read operation with a single retry.
79 80 81 82 83 |
# File 'lib/mongo/retryable.rb', line 79 def read_with_one_retry yield rescue Error::SocketError, Error::SocketTimeoutError yield end |
#read_with_retry(session = nil) {|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.
This only retries read operations on socket errors.
Execute a read operation with a retry.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongo/retryable.rb', line 41 def read_with_retry(session = nil) attempt = 0 begin attempt += 1 yield rescue Error::SocketError, Error::SocketTimeoutError => e raise(e) if attempt > cluster.max_read_retries || (session && session.in_transaction?) log_retry(e) cluster.scan! retry rescue Error::OperationFailure => e if cluster.sharded? && e.retryable? && !(session && session.in_transaction?) 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, ending_transaction = false, &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.
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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mongo/retryable.rb', line 104 def write_with_retry(session, write_concern, ending_transaction = false, &block) unless retry_write_allowed?(session, write_concern) || ending_transaction return legacy_write_with_retry(nil, session, &block) end server = cluster.next_primary unless server.retry_writes? || ending_transaction return legacy_write_with_retry(server, session, &block) end begin txn_num = session.in_transaction? ? session.txn_num : session.next_txn_num yield(server, txn_num) rescue Error::SocketError, Error::SocketTimeoutError => e raise e if session.in_transaction? && !ending_transaction retry_write(e, txn_num, &block) rescue Error::OperationFailure => e raise e if (session.in_transaction? && !ending_transaction) || !e.write_retryable? retry_write(e, txn_num, &block) end end |