2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 07:48:46 -05:00
|
|
|
module Gitlab
|
|
|
|
class RequestContext
|
2020-01-20 10:09:18 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-01-13 10:07:53 -05:00
|
|
|
include Singleton
|
|
|
|
|
|
|
|
RequestDeadlineExceeded = Class.new(StandardError)
|
|
|
|
|
|
|
|
attr_accessor :client_ip, :start_thread_cpu_time, :request_start_time
|
2019-10-17 14:08:05 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
class << self
|
|
|
|
def instance
|
|
|
|
Gitlab::SafeRequestStore[:request_context] ||= new
|
2019-10-17 14:08:05 -04:00
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
def request_deadline
|
2020-01-20 10:09:18 -05:00
|
|
|
strong_memoize(:request_deadline) do
|
|
|
|
next unless request_start_time
|
2020-01-13 10:07:53 -05:00
|
|
|
|
2020-01-20 10:09:18 -05:00
|
|
|
request_start_time + max_request_duration_seconds
|
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
def ensure_deadline_not_exceeded!
|
2020-04-27 05:09:51 -04:00
|
|
|
return unless enabled?
|
2020-01-13 10:07:53 -05:00
|
|
|
return unless request_deadline
|
|
|
|
return if Gitlab::Metrics::System.real_time < request_deadline
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
raise RequestDeadlineExceeded,
|
2020-05-08 20:09:39 -04:00
|
|
|
"Request takes longer than #{max_request_duration_seconds} seconds"
|
2020-01-13 10:07:53 -05:00
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
private
|
2019-10-17 14:08:05 -04:00
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
def max_request_duration_seconds
|
|
|
|
Settings.gitlab.max_request_duration_seconds
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
2020-04-27 05:09:51 -04:00
|
|
|
|
|
|
|
def enabled?
|
|
|
|
!Rails.env.test?
|
|
|
|
end
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
2017-02-16 05:21:30 -05:00
|
|
|
end
|