Class: Xunch::ThreadedRedisPool

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/shard/ThreadedRedisPool.rb,
lib/xunch/connection/threaded_redis_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ThreadedRedisPool

Returns a new instance of ThreadedRedisPool.



3
4
5
6
7
8
9
10
# File 'lib/xunch/shard/ThreadedRedisPool.rb', line 3

def initialize(size, timeout)
    @reserved  = {}   # map of in-progress connections
    @pending   = 0   # pending reservations (FIFO)
    @available = Array.new(@options[:size]) { redis = Redis.new(@options) }
    @pool_timeout = @options[:pool_timeout]
    @mutex = Mutex.new
    @resource = ConditionVariable.new
end

Instance Method Details

#destroyObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xunch/connection/threaded_redis_pool.rb', line 23

def destroy
  if !@closed
    @mutex.synchronize do
      if !@closed
        @closed = true
        @available.each {|redis| redis.quit if redis and redis.connected?}
        @reserved.each {|redis| redis.quit if redis and redis.connected?}
      end
    end
  end
end

#pool_statusObject



21
22
23
24
25
26
27
# File 'lib/xunch/shard/ThreadedRedisPool.rb', line 21

def pool_status
  {
    available: @available.size,
    reserved: @reserved.size,
    pending: @pending
  }
end

#withObject



12
13
14
15
16
17
18
19
# File 'lib/xunch/shard/ThreadedRedisPool.rb', line 12

def with
  redis = checkout_redis
  begin
    yield redis
  ensure
    checkin_redis(redis)
  end
end