Class: Async::Redis::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/async/redis/pool.rb

Overview

It might make sense to add support for pipelining redis.io/topics/pipelining We should be able to wrap the protocol so that write_request and read_response happen in lockstep. The only problem would be blocking operations. It might also be confusing if order of operations affects commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit = nil, &block) ⇒ Pool



29
30
31
32
33
34
35
36
37
# File 'lib/async/redis/pool.rb', line 29

def initialize(limit = nil, &block)
  @resources = []
  @available = Async::Notification.new
  
  @limit = limit
  @active = 0
  
  @constructor = block
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



39
40
41
# File 'lib/async/redis/pool.rb', line 39

def resources
  @resources
end

Instance Method Details

#acquireObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/redis/pool.rb', line 45

def acquire
  resource = wait_for_resource
  
  return resource unless block_given?
  
  begin
    yield resource
  ensure
    release(resource)
  end
end

#closeObject



67
68
69
70
71
72
# File 'lib/async/redis/pool.rb', line 67

def close
  @resources.each(&:close)
  @resources.clear
  
  @active = 0
end

#empty?Boolean



41
42
43
# File 'lib/async/redis/pool.rb', line 41

def empty?
  @resources.empty?
end

#release(resource) ⇒ Object

Make the resource resources and let waiting tasks know that there is something resources.



58
59
60
61
62
63
64
65
# File 'lib/async/redis/pool.rb', line 58

def release(resource)
  # A resource that is not good should also not be reusable.
  unless resource.closed?
    reuse(resource)
  else
    retire(resource)
  end
end

#to_sObject



74
75
76
# File 'lib/async/redis/pool.rb', line 74

def to_s
  "\#<#{self.class} resources=#{resources.count} limit=#{@limit}>"
end