diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-07-03 16:20:27 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-07-03 16:20:27 +0900 |
commit | 93a964d449ad29e94e785209d7ecde217a8e9b25 (patch) | |
tree | ba4abf6750e33ae6c0b64cc3ddf7109cb5214480 /lib/gitlab/exclusive_lease_helpers.rb | |
parent | 02e3a624d29df1736a170a1484b5686f6dfe24d5 (diff) | |
download | gitlab-ce-93a964d449ad29e94e785209d7ecde217a8e9b25.tar.gz |
Add spec for ExclusiveLeaseHelpers
Diffstat (limited to 'lib/gitlab/exclusive_lease_helpers.rb')
-rw-r--r-- | lib/gitlab/exclusive_lease_helpers.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/gitlab/exclusive_lease_helpers.rb b/lib/gitlab/exclusive_lease_helpers.rb new file mode 100644 index 00000000000..ab6838adc6d --- /dev/null +++ b/lib/gitlab/exclusive_lease_helpers.rb @@ -0,0 +1,29 @@ +module Gitlab + # This module provides helper methods which are intregrated with GitLab::ExclusiveLease + module ExclusiveLeaseHelpers + FailedToObtainLockError = Class.new(StandardError) + + ## + # This helper method blocks a process/thread until the other process cancel the obrainted lease key. + # + # Note: It's basically discouraged to use this method in the unicorn's thread, + # because it holds the connection until all `retries` is consumed. + # This could potentially eat up all connection pools. + def in_lock(key, ttl: 1.minute, retries: 10, sleep_sec: 0.01.seconds) + lease = Gitlab::ExclusiveLease.new(key, timeout: ttl) + + until uuid = lease.try_obtain + # Keep trying until we obtain the lease. To prevent hammering Redis too + # much we'll wait for a bit. + sleep(sleep_sec) + break if (retries -= 1) < 0 + end + + raise FailedToObtainLockError, 'Failed to obtain a lock' unless uuid + + return yield + ensure + Gitlab::ExclusiveLease.cancel(key, uuid) + end + end +end |