123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Retryable::RetryPolicy Private

Do not use. This class is for internal use only.
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.

Since:

  • 2.1.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#token_bucketTokenBucket | nil (readonly)

Returns:

  • (TokenBucket | nil)

    The token bucket, if adaptive retries are enabled.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Parameters:

  • attempt (Integer)

    The retry attempt number (1-indexed).

  • jitter (Float)

    A random float in [0.0, 1.0).

Returns:

  • (Float)

    The backoff delay in seconds.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Parameters:

  • is_retry (true | false)

    Whether the success came from a retried attempt.

Since:

  • 2.1.0

[ GitHub ]

  
# 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).

Parameters:

  • attempt (Integer)

    The retry attempt number (1-indexed).

  • delay (Float)

    The backoff delay in seconds.

  • context (Mongo::CsotTimeoutHolder | nil)

    The operation context (for CSOT deadline checking).

Returns:

  • (true | false)

    Whether the retry should proceed.

Since:

  • 2.1.0

[ GitHub ]

  
# 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