Class: Reaper
- Inherits:
-
Object
- Object
- Reaper
- 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
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#idle_timeout ⇒ Object
readonly
Returns the value of attribute idle_timeout.
Instance Method Summary collapse
-
#initialize(pool, frequency, idle_timeout) ⇒ Reaper
constructor
A new instance of Reaper.
- #reap ⇒ Object
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
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
7 8 9 |
# File 'lib/redis_pool/reaper.rb', line 7 def frequency @frequency end |
#idle_timeout ⇒ Object (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
#reap ⇒ Object
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 |