Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connection_timer.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9b893ffd0f7cd940f96edf3012e97d4ec9e937a (plain)
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
# frozen_string_literal: true

module Gitlab
  module Database
    class ConnectionTimer
      DEFAULT_INTERVAL = 3600
      RANDOMIZATION_INTERVAL = 600

      class << self
        def configure
          yield self
        end

        def starting_now
          # add a small amount of randomization to the interval, so reconnects don't all occur at once
          new(interval_with_randomization, current_clock_value)
        end

        attr_writer :interval

        def interval
          @interval ||= DEFAULT_INTERVAL
        end

        def interval_with_randomization
          interval + rand(RANDOMIZATION_INTERVAL) if interval > 0
        end

        def current_clock_value
          Concurrent.monotonic_time
        end
      end

      attr_reader :interval, :starting_clock_value

      def initialize(interval, starting_clock_value)
        @interval = interval
        @starting_clock_value = starting_clock_value
      end

      def expired?
        interval&.positive? && self.class.current_clock_value > (starting_clock_value + interval)
      end

      def reset!
        @starting_clock_value = self.class.current_clock_value
      end
    end
  end
end