File: fork.rb

package info (click to toggle)
ruby-connection-pool 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 228 kB
  • sloc: ruby: 1,252; makefile: 6
file content (40 lines) | stat: -rw-r--r-- 1,000 bytes parent folder | download
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
class ConnectionPool
  if Process.respond_to?(:fork)
    INSTANCES = ObjectSpace::WeakMap.new
    private_constant :INSTANCES

    def self.after_fork
      INSTANCES.each_value do |pool|
        # We're in after_fork, so we know all other threads are dead.
        # All we need to do is ensure the main thread doesn't have a
        # checked out connection
        pool.checkin(force: true)
        pool.reload do |connection|
          # Unfortunately we don't know what method to call to close the connection,
          # so we try the most common one.
          connection.close if connection.respond_to?(:close)
        end
      end
      nil
    end

    module ForkTracker
      def _fork
        pid = super
        if pid == 0
          ConnectionPool.after_fork
        end
        pid
      end
    end
    Process.singleton_class.prepend(ForkTracker)
  else
    # JRuby, et al
    INSTANCES = nil
    private_constant :INSTANCES

    def self.after_fork
      # noop
    end
  end
end