Class: Mongo::CsotTimeoutHolder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/csot_timeout_holder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class stores operation timeout and provides corresponding helper methods.

API:

  • private

Direct Known Subclasses

Operation::Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session: nil, operation_timeouts: {}) ⇒ CsotTimeoutHolder

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.

Returns a new instance of CsotTimeoutHolder.

API:

  • private



22
23
24
25
26
# File 'lib/mongo/csot_timeout_holder.rb', line 22

def initialize(session: nil, operation_timeouts: {})
  @deadline = calculate_deadline(operation_timeouts, session)
  @operation_timeouts = operation_timeouts
  @timeout_sec = (@deadline - Utils.monotonic_time if @deadline)
end

Instance Attribute Details

#deadlineObject (readonly)

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.

API:

  • private



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

def deadline
  @deadline
end

#operation_timeoutsObject (readonly)

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.

API:

  • private



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

def operation_timeouts
  @operation_timeouts
end

#timeout_secObject (readonly)

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.

API:

  • private



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

def timeout_sec
  @timeout_sec
end

Instance Method Details

#check_timeout!Object

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.

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

Raises:

API:

  • private



84
85
86
87
88
# 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

#csot?true | false

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.

Returns Whether CSOT is enabled for the operation.

Returns:

  • Whether CSOT is enabled for the operation

API:

  • private



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

def csot?
  !deadline.nil?
end

#remaining_timeout_msInteger | nil

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.

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.

Returns:

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

API:

  • private



58
59
60
61
62
63
# 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!Object

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.

API:

  • private



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

def remaining_timeout_ms!
  check_timeout!
  remaining_timeout_ms
end

#remaining_timeout_secFloat | nil

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.

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.

Returns:

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

API:

  • private



44
45
46
47
48
# 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!Object

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.

API:

  • private



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

def remaining_timeout_sec!
  check_timeout!
  remaining_timeout_sec
end

#timeout?true | false

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.

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

Returns:

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

API:

  • private



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

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

#timeout_expired?true | false

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.

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

Returns:

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

API:

  • private



72
73
74
75
76
77
78
# File 'lib/mongo/csot_timeout_holder.rb', line 72

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