Skip to content

Commit 37b4eae

Browse files
committed
Scope connection pool to clusteR
1 parent b1319d1 commit 37b4eae

File tree

7 files changed

+70
-61
lines changed

7 files changed

+70
-61
lines changed

lib/mongo/address.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def ==(other)
6363
host == other.host && port == other.port
6464
end
6565

66+
def eql?(other)
67+
self == other
68+
end
69+
6670
# Calculate the hash value for the address.
6771
#
6872
# @example Calculate the hash value.

lib/mongo/cluster.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
106106
@options = options.freeze
107107
@topology = Topology.initial(seeds, options)
108108
@update_lock = Mutex.new
109+
@pool_lock = Mutex.new
109110

110111
subscribe_to(Event::STANDALONE_DISCOVERED, Event::StandaloneDiscovered.new(self))
111112
subscribe_to(Event::DESCRIPTION_CHANGED, Event::DescriptionChanged.new(self))
@@ -168,6 +169,22 @@ def max_read_retries
168169
options[:max_read_retries] || MAX_READ_RETRIES
169170
end
170171

172+
# Get the scoped connection pool for the server.
173+
#
174+
# @example Get the connection pool.
175+
# cluster.pool(server)
176+
#
177+
# @param [ Server ] server The server.
178+
#
179+
# @return [ Server::ConnectionPool ] The connection pool.
180+
#
181+
# @since 2.2.0
182+
def pool(server)
183+
@pool_lock.synchronize do
184+
pools[server.address] ||= Server::ConnectionPool.get(server)
185+
end
186+
end
187+
171188
# Get the interval, in seconds, in which a mongos read operation is
172189
# retried.
173190
#
@@ -339,6 +356,10 @@ def addition_allowed?(address)
339356
!@topology.single? || direct_connection?(address)
340357
end
341358

359+
def pools
360+
@pools ||= {}
361+
end
362+
342363
def servers_list
343364
@update_lock.synchronize do
344365
@servers.reduce([]) do |servers, server|

lib/mongo/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def inspect
182182
#
183183
# @since 2.0.0
184184
def pool
185-
@pool ||= ConnectionPool.get(self)
185+
@pool ||= cluster.pool(self)
186186
end
187187

188188
# Determine if the provided tags are a subset of the server's tags.

lib/mongo/server/connection_pool.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ class Server
2323
class ConnectionPool
2424
include Loggable
2525

26-
# Used for synchronization of pools access.
27-
MUTEX = Mutex.new
28-
2926
# @return [ Hash ] options The pool options.
3027
attr_reader :options
3128

@@ -131,22 +128,10 @@ class << self
131128
#
132129
# @since 2.0.0
133130
def get(server)
134-
MUTEX.synchronize do
135-
pools[server.address] ||= create_pool(server)
136-
end
137-
end
138-
139-
private
140-
141-
def create_pool(server)
142131
ConnectionPool.new(server.options) do
143132
Connection.new(server, server.options)
144133
end
145134
end
146-
147-
def pools
148-
@pools ||= {}
149-
end
150135
end
151136
end
152137
end

spec/mongo/server/connection_pool_spec.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
Mongo::Event::Listeners.new
1919
end
2020

21+
let(:cluster) do
22+
double('cluster')
23+
end
24+
2125
describe '#checkin' do
2226

2327
let(:server) do
24-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
28+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
2529
end
2630

2731
let!(:pool) do
2832
described_class.get(server)
2933
end
3034

3135
after do
36+
expect(cluster).to receive(:pool).with(server).and_return(pool)
3237
server.disconnect!
3338
end
3439

@@ -55,7 +60,7 @@
5560
describe '#checkout' do
5661

5762
let(:server) do
58-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
63+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
5964
end
6065

6166
let!(:pool) do
@@ -103,53 +108,52 @@
103108
describe '#disconnect!' do
104109

105110
let(:server) do
106-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
111+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
107112
end
108113

109114
let!(:pool) do
110115
described_class.get(server)
111116
end
112117

113-
after do
114-
server.disconnect!
115-
end
116-
117118
it 'disconnects the queue' do
118-
expect(pool.send(:queue)).to receive(:disconnect!).twice.and_call_original
119-
pool.disconnect!
119+
expect(cluster).to receive(:pool).with(server).and_return(pool)
120+
expect(pool.send(:queue)).to receive(:disconnect!).once.and_call_original
121+
server.disconnect!
120122
end
121123
end
122124

123125
describe '.get' do
124126

125127
let(:server) do
126-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
128+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
127129
end
128130

129131
let!(:pool) do
130132
described_class.get(server)
131133
end
132134

133135
after do
136+
expect(cluster).to receive(:pool).with(server).and_return(pool)
134137
server.disconnect!
135138
end
136139

137140
it 'returns the pool for the server' do
138-
expect(pool).to eql(described_class.get(server))
141+
expect(pool).to_not be_nil
139142
end
140143
end
141144

142145
describe '#inspect' do
143146

144147
let(:server) do
145-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
148+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
146149
end
147150

148151
let!(:pool) do
149152
described_class.get(server)
150153
end
151154

152155
after do
156+
expect(cluster).to receive(:pool).with(server).and_return(pool)
153157
server.disconnect!
154158
end
155159

@@ -165,7 +169,7 @@
165169
describe '#with_connection' do
166170

167171
let(:server) do
168-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
172+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
169173
end
170174

171175
let!(:pool) do

spec/mongo/server/connection_spec.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,34 @@
1414
Mongo::Event::Listeners.new
1515
end
1616

17+
let(:cluster) do
18+
double('cluster')
19+
end
20+
1721
let(:server) do
18-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, TEST_OPTIONS)
22+
Mongo::Server.new(address, cluster, monitoring, listeners, TEST_OPTIONS)
23+
end
24+
25+
let(:pool) do
26+
double('pool')
1927
end
2028

2129
after do
30+
expect(cluster).to receive(:pool).with(server).and_return(pool)
31+
expect(pool).to receive(:disconnect!).and_return(true)
2232
server.disconnect!
2333
end
2434

2535
describe '#connectable?' do
2636

27-
# context 'when the connection is connectable' do
28-
29-
# let(:connection) do
30-
# described_class.new(server)
31-
# end
32-
33-
# it 'returns true' do
34-
# expect(connection).to be_connectable
35-
# end
36-
# end
37-
3837
context 'when the connection is not connectable' do
3938

4039
let(:bad_address) do
4140
Mongo::Address.new('127.0.0.1:666')
4241
end
4342

4443
let(:bad_server) do
45-
Mongo::Server.new(bad_address, double('cluster'), monitoring, listeners, TEST_OPTIONS)
44+
Mongo::Server.new(bad_address, cluster, monitoring, listeners, TEST_OPTIONS)
4645
end
4746

4847
let(:connection) do

spec/mongo/server_spec.rb

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
Mongo::Address.new('127.0.0.1:27017')
1919
end
2020

21+
let(:pool) do
22+
Mongo::Server::ConnectionPool.get(server)
23+
end
24+
2125
describe '#==' do
2226

2327
let(:server) do
2428
described_class.new(address, cluster, monitoring, listeners, TEST_OPTIONS)
2529
end
2630

2731
after do
32+
expect(cluster).to receive(:pool).with(server).and_return(pool)
2833
server.disconnect!
2934
end
3035

@@ -81,6 +86,10 @@
8186
server.disconnect!
8287
end
8388

89+
before do
90+
expect(cluster).to receive(:pool).with(server).and_return(pool)
91+
end
92+
8493
it 'returns true' do
8594
expect(server).to be_connectable
8695
end
@@ -97,6 +106,7 @@
97106
end
98107

99108
before do
109+
expect(cluster).to receive(:pool).with(server).and_return(pool)
100110
server.disconnect!
101111
end
102112

@@ -117,6 +127,7 @@
117127
end
118128

119129
after do
130+
expect(cluster).to receive(:pool).with(server).and_return(pool)
120131
server.disconnect!
121132
end
122133

@@ -133,6 +144,7 @@
133144

134145
it 'stops the monitor instance' do
135146
expect(server.instance_variable_get(:@monitor)).to receive(:stop!).and_return(true)
147+
expect(cluster).to receive(:pool).with(server).and_return(pool)
136148
server.disconnect!
137149
end
138150
end
@@ -150,6 +162,7 @@
150162
end
151163

152164
after do
165+
expect(cluster).to receive(:pool).with(server).and_return(pool)
153166
server.disconnect!
154167
end
155168

@@ -166,32 +179,14 @@
166179
end
167180
end
168181

169-
describe '#pool' do
170-
171-
let(:server) do
172-
described_class.new(address, cluster, monitoring, listeners, TEST_OPTIONS)
173-
end
174-
175-
let(:pool) do
176-
server.pool
177-
end
178-
179-
after do
180-
server.disconnect!
181-
end
182-
183-
it 'returns the connection pool for the server' do
184-
expect(pool).to be_a(Mongo::Server::ConnectionPool)
185-
end
186-
end
187-
188182
describe '#scan!' do
189183

190184
let(:server) do
191185
described_class.new(address, cluster, monitoring, listeners, TEST_OPTIONS)
192186
end
193187

194188
after do
189+
expect(cluster).to receive(:pool).with(server).and_return(pool)
195190
server.disconnect!
196191
end
197192

@@ -211,6 +206,7 @@
211206
end
212207

213208
after do
209+
expect(cluster).to receive(:pool).with(server).and_return(pool)
214210
server.disconnect!
215211
end
216212

0 commit comments

Comments
 (0)