Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allows the pool to be populated with resources created outside the pool. #3

Merged
merged 2 commits into from

2 participants

@righi

Adds pool.fill(), a method that allows existing, already-opened resources to be added to the pool.

I need this functionality for a project I'm working on, and I figured it might be useful for others as well.

@seancribbs

Thank you for the contribution, looks nice!

@seancribbs seancribbs merged commit 00ec25b into basho:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 0 deletions.
  1. +3 −0  README.md
  2. +10 −0 lib/innertube.rb
  3. +14 −0 spec/innertube_spec.rb
View
3  README.md
@@ -16,6 +16,9 @@ licensed under the Apache 2.0 License.
pool = Innertube::Pool.new(proc { Connection.new },
proc {|c| c.disconnect })
+# Optionally, fill the pool with existing resources
+pool.fill([conn1, conn2, conn3])
+
# Grab a connection from the pool, returns the same value
# as the block
pool.take {|conn| conn.ping } # => true
View
10 lib/innertube.rb
@@ -61,6 +61,16 @@ def initialize(open, close)
@pool = Set.new
end
+ # Populate the pool with existing, open resources.
+ # @param [Array] An array of resources.
+ def fill(resources)
+ @lock.synchronize do
+ resources.each do |r|
+ @pool << Element.new(r)
+ end
+ end
+ end
+
# On each element of the pool, calls close(element) and removes it.
# @private
def clear
View
14 spec/innertube_spec.rb
@@ -51,6 +51,20 @@ def wait_all(threads)
pool_members.map { |e| e.object.first }.sort.should == [1,2,3,4]
end
+ it 'should be fillable with existing resources' do
+ pool.fill(["Apple", "Banana", "Kiwi"])
+ pool_members.size.should == 3
+
+ pool.take do |x|
+ x.should eq('Apple')
+ pool.take do |y|
+ y.should eq('Banana')
+ pool.take do |z|
+ z.should eq('Kiwi')
+ end
+ end
+ end
+ end
it 'should unlock when exceptions are raised' do
begin
Something went wrong with that request. Please try again.