summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2016-03-11 10:17:22 +0100
committerJacob Vosmaer <contact@jacobvosmaer.nl>2016-03-11 13:38:58 +0100
commit73c777cf673345d39a17519e55005082bd70ee2e (patch)
tree44c07ed6f29ece47a8d11e7244d86258df558896
parent11f388aaac6216e49cccba3b440fcabc52cb9129 (diff)
downloadgitlab-ce-73c777cf673345d39a17519e55005082bd70ee2e.tar.gz
Backport ExclusiveLease to 8.5
-rw-r--r--config/initializers/redis_config.rb12
-rw-r--r--lib/gitlab/exclusive_lease.rb41
-rw-r--r--spec/lib/gitlab/exclusive_lease_spec.rb21
3 files changed, 74 insertions, 0 deletions
diff --git a/config/initializers/redis_config.rb b/config/initializers/redis_config.rb
new file mode 100644
index 00000000000..1cfecc2dd00
--- /dev/null
+++ b/config/initializers/redis_config.rb
@@ -0,0 +1,12 @@
+# This is a quick hack to get ExclusiveLease working in GitLab 8.5
+
+module Gitlab
+ REDIS_URL = begin
+ redis_config_file = Rails.root.join('config/resque.yml')
+ if File.exists?(redis_config_file)
+ YAML.load_file(redis_config_file)[Rails.env]
+ else
+ 'redis://localhost:6379'
+ end
+ end
+end
diff --git a/lib/gitlab/exclusive_lease.rb b/lib/gitlab/exclusive_lease.rb
new file mode 100644
index 00000000000..abf5fbe7ff1
--- /dev/null
+++ b/lib/gitlab/exclusive_lease.rb
@@ -0,0 +1,41 @@
+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.
+ #
+ # It is important to choose the timeout wisely. If the timeout is very
+ # high (1 hour) then the throughput of your operation gets very low (at
+ # most once an hour). If the timeout is lower than how long your
+ # operation may take then you cannot count on exclusivity. For example,
+ # if the timeout is 10 seconds and you do an operation which may take 20
+ # seconds then two overlapping operations may hold a lease for the same
+ # key at the same time.
+ #
+ class ExclusiveLease
+ def initialize(key, timeout:)
+ @key, @timeout = key, timeout
+ end
+
+ # Try to obtain the lease. Return true on success,
+ # false if the lease is already taken.
+ def try_obtain
+ # Performing a single SET is atomic
+ !!redis.set(redis_key, '1', nx: true, ex: @timeout)
+ end
+
+ private
+
+ def redis
+ # Maybe someday we want to use a connection pool...
+ @redis ||= Redis.new(url: Gitlab::REDIS_URL)
+ end
+
+ def redis_key
+ "gitlab:exclusive_lease:#{@key}"
+ end
+ end
+end
diff --git a/spec/lib/gitlab/exclusive_lease_spec.rb b/spec/lib/gitlab/exclusive_lease_spec.rb
new file mode 100644
index 00000000000..fbdb7ea34ac
--- /dev/null
+++ b/spec/lib/gitlab/exclusive_lease_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Gitlab::ExclusiveLease do
+ it 'cannot obtain twice before the lease has expired' do
+ lease = Gitlab::ExclusiveLease.new(unique_key, timeout: 3600)
+ expect(lease.try_obtain).to eq(true)
+ expect(lease.try_obtain).to eq(false)
+ end
+
+ it 'can obtain after the lease has expired' do
+ timeout = 1
+ lease = Gitlab::ExclusiveLease.new(unique_key, timeout: timeout)
+ lease.try_obtain # start the lease
+ sleep(2 * timeout) # lease should have expired now
+ expect(lease.try_obtain).to eq(true)
+ end
+
+ def unique_key
+ SecureRandom.hex(10)
+ end
+end