From 09256509fab4a40bb8c0bb5575bd76ea68b237f2 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Fri, 26 Oct 2018 15:03:25 +1000 Subject: [PATCH 1/3] Fix memory leak in readiness health check endpoint --- app/controllers/health_controller.rb | 2 +- spec/requests/health_controller_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index aec10bc97..b068d21a6 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -33,7 +33,7 @@ def run_mongo_check end def impatient_mongoid_client - @impatient_mongoid_client ||= Mongo::Client.new( + @@impatient_mongoid_client ||= Mongo::Client.new( Errbit::Config.mongo_url, server_selection_timeout: 0.5, connect_timeout: 0.5, diff --git a/spec/requests/health_controller_spec.rb b/spec/requests/health_controller_spec.rb index 7a28ad012..b381e0764 100644 --- a/spec/requests/health_controller_spec.rb +++ b/spec/requests/health_controller_spec.rb @@ -2,6 +2,12 @@ let(:errbit_app) { Fabricate(:app, api_key: 'APIKEY') } describe "readiness" do + before do + HealthController.class_variables.each do |name| + HealthController.remove_class_variable name + end + end + it 'can let you know when the app is ready to receive requests' do get '/health/readiness' expect(response).to be_success From 387146024534611da4310c87d652a040b9145e38 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Fri, 26 Oct 2018 16:33:10 +1000 Subject: [PATCH 2/3] Ignore warning about using a class variable for global memorisation --- app/controllers/health_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index b068d21a6..e8ee0c1ee 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -32,6 +32,7 @@ def run_mongo_check impatient_mongoid_client.close end + # rubocop:disable Style/ClassVars def impatient_mongoid_client @@impatient_mongoid_client ||= Mongo::Client.new( Errbit::Config.mongo_url, @@ -40,4 +41,5 @@ def impatient_mongoid_client socket_timeout: 0.5 ) end + # rubocop:enable Style/ClassVars end From 00cbfcf4098188038e390943254855216f0fd1f4 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Wed, 31 Oct 2018 08:07:46 +1000 Subject: [PATCH 3/3] Promote impatient_mongoid_client to a class method --- app/controllers/health_controller.rb | 22 +++++++++++----------- spec/requests/health_controller_spec.rb | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index e8ee0c1ee..73b894b03 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -19,8 +19,19 @@ def api_key_tester render json: { ok: is_good_result }, status: response_status end + def self.impatient_mongoid_client + @impatient_mongoid_client ||= Mongo::Client.new( + Errbit::Config.mongo_url, + server_selection_timeout: 0.5, + connect_timeout: 0.5, + socket_timeout: 0.5 + ) + end + private + delegate :impatient_mongoid_client, to: :class + def run_mongo_check # collections might be empty which is ok but it will raise an exception if # database cannot be contacted @@ -31,15 +42,4 @@ def run_mongo_check ensure impatient_mongoid_client.close end - - # rubocop:disable Style/ClassVars - def impatient_mongoid_client - @@impatient_mongoid_client ||= Mongo::Client.new( - Errbit::Config.mongo_url, - server_selection_timeout: 0.5, - connect_timeout: 0.5, - socket_timeout: 0.5 - ) - end - # rubocop:enable Style/ClassVars end diff --git a/spec/requests/health_controller_spec.rb b/spec/requests/health_controller_spec.rb index b381e0764..c122c4225 100644 --- a/spec/requests/health_controller_spec.rb +++ b/spec/requests/health_controller_spec.rb @@ -3,8 +3,8 @@ describe "readiness" do before do - HealthController.class_variables.each do |name| - HealthController.remove_class_variable name + if HealthController.instance_variable_defined? :@impatient_mongoid_client + HealthController.remove_instance_variable :@impatient_mongoid_client end end