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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
43c3fa44
Commit
43c3fa44
authored
Jun 23, 2017
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce #renew for ExclusiveLease
parent
6b86ce75
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletion
+31
-1
lib/gitlab/exclusive_lease.rb
lib/gitlab/exclusive_lease.rb
+18
-1
spec/lib/gitlab/exclusive_lease_spec.rb
spec/lib/gitlab/exclusive_lease_spec.rb
+13
-0
No files found.
lib/gitlab/exclusive_lease.rb
View file @
43c3fa44
...
...
@@ -10,13 +10,21 @@ module Gitlab
# ExclusiveLease.
#
class
ExclusiveLease
LUA_CANCEL_SCRIPT
=
<<
-
EOS
.
freeze
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
LUA_RENEW_SCRIPT
=
<<~
EOS
.
freeze
local key, uuid, ttl = KEYS[1], ARGV[1], ARGV[2]
if redis.call("get", key) == uuid then
redis.call("expire", key, ttl)
return uuid
end
EOS
def
self
.
cancel
(
key
,
uuid
)
Gitlab
::
Redis
.
with
do
|
redis
|
redis
.
eval
(
LUA_CANCEL_SCRIPT
,
keys:
[
redis_key
(
key
)],
argv:
[
uuid
])
...
...
@@ -42,6 +50,15 @@ module Gitlab
end
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
Gitlab
::
Redis
.
with
do
|
redis
|
result
=
redis
.
eval
(
LUA_RENEW_SCRIPT
,
keys:
[
@redis_key
],
argv:
[
@uuid
,
@timeout
])
result
==
@uuid
end
end
# Returns true if the key for this lease is set.
def
exists?
Gitlab
::
Redis
.
with
do
|
redis
|
...
...
spec/lib/gitlab/exclusive_lease_spec.rb
View file @
43c3fa44
...
...
@@ -19,6 +19,19 @@ describe Gitlab::ExclusiveLease, type: :redis do
end
end
describe
'#renew'
do
it
'returns true when we have the existing lease'
do
lease
=
described_class
.
new
(
unique_key
,
timeout:
3600
)
expect
(
lease
.
try_obtain
).
to
be_present
expect
(
lease
.
renew
).
to
be_truthy
end
it
'returns false when we dont have a lease'
do
lease
=
described_class
.
new
(
unique_key
,
timeout:
3600
)
expect
(
lease
.
renew
).
to
be_falsey
end
end
describe
'#exists?'
do
it
'returns true for an existing lease'
do
lease
=
described_class
.
new
(
unique_key
,
timeout:
3600
)
...
...
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