123456789_123456789_123456789_123456789_123456789_

Class: Mongo::CsotTimeoutHolder Private

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Mongo::Operation::Context
Inherits: Object
Defined in: lib/mongo/csot_timeout_holder.rb

Overview

This class stores operation timeout and provides corresponding helper methods.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#csot?true | false (readonly)

Returns:

  • (true | false)

    Whether CSOT is enabled for the operation

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 31

def csot?
  !deadline.nil?
end

#deadline (readonly)

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 28

attr_reader :deadline, :timeout_sec, :operation_timeouts

#operation_timeouts (readonly)

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 28

attr_reader :deadline, :timeout_sec, :operation_timeouts

#timeout?true | false (readonly)

Returns:

  • (true | false)

    Returns false if CSOT is not enabled, or if CSOT is set to 0 (means unlimited), otherwise true.

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 37

def timeout?
  ![ nil, 0 ].include?(@deadline)
end

#timeout_expired?true | false (readonly)

Returns:

  • (true | false)

    Whether the timeout for the operation expired. If no timeout set, this method returns false.

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 72

def timeout_expired?
  if timeout?
    Utils.monotonic_time >= deadline
  else
    false
  end
end

#timeout_sec (readonly)

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 28

attr_reader :deadline, :timeout_sec, :operation_timeouts

Instance Method Details

#calculate_deadline(opts = {}, session = nil) (private)

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 92

def calculate_deadline(opts = {}, session = nil)
  check_no_override_inside_transaction!(opts, session)
  return session&.with_transaction_deadline if session&.with_transaction_deadline

  if (operation_timeout_ms = opts[:operation_timeout_ms])
    calculate_deadline_from_timeout_ms(operation_timeout_ms)
  elsif (inherited_timeout_ms = opts[:inherited_timeout_ms])
    calculate_deadline_from_timeout_ms(inherited_timeout_ms)
  end
end

#calculate_deadline_from_timeout_ms(operation_timeout_ms) (private)

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 109

def calculate_deadline_from_timeout_ms(operation_timeout_ms)
  if operation_timeout_ms.positive?
    Utils.monotonic_time + (operation_timeout_ms / 1_000.0)
  elsif operation_timeout_ms.zero?
    0
  elsif operation_timeout_ms.negative?
    raise ArgumentError, "timeout_ms must be a non-negative integer but #{operation_timeout_ms} given"
  end
end

#check_no_override_inside_transaction!(opts, session) (private)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 103

def check_no_override_inside_transaction!(opts, session)
  return unless opts[:operation_timeout_ms] && session&.with_transaction_deadline

  raise ArgumentError, 'Cannot override timeout_ms inside with_transaction block'
end

#check_timeout!

Check whether the operation timeout expired, and raises an appropriate error if yes.

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 84

def check_timeout!
  return unless timeout_expired?

  raise Error::TimeoutError, "Operation took more than #{timeout_sec} seconds"
end

#remaining_timeout_msInteger | nil

Returns:

  • (Integer | nil)

    Returns the remaining milliseconds of the timeout set for the operation; if no timeout is set, or the timeout is 0 (means unlimited) returns nil.

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 58

def remaining_timeout_ms
  seconds = remaining_timeout_sec
  return nil if seconds.nil?

  (seconds * 1_000).to_i
end

#remaining_timeout_ms!

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 65

def remaining_timeout_ms!
  check_timeout!
  remaining_timeout_ms
end

#remaining_timeout_secFloat | nil

Returns:

  • (Float | nil)

    Returns the remaining seconds of the timeout set for the operation; if no timeout is set, or the timeout is 0 (means unlimited) returns nil.

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 44

def remaining_timeout_sec
  return nil unless timeout?

  deadline - Utils.monotonic_time
end

#remaining_timeout_sec!

[ GitHub ]

  
# File 'lib/mongo/csot_timeout_holder.rb', line 50

def remaining_timeout_sec!
  check_timeout!
  remaining_timeout_sec
end