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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43b6b34290b0dedbc0793205bb5cbb1d6e5442ebbcc241998529ed5e4d73d913
4
- data.tar.gz: b59b6d7e9c999093e30a6119bf96c782d66db865f803ac2c0f2054b76e0e9a27
3
+ metadata.gz: 53e78ca0038c386a4c60193184ad752353ed2726ad7c7af82dfdeb7fda8fd301
4
+ data.tar.gz: ed70b0a19611a98ea40068e464dd5484acf4eefd7b06717ff73645730c44bf0d
5
5
  SHA512:
6
- metadata.gz: 06f41badc92181f8d4d28e66fa59723488f78110aa3fbfcc7d974b89953db30684c529b5d08d6d0705900abdcc3f4959267f41bcb0a0e7acd1c6a3b77c547f41
7
- data.tar.gz: ffe4a652c57acbe76680eb1204f647c0fcf8cf378f7839d4778fe3e6f16279cd3189f59f4ee18f275dd496a8363b4a43c715ec93f433e177a997bd912eeb7922
6
+ metadata.gz: f5fd3c5a18afdaa297d87d45601ff4ba1e24273b6c9f03d889180dfeb41935d9704654113b1ad729cb3cc85af3364221e236706ef18794cdaf7c6c575839f273
7
+ data.tar.gz: 664ff98be39c7b905df91cc0642b245dee757ccbecaccc43951cae3e6ac8e8eee19b35e11dcb0da927fc78159ca3e50fa981b60c0041907428071dffa1f4920f
@@ -1,2 +1,11 @@
1
- # Forward module declaration
2
- module Locksy; end
1
+ require 'logger'
2
+
3
+ module Locksy
4
+ def self.logger=(value)
5
+ @logger = value
6
+ end
7
+
8
+ def self.logger
9
+ @logger ||= ::Logger.new(STDOUT)
10
+ end
11
+ end
@@ -58,7 +58,7 @@ module Locksy
58
58
  end
59
59
 
60
60
  def logger
61
- @logger ||= Logger.new
61
+ @logger || Locksy.logger
62
62
  end
63
63
 
64
64
  def expiry(after)
@@ -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
- expire_at = expiry(expire_after)
25
- dynamo_client.put_item \
26
- ({ table_name: table_name,
27
- item: { id: lock_name, expires: expire_at, lock_owner: owner },
28
- condition_expression: '(attribute_not_exists(expires) OR expires < :expires) ' \
29
- 'OR (attribute_not_exists(lock_owner) OR lock_owner = :owner)',
30
- expression_attribute_values: { ':expires' => now, ':owner' => owner } })
31
- expire_at
32
- rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
33
- if stop_waiting_at && stop_waiting_at > now
34
- # Retry at a maximum of 1/2 of the remaining time until the
35
- # current lock expires or the remaining time from the what the
36
- # caller was willing to wait, subject to a minimum of 0.1s to
37
- # prevent busy looping.
38
- _wait_for_timeout \
39
- ( if (current = retrieve_current_lock).nil?
40
- 0.1
41
- else
42
- [stop_waiting_at - now, [(current[:expires] - now) / 2, 0.1].max].min
43
- end)
44
- retry unless self.class.shutting_down?
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
@@ -0,0 +1,7 @@
1
+ require_relative './lock_interface'
2
+ require_relative './errors'
3
+
4
+ module Locksy
5
+ class Factory
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  require_relative '../locksy'
2
2
 
3
3
  module Locksy
4
- VERSION = '0.0.1'.freeze
4
+ VERSION = '0.0.3'.freeze
5
5
  end
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.1
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: 2019-10-20 00:00:00.000000000 Z
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: '10.0'
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: '10.0'
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