Class: Gitlab::Database::ConnectionTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/connection_timer.rb

Constant Summary collapse

DEFAULT_INTERVAL =
3600
RANDOMIZATION_INTERVAL =
600

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, starting_clock_value) ⇒ ConnectionTimer

Returns a new instance of ConnectionTimer.



36
37
38
39
# File 'lib/gitlab/database/connection_timer.rb', line 36

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

Class Attribute Details

.intervalObject



21
22
23
# File 'lib/gitlab/database/connection_timer.rb', line 21

def interval
  @interval ||= DEFAULT_INTERVAL
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



34
35
36
# File 'lib/gitlab/database/connection_timer.rb', line 34

def interval
  @interval
end

#starting_clock_valueObject (readonly)

Returns the value of attribute starting_clock_value.



34
35
36
# File 'lib/gitlab/database/connection_timer.rb', line 34

def starting_clock_value
  @starting_clock_value
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



10
11
12
# File 'lib/gitlab/database/connection_timer.rb', line 10

def configure
  yield self
end

.current_clock_valueObject



29
30
31
# File 'lib/gitlab/database/connection_timer.rb', line 29

def current_clock_value
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.interval_with_randomizationObject



25
26
27
# File 'lib/gitlab/database/connection_timer.rb', line 25

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

.starting_nowObject



14
15
16
17
# File 'lib/gitlab/database/connection_timer.rb', line 14

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

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gitlab/database/connection_timer.rb', line 41

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

#reset!Object



45
46
47
# File 'lib/gitlab/database/connection_timer.rb', line 45

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