fix(pool): returning closed resources to the pool (#154)

This commit is contained in:
Stephen von Takach 2021-09-07 08:02:43 +10:00 committed by GitHub
parent bf5ca75d1a
commit 6dc3f2dd6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -190,6 +190,23 @@ describe DB::Pool do
all[2].closed?.should be_false
end
it "should not return closed resources to the pool" do
pool = DB::Pool.new(max_pool_size: 1, max_idle_pool_size: 1) { Closable.new }
# pool size 1 should be reusing the one resource
resource1 = pool.checkout
pool.release resource1
resource2 = pool.checkout
resource1.should eq resource2
# it should not return a closed resource to the pool
resource2.close
pool.release resource2
resource2 = pool.checkout
resource1.should_not eq resource2
end
it "should create resource after max_pool was reached if idle forced some close up" do
all = [] of Closable
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }

View file

@ -141,7 +141,9 @@ module DB
idle_pushed = false
sync do
if can_increase_idle_pool
if resource.responds_to?(:closed?) && resource.closed?
@total.delete(resource)
elsif can_increase_idle_pool
@idle << resource
if resource.responds_to?(:after_release)
resource.after_release