2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
class RequestContext
|
2020-03-09 13:42:32 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
RequestDeadlineExceeded = Class.new(StandardError)
|
|
|
|
|
|
|
|
attr_accessor :client_ip, :start_thread_cpu_time, :request_start_time
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
class << self
|
|
|
|
def instance
|
|
|
|
Gitlab::SafeRequestStore[:request_context] ||= new
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def request_deadline
|
|
|
|
strong_memoize(:request_deadline) do
|
|
|
|
next unless request_start_time
|
|
|
|
|
|
|
|
request_start_time + max_request_duration_seconds
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def ensure_deadline_not_exceeded!
|
2020-05-24 23:13:21 +05:30
|
|
|
return unless enabled?
|
2020-03-09 13:42:32 +05:30
|
|
|
return unless request_deadline
|
|
|
|
return if Gitlab::Metrics::System.real_time < request_deadline
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
raise RequestDeadlineExceeded,
|
2020-05-24 23:13:21 +05:30
|
|
|
"Request takes longer than #{max_request_duration_seconds} seconds"
|
2020-03-09 13:42:32 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
private
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def max_request_duration_seconds
|
|
|
|
Settings.gitlab.max_request_duration_seconds
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def enabled?
|
|
|
|
!Rails.env.test?
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|