Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge 7e9b992 into 95f6ac9
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurnn committed Nov 3, 2013
2 parents 95f6ac9 + 7e9b992 commit 5153026
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
27 changes: 8 additions & 19 deletions lib/moped/connection/pool.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/moped/cluster_spec.rb
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions spec/moped/connection/pool_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/moped/node_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5153026

Please sign in to comment.