class RedisPool
Constants
- DEFAULT_REDIS_CONFIG
Attributes
available[R]
connection_timeout[R]
idle_timeout[R]
max_size[R]
reaping_frequency[R]
Public Class Methods
new(max_size: 5, connection_timeout: 5, idle_timeout: 100, reaping_frequency: 300, redis_config: {})
click to toggle source
# File lib/redis_pool.rb, line 11 def initialize(max_size: 5, connection_timeout: 5, idle_timeout: 100, reaping_frequency: 300, redis_config: {}) @redis_config = DEFAULT_REDIS_CONFIG.merge(redis_config) @max_size = max_size @connection_timeout = connection_timeout @idle_timeout = idle_timeout @reaping_frequency = reaping_frequency @available = ConnectionQueue.new(@max_size, &redis_creation_block) @reaper = Reaper.new(self, @reaping_frequency, @idle_timeout) @key = :"pool-#{@available.object_id}" @key_count = :"pool-#{@available.object_id}-count" @reaper.reap end
Public Instance Methods
checkin()
click to toggle source
# File lib/redis_pool.rb, line 53 def checkin raise 'no connections are checked out' unless current_thread[@key] if current_thread[@key_count] == 1 @available.add current_thread[@key] current_thread[@key] = nil current_thread[@key_count] = nil else current_thread[@key_count] -= 1 end end
checkout(timeout = nil)
click to toggle source
# File lib/redis_pool.rb, line 43 def checkout(timeout = nil) if current_thread[@key] current_thread[@key_count] += 1 current_thread[@key] else current_thread[@key_count] = 1 current_thread[@key] = @available.poll(timeout || @connection_timeout) end end
stats()
click to toggle source
# File lib/redis_pool.rb, line 65 def stats conn_stats = @available.queue.map do |conn| conn.last end pool_stats = { available_to_create: @available.available_to_create, total_available: @available.total_available, connections_stats: conn_stats } end
with(timeout = nil) { |first| ... }
click to toggle source
# File lib/redis_pool.rb, line 28 def with(timeout = nil) Thread.handle_interrupt(Exception => :never) do conn = checkout(timeout) begin Thread.handle_interrupt(Exception => :immediate) do yield conn.first end ensure checkin end end end
Also aliased as: with_conn, with_connection
Private Instance Methods
current_thread()
click to toggle source
# File lib/redis_pool.rb, line 78 def current_thread Thread.current end
redis_creation_block()
click to toggle source
# File lib/redis_pool.rb, line 82 def redis_creation_block -> { Redis.new(@redis_config) } end