123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Retryable::Backpressure Private

Relationships & Source Files
Defined in: lib/mongo/retryable/backpressure.rb

Overview

Constants and helpers for client backpressure (exponential backoff and jitter in retry loops).

Since:

  • 2.1.0

Constant Summary

Class Method Summary

Class Method Details

.backoff_delay(attempt, jitter: rand, err: nil) ⇒ 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). Defaults to a random value. Can be injected for deterministic testing.

Returns:

  • (Float)

    The backoff delay in seconds.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/retryable/backpressure.rb', line 26

def self.backoff_delay(attempt, jitter: rand, err: nil)
  jitter * [ MAX_BACKOFF, base_backoff(err) * (2**attempt) ].min
end

.base_backoff(err) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/retryable/backpressure.rb', line 30

def self.base_backoff(err)
  return BASE_BACKOFF if err.nil?
  return BASE_BACKOFF unless err.respond_to?(:result) && err.result.respond_to?(:base_backoff_ms)

  base_backoff_ms = err.result.base_backoff_ms

  if base_backoff_ms && base_backoff_ms > 0
    err.result.base_backoff_ms / 1000.0
  else
    BASE_BACKOFF
  end
end