debian-mirror-gitlab/lib/gitlab/query_limiting.rb

42 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module QueryLimiting
# Returns true if we should enable tracking of query counts.
#
2021-04-17 20:07:23 +05:30
# This is only enabled in development and test to ensure we don't produce
# any errors that users of other environments can't do anything about themselves.
2021-04-29 21:17:54 +05:30
def self.enabled_for_env?
2018-03-17 18:26:18 +05:30
Rails.env.development? || Rails.env.test?
end
2021-04-29 21:17:54 +05:30
def self.enabled?
enabled_for_env? &&
!Gitlab::SafeRequestStore[:query_limiting_disabled]
end
2018-03-17 18:26:18 +05:30
# Allows the current request to execute any number of SQL queries.
#
# This method should _only_ be used when there's a corresponding issue to
# reduce the number of queries.
#
# The issue URL is only meant to push developers into creating an issue
2021-04-29 21:17:54 +05:30
# instead of blindly disabling for offending blocks of code.
def self.disable!(issue_url)
2018-03-17 18:26:18 +05:30
unless issue_url.start_with?('https://')
raise(
ArgumentError,
2021-04-29 21:17:54 +05:30
'You must provide a valid issue URL in order to allow a block of code'
2018-03-17 18:26:18 +05:30
)
end
2021-04-29 21:17:54 +05:30
Gitlab::SafeRequestStore[:query_limiting_disabled] = true
end
# Enables query limiting for the request.
def self.enable!
Gitlab::SafeRequestStore[:query_limiting_disabled] = nil
2018-03-17 18:26:18 +05:30
end
end
end