From 24085c68274294722745bcd4dce2b391f1e63aaf Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Fri, 8 Apr 2022 18:19:19 +0200 Subject: [PATCH 1/2] MONGOID-5255 Reuse pre-configured clients --- lib/mongoid/persistence_context.rb | 17 ++++++++++++++- spec/mongoid/clients_spec.rb | 26 +++++++++++++++++++++++ spec/mongoid/persistence_context_spec.rb | 27 +++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/lib/mongoid/persistence_context.rb b/lib/mongoid/persistence_context.rb index 631005787d..6dd9ced6d2 100644 --- a/lib/mongoid/persistence_context.rb +++ b/lib/mongoid/persistence_context.rb @@ -122,6 +122,21 @@ def ==(other) options == other.options end + # Whether the client of the context can be reused later, and therefore should + # not be closed. + # + # If the persistence context is requested with :client option only, it means + # that the context should use a client configured in mongoid.yml. + # Such clients should not be closed when the context is cleared since they + # will be reused later. + # + # @return [ true | false ] True if client can be reused, otherwise false. + # + # @api private + def reusable_client? + @options.keys == [:client] + end + private def set_options!(opts) @@ -211,7 +226,7 @@ def get(object) def clear(object, cluster = nil, original_context = nil) if context = get(object) unless cluster.nil? || context.cluster.equal?(cluster) - context.client.close + context.client.close unless context.reusable_client? end end ensure diff --git a/spec/mongoid/clients_spec.rb b/spec/mongoid/clients_spec.rb index 19bda0dce5..fe9853b584 100644 --- a/spec/mongoid/clients_spec.rb +++ b/spec/mongoid/clients_spec.rb @@ -1012,6 +1012,32 @@ end end end + + context 'when requesting named client' do + let(:secondary_client) do + double(Mongo::Client).tap do |client| + allow(client).to receive(:cluster).and_return(double("cluster")) + end + end + before do + + expect(Mongoid::Clients::Factory).to receive(:create) + .with(:secondary) + .and_return(secondary_client) + end + + it 'does not close the client' do + expect(secondary_client).not_to receive(:close) + + Band.with(client: :default) do |klass| + klass.mongo_client + end + + Band.with(client: :secondary) do |klass| + klass.mongo_client + end + end + end end context "when overriding the default database" do diff --git a/spec/mongoid/persistence_context_spec.rb b/spec/mongoid/persistence_context_spec.rb index 59d92dc404..b0f4771925 100644 --- a/spec/mongoid/persistence_context_spec.rb +++ b/spec/mongoid/persistence_context_spec.rb @@ -39,7 +39,6 @@ end end - describe '.get' do let(:options) do @@ -134,6 +133,32 @@ end end end + + context 'with reusable client' do + let(:options) do + {client: :some_client} + end + + let(:cluster) do + double(Mongo::Cluster) + end + + let(:client) do + double(Mongo::Client).tap do |client| + allow(client).to receive(:cluster).and_return(cluster) + end + end + + before do + expect(Mongoid::Clients).to receive(:with_name).with(:some_client).and_return(client) + expect(client).not_to receive(:close) + end + + it 'does not close the client' do + described_class.set(object, options) + described_class.clear(object, cluster.dup) + end + end end describe '#initialize' do From 50458c629d18ea93a0d67a7d15e0e89d601abfc0 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Mon, 11 Apr 2022 09:42:51 +0200 Subject: [PATCH 2/2] 5255 --- spec/mongoid/clients_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/mongoid/clients_spec.rb b/spec/mongoid/clients_spec.rb index fe9853b584..4ef3220662 100644 --- a/spec/mongoid/clients_spec.rb +++ b/spec/mongoid/clients_spec.rb @@ -1019,13 +1019,17 @@ allow(client).to receive(:cluster).and_return(double("cluster")) end end - before do + before do expect(Mongoid::Clients::Factory).to receive(:create) .with(:secondary) .and_return(secondary_client) end + after do + Mongoid::Clients.clients.delete(:secondary) + end + it 'does not close the client' do expect(secondary_client).not_to receive(:close)