Method: Extlib::Pooling.scavenger

Defined in:
lib/extlib/pooling.rb

.scavengerObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/extlib/pooling.rb', line 32

def self.scavenger
  unless scavenger?
    @scavenger = Thread.new do
      running = true
      while running do
        # Sleep before we actually start doing anything.
        # Otherwise we might clean up something we just made
        sleep(scavenger_interval)

        lock.synchronize do
          pools.each do |pool|
            # This is a useful check, but non-essential, and right now it breaks lots of stuff.
            # if pool.expired?
            pool.lock.synchronize do
              if pool.expired?
                pool.dispose
              end
            end
            # end
          end

          # The pool is empty, we stop the scavenger
          # It wil be restarted if new resources are added again
          if pools.empty?
            running = false
          end
        end
      end # loop
    end
  end

  @scavenger.priority = -10
  @scavenger
end