diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-17 13:07:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-17 13:07:47 +0300 |
commit | d670c3006e6e44901bce0d53cc4768d1d80ffa92 (patch) | |
tree | 8f65743c232e5b76850c4cc264ba15e1185815ff /lib/gitlab/exclusive_lease.rb | |
parent | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-ee
Diffstat (limited to 'lib/gitlab/exclusive_lease.rb')
-rw-r--r-- | lib/gitlab/exclusive_lease.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/gitlab/exclusive_lease.rb b/lib/gitlab/exclusive_lease.rb index 6749bd6ca60..75d07a36dcd 100644 --- a/lib/gitlab/exclusive_lease.rb +++ b/lib/gitlab/exclusive_lease.rb @@ -36,6 +36,28 @@ module Gitlab end end + # yield to the {block} at most {count} times per {period} + # + # Defaults to once per hour. + # + # For example: + # + # # toot the train horn at most every 20min: + # throttle(locomotive.id, count: 3, period: 1.hour) { toot_train_horn } + # # Brake suddenly at most once every minute: + # throttle(locomotive.id, period: 1.minute) { brake_suddenly } + # # Specify a uniqueness group: + # throttle(locomotive.id, group: :locomotive_brake) { brake_suddenly } + # + # If a group is not specified, each block will get a separate group to itself. + def self.throttle(key, group: nil, period: 1.hour, count: 1, &block) + group ||= block.source_location.join(':') + + return if new("el:throttle:#{group}:#{key}", timeout: period.to_i / count).waiting? + + yield + end + def self.cancel(key, uuid) return unless key.present? @@ -79,6 +101,11 @@ module Gitlab end end + # This lease is waiting to obtain + def waiting? + !try_obtain + end + # Try to renew an existing lease. Return lease UUID on success, # false if the lease is taken by a different UUID or inexistent. def renew |