locksy 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/locksy.rb +11 -2
- data/lib/locksy/base_lock.rb +1 -1
- data/lib/locksy/dynamodb.rb +30 -22
- data/lib/locksy/factory.rb +7 -0
- data/lib/locksy/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53e78ca0038c386a4c60193184ad752353ed2726ad7c7af82dfdeb7fda8fd301
|
4
|
+
data.tar.gz: ed70b0a19611a98ea40068e464dd5484acf4eefd7b06717ff73645730c44bf0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5fd3c5a18afdaa297d87d45601ff4ba1e24273b6c9f03d889180dfeb41935d9704654113b1ad729cb3cc85af3364221e236706ef18794cdaf7c6c575839f273
|
7
|
+
data.tar.gz: 664ff98be39c7b905df91cc0642b245dee757ccbecaccc43951cae3e6ac8e8eee19b35e11dcb0da927fc78159ca3e50fa981b60c0041907428071dffa1f4920f
|
data/lib/locksy.rb
CHANGED
data/lib/locksy/base_lock.rb
CHANGED
data/lib/locksy/dynamodb.rb
CHANGED
@@ -21,29 +21,37 @@ module Locksy
|
|
21
21
|
|
22
22
|
def obtain_lock(expire_after: default_expiry, wait_for: nil, **_args)
|
23
23
|
stop_waiting_at = wait_for ? now + wait_for : nil
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
24
|
+
begin
|
25
|
+
expire_at = expiry(expire_after)
|
26
|
+
logger.debug "trying to obtain lock #{lock_name} for #{owner} to be held until #{expire_at}"
|
27
|
+
dynamo_client.put_item \
|
28
|
+
({ table_name: table_name,
|
29
|
+
item: { id: lock_name, expires: expire_at, lock_owner: owner },
|
30
|
+
condition_expression: '(attribute_not_exists(expires) OR expires < :now) ' \
|
31
|
+
'OR (attribute_not_exists(lock_owner) OR lock_owner = :owner)',
|
32
|
+
expression_attribute_values: { ':now' => now, ':owner' => owner } })
|
33
|
+
logger.debug "acquired lock #{lock_name} for #{owner} to be held until #{expire_at}"
|
34
|
+
expire_at
|
35
|
+
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
|
36
|
+
if stop_waiting_at && stop_waiting_at > now
|
37
|
+
# Retry at a maximum of 1/2 of the remaining time until the
|
38
|
+
# current lock expires or the remaining time from the what the
|
39
|
+
# caller was willing to wait, subject to a minimum of 0.1s to
|
40
|
+
# prevent busy looping.
|
41
|
+
if (current = retrieve_current_lock).nil?
|
42
|
+
retry_wait = 0.1
|
43
|
+
else
|
44
|
+
retry_wait = [stop_waiting_at - now, [(current[:expires] - now) / 2, 0.1].max].min
|
45
|
+
end
|
46
|
+
logger.debug "Attempt to acquire lock #{lock_name} for #{owner} failed - "\
|
47
|
+
"lock owned by #{current[:owner]} until #{format('%0.02f', current[:expires])}. " \
|
48
|
+
"Will retry in #{format('%0.02f', retry_wait)}s"
|
49
|
+
_wait_for_timeout retry_wait
|
50
|
+
retry unless self.class.shutting_down?
|
51
|
+
end
|
52
|
+
logger.debug "Attempt to acquire lock #{lock_name} for #{owner} failed. Giving up"
|
53
|
+
raise build_not_owned_error_from_remote
|
45
54
|
end
|
46
|
-
raise build_not_owned_error_from_remote
|
47
55
|
end
|
48
56
|
|
49
57
|
def release_lock
|
data/lib/locksy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: locksy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dan@52degreesnorth.com
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 12.3.3
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 12.3.3
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -118,6 +118,7 @@ files:
|
|
118
118
|
- lib/locksy/base_lock.rb
|
119
119
|
- lib/locksy/dynamodb.rb
|
120
120
|
- lib/locksy/errors.rb
|
121
|
+
- lib/locksy/factory.rb
|
121
122
|
- lib/locksy/lock_interface.rb
|
122
123
|
- lib/locksy/memory.rb
|
123
124
|
- lib/locksy/version.rb
|