Class: Reaper

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_pool/reaper.rb

Overview

A reaper class that initializes a thread running in the background, that kills all connections in ‘pool` that has been idle for more than `idle_timeout`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, frequency, idle_timeout) ⇒ Reaper

Returns a new instance of Reaper.



9
10
11
12
13
14
# File 'lib/redis_pool/reaper.rb', line 9

def initialize(pool, frequency, idle_timeout)
  @frequency = frequency
  @idle_timeout = idle_timeout
  @pool = pool
  @lock = Mutex.new
end

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



7
8
9
# File 'lib/redis_pool/reaper.rb', line 7

def frequency
  @frequency
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



7
8
9
# File 'lib/redis_pool/reaper.rb', line 7

def idle_timeout
  @idle_timeout
end

Instance Method Details

#reapObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redis_pool/reaper.rb', line 16

def reap
  Thread.new do
    loop do
      @pool.available.queue.each do |conn|
        idle_since = conn.last[:last_used_at] - Time.now.utc

        next unless idle_since >= @idle_timeout

        @lock.synchronize do
          @pool.available.delete conn
          conn.first.disconnect!
        end
      end
      sleep @frequency
    end
  end
end