Class: Xunch::FiberRedisPool

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/connection/fiber_redis_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FiberRedisPool

Returns a new instance of FiberRedisPool.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/xunch/connection/fiber_redis_pool.rb', line 4

def initialize(options)
  @reserved  = {}   # map of in-progress connections
  @available = []   # pool of free connections
  @pending   = []   # pending reservations (FIFO)
  @pool_timeout = options[:pool_timeout]

  options[:size].times do
    @available.push(redis = Redis.new(options))
  end

  @closed = false
end

Instance Method Details

#destroyObject



29
30
31
32
33
34
35
# File 'lib/xunch/connection/fiber_redis_pool.rb', line 29

def destroy
  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

#pool_statusHash

Returns current pool utilization.

Returns:

  • (Hash)

    Current utilization.



40
41
42
43
44
45
46
47
# File 'lib/xunch/connection/fiber_redis_pool.rb', line 40

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

#withObject



19
20
21
22
23
24
25
26
27
# File 'lib/xunch/connection/fiber_redis_pool.rb', line 19

def with
  f = Fiber.current
  begin
    conn = checkout_redis(f)
    yield conn
  ensure
    checkin_redis(f) 
  end
end