debian-mirror-gitlab/lib/gitlab/exclusive_lease_helpers.rb

35 lines
1.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
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)
2019-02-15 15:39:39 +05:30
raise ArgumentError, 'Key needs to be specified' unless key
2018-11-08 19:23:39 +05:30
lease = Gitlab::ExclusiveLease.new(key, timeout: ttl)
2019-09-30 21:07:59 +05:30
retried = false
2018-11-08 19:23:39 +05:30
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)
2019-09-30 21:07:59 +05:30
(retries -= 1) < 0 ? break : retried ||= true
2018-11-08 19:23:39 +05:30
end
raise FailedToObtainLockError, 'Failed to obtain a lock' unless uuid
2019-09-30 21:07:59 +05:30
yield(retried)
2018-11-08 19:23:39 +05:30
ensure
Gitlab::ExclusiveLease.cancel(key, uuid)
end
end
end