2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module TemporarilyAllow
|
|
|
|
TEMPORARILY_ALLOW_MUTEX = Mutex.new
|
|
|
|
|
|
|
|
def temporarily_allow(key)
|
|
|
|
temporarily_allow_add(key, 1)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
temporarily_allow_add(key, -1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def temporarily_allowed?(key)
|
2018-12-05 23:21:45 +05:30
|
|
|
if Gitlab::SafeRequestStore.active?
|
2018-11-08 19:23:39 +05:30
|
|
|
temporarily_allow_request_store[key] > 0
|
|
|
|
else
|
|
|
|
TEMPORARILY_ALLOW_MUTEX.synchronize do
|
|
|
|
temporarily_allow_ivar[key] > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def temporarily_allow_ivar
|
|
|
|
@temporarily_allow ||= Hash.new(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def temporarily_allow_request_store
|
2018-12-05 23:21:45 +05:30
|
|
|
Gitlab::SafeRequestStore[:temporarily_allow] ||= Hash.new(0)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def temporarily_allow_add(key, value)
|
2018-12-05 23:21:45 +05:30
|
|
|
if Gitlab::SafeRequestStore.active?
|
2018-11-08 19:23:39 +05:30
|
|
|
temporarily_allow_request_store[key] += value
|
|
|
|
else
|
|
|
|
TEMPORARILY_ALLOW_MUTEX.synchronize do
|
|
|
|
temporarily_allow_ivar[key] += value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|