From 6dc3f2dd6f492eb2d9354084cfe5ea00155b07e8 Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Tue, 7 Sep 2021 08:02:43 +1000 Subject: [PATCH] fix(pool): returning closed resources to the pool (#154) --- spec/pool_spec.cr | 17 +++++++++++++++++ src/db/pool.cr | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spec/pool_spec.cr b/spec/pool_spec.cr index 6a1612c..9aed907 100644 --- a/spec/pool_spec.cr +++ b/spec/pool_spec.cr @@ -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 } } diff --git a/src/db/pool.cr b/src/db/pool.cr index e483dd8..5964de8 100644 --- a/src/db/pool.cr +++ b/src/db/pool.cr @@ -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