Skip to content

Commit bf0e43c

Browse files
jasoncodesstevecrozz
authored andcommitted
Fix memory leak in readiness health check endpoint (errbit#1370)
1 parent 87bc93a commit bf0e43c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

app/controllers/health_controller.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,19 @@ def api_key_tester
1919
render json: { ok: is_good_result }, status: response_status
2020
end
2121

22+
def self.impatient_mongoid_client
23+
@impatient_mongoid_client ||= Mongo::Client.new(
24+
Errbit::Config.mongo_url,
25+
server_selection_timeout: 0.5,
26+
connect_timeout: 0.5,
27+
socket_timeout: 0.5
28+
)
29+
end
30+
2231
private
2332

33+
delegate :impatient_mongoid_client, to: :class
34+
2435
def run_mongo_check
2536
# collections might be empty which is ok but it will raise an exception if
2637
# database cannot be contacted
@@ -31,13 +42,4 @@ def run_mongo_check
3142
ensure
3243
impatient_mongoid_client.close
3344
end
34-
35-
def impatient_mongoid_client
36-
@impatient_mongoid_client ||= Mongo::Client.new(
37-
Errbit::Config.mongo_url,
38-
server_selection_timeout: 0.5,
39-
connect_timeout: 0.5,
40-
socket_timeout: 0.5
41-
)
42-
end
4345
end

spec/requests/health_controller_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
let(:errbit_app) { Fabricate(:app, api_key: 'APIKEY') }
33

44
describe "readiness" do
5+
before do
6+
if HealthController.instance_variable_defined? :@impatient_mongoid_client
7+
HealthController.remove_instance_variable :@impatient_mongoid_client
8+
end
9+
end
10+
511
it 'can let you know when the app is ready to receive requests' do
612
get '/health/readiness'
713
expect(response).to be_success

0 commit comments

Comments
 (0)