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

Returns a new instance of 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.



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

def resources
  @resources
end

Instance Method Details

#acquireObject



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

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

#closeObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@resources.empty?
end

#release(resource) ⇒ Object

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



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

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



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

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