2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-15 10:21:04 -05:00
|
|
|
module Gitlab
|
|
|
|
module QueryLimiting
|
|
|
|
# Returns true if we should enable tracking of query counts.
|
|
|
|
#
|
2021-03-09 22:09:10 -05:00
|
|
|
# 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-03-16 14:11:53 -04:00
|
|
|
def self.enabled_for_env?
|
2018-02-12 14:15:26 -05:00
|
|
|
Rails.env.development? || Rails.env.test?
|
2018-01-15 10:21:04 -05:00
|
|
|
end
|
|
|
|
|
2021-03-16 14:11:53 -04:00
|
|
|
def self.enabled?
|
|
|
|
enabled_for_env? &&
|
|
|
|
!Gitlab::SafeRequestStore[:query_limiting_disabled]
|
|
|
|
end
|
|
|
|
|
2018-01-15 10:21:04 -05:00
|
|
|
# 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-03-16 14:11:53 -04:00
|
|
|
# instead of blindly disabling for offending blocks of code.
|
|
|
|
def self.disable!(issue_url)
|
2018-01-15 10:21:04 -05:00
|
|
|
unless issue_url.start_with?('https://')
|
|
|
|
raise(
|
|
|
|
ArgumentError,
|
2021-03-16 14:11:53 -04:00
|
|
|
'You must provide a valid issue URL in order to allow a block of code'
|
2018-01-15 10:21:04 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-03-16 14:11:53 -04:00
|
|
|
Gitlab::SafeRequestStore[:query_limiting_disabled] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enables query limiting for the request.
|
|
|
|
def self.enable!
|
|
|
|
Gitlab::SafeRequestStore[:query_limiting_disabled] = nil
|
2018-01-15 10:21:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|