Module: PgFailover::SequelAdapter::ConnectionValidator

Defined in:
lib/pg_failover/sequel_adapter.rb

Instance Method Summary collapse

Instance Method Details

#acquire(*adapter) ⇒ Object



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
# File 'lib/pg_failover/sequel_adapter.rb', line 22

def acquire(*adapter)
  connection = super

  PgFailover.connection_validator.call(
    throttle_by: connection,
    in_recovery: proc {
      result = connection.execute('select pg_is_in_recovery()', &:to_a).first
      %w[1 t true].include?(result['pg_is_in_recovery'].to_s)
    },
    reconnect: proc {
      # This disconnect is copy pasted from the
      # https://github.com/jeremyevans/sequel/blob/5.15.0/lib/sequel/extensions/connection_validator.rb#L103-L109
      #
      if pool_type == :sharded_threaded
        sync { allocated(adapter.last).delete(Thread.current) }
      else
        sync { @allocated.delete(Thread.current) }
      end

      disconnect_connection(connection)

      connection = super
    }
  )

  connection
end