summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin.rb
blob: 9f664fa21376fa2a5355d02daa3dc9cc820a247c (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
# frozen_string_literal: true

module Gitlab
  module Database
    module PostgresqlAdapter
      module ForceDisconnectableMixin
        extend ActiveSupport::Concern

        prepended do
          set_callback :checkin, :after, :force_disconnect_if_old!
        end

        def force_disconnect_if_old!
          if force_disconnect_timer.expired?
            disconnect!
            reset_force_disconnect_timer!
          end
        end

        def reset_force_disconnect_timer!
          force_disconnect_timer.reset!
        end

        def force_disconnect_timer
          @force_disconnect_timer ||= ConnectionTimer.starting_now
        end
      end
    end
  end
end