summaryrefslogtreecommitdiff
path: root/lib/gitlab/temporarily_allow.rb
diff options
context:
space:
mode:
authorAlessio Caiazza <acaiazza@gitlab.com>2018-06-04 13:57:51 +0200
committerAlessio Caiazza <acaiazza@gitlab.com>2018-06-04 13:57:51 +0200
commit8ffcaf3b85fdf716ffeacf4e9d4c89b922de50b9 (patch)
tree2e6acfa369e3e659ef4c6afb9290ad122a8fea5c /lib/gitlab/temporarily_allow.rb
parent1841da16abe864b3dae19636fee9e9bbe9a01b56 (diff)
parent23ae072ba2d601a3639a32f5a29302c430106bb8 (diff)
downloadgitlab-ce-11-0-stable-prepare-rc2.tar.gz
Merge branch 'master' into 11-0-stable-prepare-rc211-0-stable-prepare-rc2
Diffstat (limited to 'lib/gitlab/temporarily_allow.rb')
-rw-r--r--lib/gitlab/temporarily_allow.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/gitlab/temporarily_allow.rb b/lib/gitlab/temporarily_allow.rb
new file mode 100644
index 00000000000..880e55f71df
--- /dev/null
+++ b/lib/gitlab/temporarily_allow.rb
@@ -0,0 +1,42 @@
+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)
+ if RequestStore.active?
+ 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
+ RequestStore[:temporarily_allow] ||= Hash.new(0)
+ end
+
+ def temporarily_allow_add(key, value)
+ if RequestStore.active?
+ temporarily_allow_request_store[key] += value
+ else
+ TEMPORARILY_ALLOW_MUTEX.synchronize do
+ temporarily_allow_ivar[key] += value
+ end
+ end
+ end
+ end
+end