Skip to content

Commit d3e1458

Browse files
p-mongop
authored andcommitted
Close clients in ClusterTools when all clients are closed and reconnect them automatically if perished (#1469)
1 parent 9b93996 commit d3e1458

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

spec/support/client_registry.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,33 @@ def initialize
5050
@lock = Mutex.new
5151
end
5252

53-
def global_client(name)
54-
if client = @global_clients[name]
53+
class << self
54+
def client_perished?(client)
5555
if !client.cluster.connected?
56-
reconnect = true
56+
true
5757
else
58-
reconnect = false
58+
perished = false
5959
client.cluster.servers_list.each do |server|
6060
thread = server.monitor.instance_variable_get('@thread')
6161
if thread.nil? || !thread.alive?
62-
reconnect = true
62+
perished = true
6363
end
6464
end
65+
perished
6566
end
66-
if reconnect
67+
end
68+
private :client_perished?
69+
70+
def reconnect_client_if_perished(client)
71+
if client_perished?(client)
6772
client.reconnect
6873
end
74+
end
75+
end
76+
77+
def global_client(name)
78+
if client = @global_clients[name]
79+
self.class.reconnect_client_if_perished(client)
6980
return client
7081
end
7182

@@ -204,6 +215,7 @@ def close_local_clients
204215
end
205216

206217
def close_all_clients
218+
ClusterTools.instance.close_clients
207219
close_local_clients
208220
@lock.synchronize do
209221
@global_clients.each do |name, client|

spec/support/cluster_tools.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,39 @@ def admin_client
313313
# Since we are triggering elections, we need to have a higher server
314314
# selection timeout applied. The default timeout for tests assumes a
315315
# stable deployment.
316-
@admin_client ||= ClientRegistry.instance.global_client('root_authorized').
317-
with(server_selection_timeout: 15).use(:admin)
316+
(
317+
@admin_client ||= ClientRegistry.instance.global_client('root_authorized').
318+
with(server_selection_timeout: 15).use(:admin)
319+
).tap do |client|
320+
ClientRegistry.reconnect_client_if_perished(client)
321+
end
318322
end
319323

320324
def direct_client(address, options = {})
321325
@direct_clients ||= {}
322326
cache_key = {address: address}.update(options)
323-
@direct_clients[cache_key] ||= ClientRegistry.instance.new_local_client(
324-
[address.to_s],
325-
SpecConfig.instance.test_options.merge(
326-
SpecConfig.instance.auth_options).merge(
327-
connect: :direct, server_selection_timeout: 10).merge(options))
327+
(
328+
@direct_clients[cache_key] ||= ClientRegistry.instance.new_local_client(
329+
[address.to_s],
330+
SpecConfig.instance.test_options.merge(
331+
SpecConfig.instance.auth_options).merge(
332+
connect: :direct, server_selection_timeout: 10).merge(options))
333+
).tap do |client|
334+
ClientRegistry.reconnect_client_if_perished(client)
335+
end
336+
end
337+
338+
def close_clients
339+
if @admin_client
340+
@admin_client.close
341+
@admin_client = nil
342+
end
343+
if @direct_clients
344+
@direct_clients.each do |cache_key, client|
345+
client.close
346+
end
347+
@direct_clients = nil
348+
end
328349
end
329350

330351
private

0 commit comments

Comments
 (0)