lock-smith 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/locksmith/dynamodb.rb +18 -10
- data/lib/locksmith/log.rb +2 -2
- data/lib/locksmith/pg.rb +6 -2
- data/readme.md +1 -3
- metadata +1 -1
data/lib/locksmith/dynamodb.rb
CHANGED
@@ -15,45 +15,53 @@ module Locksmith
|
|
15
15
|
|
16
16
|
def lock(name)
|
17
17
|
lock = fetch_lock(name)
|
18
|
-
last_rev = lock[
|
18
|
+
last_rev = (lock["Locked"] || 0).to_i
|
19
19
|
new_rev = Time.now.to_i
|
20
20
|
attempts = 0
|
21
21
|
while attempts < MAX_LOCK_ATTEMPTS
|
22
22
|
begin
|
23
23
|
Timeout::timeout(LOCK_TIMEOUT) do
|
24
|
-
|
24
|
+
if last_rev != 0 && last_rev < (Time.now.to_i - TTL)
|
25
|
+
log(:at => "lock-expired", :lock => name, :last_rev => last_rev)
|
26
|
+
release_lock!(name)
|
27
|
+
end
|
25
28
|
write_lock(name, 0, new_rev)
|
26
|
-
log(at
|
29
|
+
log(:at => "lock-acquired", :lock => name, :rev => new_rev)
|
27
30
|
result = yield
|
28
31
|
release_lock(name, new_rev)
|
29
|
-
log(at
|
32
|
+
log(:at => "lock-released", :lock => name, :rev => new_rev)
|
30
33
|
return result
|
31
34
|
end
|
32
35
|
rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException
|
36
|
+
log(:at => "lock-not-acquired", :lock => name, :last_rev => last_rev, :new_rev => new_rev)
|
33
37
|
attempts += 1
|
34
38
|
rescue Timeout::Error
|
35
39
|
attempts += 1
|
36
40
|
release_lock(name, new_rev)
|
37
|
-
log(at
|
41
|
+
log(:at => "timeout-lock-released", :lock => name, :rev => new_rev)
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
42
46
|
def write_lock(name, rev, new_rev)
|
43
|
-
locks.put({Name
|
47
|
+
locks.put({:Name => name, :Locked => new_rev},
|
44
48
|
:if => {:Locked => rev})
|
45
49
|
end
|
46
50
|
|
47
51
|
def release_lock(name, rev)
|
48
|
-
locks.put({Name
|
52
|
+
locks.put({:Name => name, :Locked => 0},
|
49
53
|
:if => {:Locked => rev})
|
50
54
|
end
|
51
55
|
|
56
|
+
def release_lock!(name)
|
57
|
+
locks.put({:Name => name, :Locked => 0})
|
58
|
+
end
|
59
|
+
|
52
60
|
def fetch_lock(name)
|
53
61
|
if locks.at(name).exists?(consistent_read: true)
|
54
62
|
locks[name].attributes.to_h(consistent_read: true)
|
55
63
|
else
|
56
|
-
locks.put(Name
|
64
|
+
locks.put(:Name => name, :Locked => 0).attributes.to_h(consistent_read: true)
|
57
65
|
end
|
58
66
|
end
|
59
67
|
|
@@ -73,8 +81,8 @@ module Locksmith
|
|
73
81
|
|
74
82
|
def dynamo
|
75
83
|
@dynamo_lock.synchronize do
|
76
|
-
@db ||= AWS::DynamoDB.new(access_key_id
|
77
|
-
|
84
|
+
@db ||= AWS::DynamoDB.new(:access_key_id => Config.aws_id,
|
85
|
+
:secret_access_key => Config.aws_secret)
|
78
86
|
end
|
79
87
|
end
|
80
88
|
|
data/lib/locksmith/log.rb
CHANGED
@@ -4,11 +4,11 @@ module Locksmith
|
|
4
4
|
|
5
5
|
def log(data)
|
6
6
|
result = nil
|
7
|
-
data = {lib
|
7
|
+
data = {:lib => "locksmith"}.merge(data)
|
8
8
|
if block_given?
|
9
9
|
start = Time.now
|
10
10
|
result = yield
|
11
|
-
data.merge(elapsed
|
11
|
+
data.merge(:elapsed => Time.now - start)
|
12
12
|
end
|
13
13
|
data.reduce(out=String.new) do |s, tup|
|
14
14
|
s << [tup.first, tup.last].join("=") << " "
|
data/lib/locksmith/pg.rb
CHANGED
@@ -28,8 +28,12 @@ module Locksmith
|
|
28
28
|
conn.exec("select pg_advisory_unlock($1)", [i])
|
29
29
|
end
|
30
30
|
|
31
|
+
def conn=(conn)
|
32
|
+
@conn = conn
|
33
|
+
end
|
34
|
+
|
31
35
|
def conn
|
32
|
-
@
|
36
|
+
@conn ||= PG::Connection.open(
|
33
37
|
dburl.host,
|
34
38
|
dburl.port || 5432,
|
35
39
|
nil, '', #opts, tty
|
@@ -44,7 +48,7 @@ module Locksmith
|
|
44
48
|
end
|
45
49
|
|
46
50
|
def log(data, &blk)
|
47
|
-
Log.log({ns
|
51
|
+
Log.log({:ns => "postgresql-lock"}.merge(data), &blk)
|
48
52
|
end
|
49
53
|
|
50
54
|
end
|
data/readme.md
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
# Locksmith
|
2
2
|
|
3
|
-
**This software is beta quality. Check back later
|
3
|
+
**This software is beta quality. Check back later for production quality.**
|
4
4
|
|
5
5
|
A library of locking algorithms for a variety of data stores. Supported Data Stores:
|
6
6
|
|
7
7
|
* DynamoDB
|
8
8
|
* PostgreSQL
|
9
|
-
* TODO: Memcached
|
10
|
-
* TODO: Redis
|
11
9
|
* TODO: Doozerd
|
12
10
|
* TODO: Zookeeper
|
13
11
|
|