summaryrefslogtreecommitdiff
path: root/lib/gitlab/exclusive_lease.rb
blob: 62ddd45785d90780a677957c8efc957bc466464e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'securerandom'

module Gitlab
  # This class implements an 'exclusive lease'. We call it a 'lease'
  # because it has a set expiry time. We call it 'exclusive' because only
  # one caller may obtain a lease for a given key at a time. The
  # implementation is intended to work across GitLab processes and across
  # servers. It is a cheap alternative to using SQL queries and updates:
  # you do not need to change the SQL schema to start using
  # ExclusiveLease.
  #
  class ExclusiveLease
    LUA_CANCEL_SCRIPT = <<-EOS.freeze
      local key, uuid = KEYS[1], ARGV[1]
      if redis.call("get", key) == uuid then
        redis.call("del", key)
      end
    EOS

    def self.cancel(key, uuid)
      Gitlab::Redis.with do |redis|
        redis.eval(LUA_CANCEL_SCRIPT, keys: [redis_key(key)], argv: [uuid])
      end
    end

    def self.redis_key(key)
      "gitlab:exclusive_lease:#{key}"
    end

    def initialize(key, timeout:)
      @redis_key = self.class.redis_key(key)
      @timeout = timeout
      @uuid = SecureRandom.uuid
    end

    # Try to obtain the lease. Return lease UUID on success,
    # false if the lease is already taken.
    def try_obtain
      # Performing a single SET is atomic
      Gitlab::Redis.with do |redis|
        redis.set(@redis_key, @uuid, nx: true, ex: @timeout) && @uuid
      end
    end

    # Returns true if the key for this lease is set.
    def exists?
      Gitlab::Redis.with do |redis|
        redis.exists(@redis_key)
      end
    end
  end
end