require'securerandom'moduleGitlab# 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.classExclusiveLeasedefinitialize(key,timeout)@key,@timeout=key,timeoutend# Try to obtain the lease. Return true on succes,# false if the lease is already taken.deftry_obtain!!redis.set(redis_key,redis_value,nx: true,ex: @timeout)endprivatedefredis# Maybe someday we want to use a connection pool...@redis||=Redis.new(url: Gitlab::RedisConfig.url)enddefredis_key"gitlab:exclusive_lease:#{@key}"enddefredis_value@redis_value||=SecureRandom.hex(10)endendend