Class: Mongo::Retryable::RetryPolicy Private
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/mongo/retryable/retry_policy.rb |
Overview
Encapsulates the retry policy for client backpressure, combining exponential backoff with jitter and an optional token bucket for adaptive retries.
One instance is created per ::Mongo::Client and shared across all operations on that client.
Class Method Summary
-
.new(adaptive_retries: false) ⇒ RetryPolicy
constructor
Internal use only
Create a new retry policy.
Instance Attribute Summary
- #token_bucket ⇒ TokenBucket | nil readonly Internal use only
Instance Method Summary
-
#backoff_delay(attempt, jitter: rand) ⇒ Float
Internal use only
Calculate the backoff delay for a given retry attempt.
-
#record_non_overload_retry_failure
Internal use only
Record a non-overload failure during a retry attempt by depositing 1 token.
-
#record_success(is_retry:)
Internal use only
Record a successful operation by depositing tokens into the bucket.
-
#should_retry_overload?(attempt, delay, context: nil) ⇒ true | false
Internal use only
Determine whether an overload retry should be attempted.
-
#exceeds_deadline?(delay, context) ⇒ Boolean
private
Internal use only
Check whether the backoff delay would exceed the CSOT deadline.
Instance Attribute Details
#token_bucket ⇒ TokenBucket | nil (readonly)
# File 'lib/mongo/retryable/retry_policy.rb', line 24
attr_reader :token_bucket
Instance Method Details
#backoff_delay(attempt, jitter: rand) ⇒ Float
Calculate the backoff delay for a given retry attempt.
# File 'lib/mongo/retryable/retry_policy.rb', line 32
def backoff_delay(attempt, jitter: rand) Backpressure.backoff_delay(attempt, jitter: jitter) end
#exceeds_deadline?(delay, context) ⇒ Boolean (private)
Check whether the backoff delay would exceed the CSOT deadline.
# File 'lib/mongo/retryable/retry_policy.rb', line 78
def exceeds_deadline?(delay, context) return false unless context&.csot? deadline = context&.deadline deadline&.nonzero? && Utils.monotonic_time + delay > deadline end
#record_non_overload_retry_failure
Record a non-overload failure during a retry attempt by depositing 1 token.
# File 'lib/mongo/retryable/retry_policy.rb', line 71
def record_non_overload_retry_failure @token_bucket&.deposit(1) end
#record_success(is_retry:)
Record a successful operation by depositing tokens into the bucket.
# File 'lib/mongo/retryable/retry_policy.rb', line 61
def record_success(is_retry:) return unless @token_bucket tokens = Backpressure::RETRY_TOKEN_RETURN_RATE tokens += 1 if is_retry @token_bucket.deposit(tokens) end
#should_retry_overload?(attempt, delay, context: nil) ⇒ true | false
Determine whether an overload retry should be attempted.
Checks that the attempt number does not exceed MAX_RETRIES, that the backoff delay would not exceed the CSOT deadline (if set), and that a token is available (if adaptive retries are enabled).
# File 'lib/mongo/retryable/retry_policy.rb', line 48
def should_retry_overload?(attempt, delay, context: nil) return false if attempt > Backpressure::MAX_RETRIES return false if exceeds_deadline?(delay, context) return false if @token_bucket && !@token_bucket.consume(1) true end