Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in readiness health check endpoint #1370

Merged
merged 3 commits into from Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/controllers/health_controller.rb
Expand Up @@ -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
Expand All @@ -31,13 +42,4 @@ def run_mongo_check
ensure
impatient_mongoid_client.close
end

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
end
6 changes: 6 additions & 0 deletions spec/requests/health_controller_spec.rb
Expand Up @@ -2,6 +2,12 @@
let(:errbit_app) { Fabricate(:app, api_key: 'APIKEY') }

describe "readiness" do
before do
if HealthController.instance_variable_defined? :@impatient_mongoid_client
HealthController.remove_instance_variable :@impatient_mongoid_client
end
end

it 'can let you know when the app is ready to receive requests' do
get '/health/readiness'
expect(response).to be_success
Expand Down