debian-mirror-gitlab/lib/gitlab/database/connection_timer.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# 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
2020-10-24 23:57:45 +05:30
interval + rand(RANDOMIZATION_INTERVAL) if interval > 0
2020-04-08 14:13:33 +05:30
end
def current_clock_value
2023-04-23 21:23:45 +05:30
Process.clock_gettime(Process::CLOCK_MONOTONIC)
2020-04-08 14:13:33 +05:30
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