From 5822e820110675af31baf6fb01935d5e484d7369 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 29 Oct 2013 12:01:45 -0400 Subject: [PATCH] ConnectionPool checkin should put connection back into the pool [fixes #223] --- lib/moped/connection/pool.rb | 27 ++++++++------------------- spec/moped/cluster_spec.rb | 2 +- spec/moped/connection/pool_spec.rb | 17 +++++++++++------ spec/moped/node_spec.rb | 4 ++-- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/moped/connection/pool.rb b/lib/moped/connection/pool.rb index 1c587fe..c02566e 100644 --- a/lib/moped/connection/pool.rb +++ b/lib/moped/connection/pool.rb @@ -36,16 +36,12 @@ class Pool # @since 2.0.0 def checkout mutex.synchronize do - connection = pinned[thread_id] - if connection - unless connection.expired? - raise Errors::ConnectionInUse, "The connection on thread: #{thread_id} is in use." - else - lease(connection) - end + if connection = pinned[thread_id] + raise Errors::ConnectionInUse, "The connection on thread: #{thread_id} is in use." else connection = pinned[thread_id] = next_connection - lease(connection) + connection.lease + connection end end end @@ -61,7 +57,10 @@ def checkout # @since 2.0.0 def checkin(connection) mutex.synchronize do - expire(connection) + if connection == pinned[thread_id] + connection.expire + unpinned.push(pinned.delete(thread_id)) + end end end @@ -167,16 +166,6 @@ def with_connection attr_reader :mutex, :resource, :pinned, :unpinned - def expire(connection) - connection.expire - pinned[thread_id] = connection - end - - def lease(connection) - connection.lease - connection - end - def next_connection reap if saturated? unpinned.shift diff --git a/spec/moped/cluster_spec.rb b/spec/moped/cluster_spec.rb index e58f2c6..068ac42 100644 --- a/spec/moped/cluster_spec.rb +++ b/spec/moped/cluster_spec.rb @@ -270,7 +270,7 @@ context "with down interval" do let(:cluster) do - Moped::Cluster.new(seeds, { down_interval: 5 }) + Moped::Cluster.new(seeds, { down_interval: 5, pool_size: 1 }) end context "and all secondaries are down" do diff --git a/spec/moped/connection/pool_spec.rb b/spec/moped/connection/pool_spec.rb index 1385a7a..b07a33d 100644 --- a/spec/moped/connection/pool_spec.rb +++ b/spec/moped/connection/pool_spec.rb @@ -28,12 +28,12 @@ pool.checkin(connection) end - it "keeps the connection pinned" do - expect(pinned[Thread.current.object_id]).to equal(connection) + it "releases the connection" do + expect(pinned[Thread.current.object_id]).to be_nil end - it "does not modify the unpinned connections" do - expect(unpinned.size).to eq(1) + it "sets to zero the pinned connections" do + expect(pinned.size).to eq(0) end it "expires the connection" do @@ -103,8 +103,13 @@ pool.checkin(existing) end - it "returns the connection" do - expect(pool.checkout).to equal(existing) + let(:connection) do + pool.checkout + end + + it "returns a new connection" do + expect(connection).to_not be_nil + expect(connection).to_not equal(existing) end end diff --git a/spec/moped/node_spec.rb b/spec/moped/node_spec.rb index a3cafc4..8e535c8 100644 --- a/spec/moped/node_spec.rb +++ b/spec/moped/node_spec.rb @@ -7,7 +7,7 @@ end let(:node) do - Moped::Node.new(replica_set_node.address) + Moped::Node.new(replica_set_node.address, pool_size: 1) end describe "#auto_discovering?" do @@ -105,7 +105,7 @@ context "when the node is auto discovering" do let(:node) do - described_class.new("127.0.0.1:27017") + described_class.new("127.0.0.1:27017", pool_size: 1) end before do