File: csot_timeout_holder.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (119 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

# Copyright (C) 2024 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  # This class stores operation timeout and provides corresponding helper methods.
  #
  # @api private
  class CsotTimeoutHolder
    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

    attr_reader :deadline, :timeout_sec, :operation_timeouts

    # @return [ true | false ] Whether CSOT is enabled for the operation
    def csot?
      !deadline.nil?
    end

    # @return [ true | false ] Returns false if CSOT is not enabled, or if
    #   CSOT is set to 0 (means unlimited), otherwise true.
    def timeout?
      ![ nil, 0 ].include?(@deadline)
    end

    # @return [ 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.
    def remaining_timeout_sec
      return nil unless timeout?

      deadline - Utils.monotonic_time
    end

    def remaining_timeout_sec!
      check_timeout!
      remaining_timeout_sec
    end

    # @return [ 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.
    def remaining_timeout_ms
      seconds = remaining_timeout_sec
      return nil if seconds.nil?

      (seconds * 1_000).to_i
    end

    def remaining_timeout_ms!
      check_timeout!
      remaining_timeout_ms
    end

    # @return [ true | false ] Whether the timeout for the operation expired.
    #   If no timeout set, this method returns false.
    def timeout_expired?
      if timeout?
        Utils.monotonic_time >= deadline
      else
        false
      end
    end

    # Check whether the operation timeout expired, and raises an appropriate
    # error if yes.
    #
    # @raise [ Error::TimeoutError ]
    def check_timeout!
      return unless timeout_expired?

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

    private

    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

    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

    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
  end
end