Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
iv
gitlab-ce
Commits
73c777cf
Commit
73c777cf
authored
Mar 11, 2016
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backport ExclusiveLease to 8.5
parent
11f388aa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
0 deletions
+74
-0
config/initializers/redis_config.rb
config/initializers/redis_config.rb
+12
-0
lib/gitlab/exclusive_lease.rb
lib/gitlab/exclusive_lease.rb
+41
-0
spec/lib/gitlab/exclusive_lease_spec.rb
spec/lib/gitlab/exclusive_lease_spec.rb
+21
-0
No files found.
config/initializers/redis_config.rb
0 → 100644
View file @
73c777cf
# 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
lib/gitlab/exclusive_lease.rb
0 → 100644
View file @
73c777cf
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
spec/lib/gitlab/exclusive_lease_spec.rb
0 → 100644
View file @
73c777cf
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment