Class: RedisPool

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

Constant Summary collapse

DEFAULT_REDIS_CONFIG =
{ host: 'localhost', port: 6379 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: 5, connection_timeout: 5, idle_timeout: 100, reaping_frequency: 300, redis_config: {}) ⇒ RedisPool

Returns a new instance of RedisPool.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



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

def available
  @available
end

#connection_timeoutObject (readonly)

Returns the value of attribute connection_timeout.



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

def connection_timeout
  @connection_timeout
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



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

def idle_timeout
  @idle_timeout
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



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

def max_size
  @max_size
end

#reaping_frequencyObject (readonly)

Returns the value of attribute reaping_frequency.



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

def reaping_frequency
  @reaping_frequency
end

Instance Method Details

#checkinObject



53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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

#statsObject



65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object Also known as: with_conn, with_connection



28
29
30
31
32
33
34
35
36
37
38
39
# 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