mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9d3f1b7a1 | ||
|
|
270c05b80e | ||
|
|
60e167e146 | ||
|
|
ad08c8b5ea | ||
|
|
9b8ef6c244 | ||
|
|
b8a1d7d9ac | ||
|
|
c58a247156 | ||
|
|
8c37c24ee6 | ||
|
|
29da8cf090 | ||
|
|
8ed488f071 | ||
|
|
152b6acf9c | ||
|
|
5e10afe534 | ||
|
|
0423eb9e12 | ||
|
|
ca46f5f369 | ||
|
|
1022a6f9d3 | ||
|
|
6be3a5bdda | ||
|
|
aa4da5d739 | ||
|
|
fdb0b7f9d5 | ||
|
|
a13edcf7d1 | ||
|
|
af1c476f08 | ||
|
|
58fae54022 | ||
|
|
2088fd90b3 | ||
|
|
05661e143c |
@@ -74,7 +74,7 @@ Style/SpaceInsideBrackets:
|
|||||||
Style/AndOr:
|
Style/AndOr:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
Style/TrailingComma:
|
Style/TrailingCommaInLiteral:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceBeforeComma:
|
Style/SpaceBeforeComma:
|
||||||
@@ -98,7 +98,7 @@ Style/SpaceAfterColon:
|
|||||||
Style/SpaceAfterComma:
|
Style/SpaceAfterComma:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceAfterControlKeyword:
|
Style/SpaceAroundKeyword:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Style/SpaceAfterNot:
|
Style/SpaceAfterNot:
|
||||||
@@ -163,7 +163,7 @@ Style/StringLiterals:
|
|||||||
EnforcedStyle: double_quotes
|
EnforcedStyle: double_quotes
|
||||||
|
|
||||||
Metrics/CyclomaticComplexity:
|
Metrics/CyclomaticComplexity:
|
||||||
Max: 8
|
Max: 10
|
||||||
|
|
||||||
Metrics/LineLength:
|
Metrics/LineLength:
|
||||||
Max: 128
|
Max: 128
|
||||||
@@ -214,3 +214,6 @@ Metrics/ParameterLists:
|
|||||||
|
|
||||||
Metrics/PerceivedComplexity:
|
Metrics/PerceivedComplexity:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Style/Documentation:
|
||||||
|
Enabled: false
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
rvm:
|
rvm:
|
||||||
- 2.2.0
|
- 2.2.6
|
||||||
|
- 2.3.7
|
||||||
|
- 2.4.4
|
||||||
|
- 2.5.1
|
||||||
services:
|
services:
|
||||||
- memcached
|
- memcached
|
||||||
- redis-server
|
- redis-server
|
||||||
|
|||||||
13
CHANGELOG.md
13
CHANGELOG.md
@@ -1,3 +1,16 @@
|
|||||||
|
## 0.3.4
|
||||||
|
|
||||||
|
- Support for connection pooling when using memcached locks, via `with` blocks using Dalli (thanks to Lev).
|
||||||
|
|
||||||
|
## 0.3.3
|
||||||
|
|
||||||
|
- Default TTL for keys to allow for short-lived locking keys (thanks to Ian Remillard) without leaking memory.
|
||||||
|
- Vastly improve initial lock acquisition, especially on Redis (thanks to Jeremy Wadscak).
|
||||||
|
|
||||||
|
## 0.3.2
|
||||||
|
|
||||||
|
- Custom lock tokens (thanks to avokhmin).
|
||||||
|
|
||||||
## 0.3.1
|
## 0.3.1
|
||||||
|
|
||||||
- Slight memory leak fix.
|
- Slight memory leak fix.
|
||||||
|
|||||||
@@ -72,6 +72,15 @@ suo.lock do |token|
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Time To Live
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Suo::Client::Redis.new("bar_resource", ttl: 60) #ttl in seconds
|
||||||
|
```
|
||||||
|
|
||||||
|
A key representing a set of lockable resources is removed once the last resource lock is released and the `ttl` time runs out. When another lock is acquired and the key has been removed the key has to be recreated.
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
- more race condition tests
|
- more race condition tests
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ module Suo
|
|||||||
acquisition_timeout: 0.1,
|
acquisition_timeout: 0.1,
|
||||||
acquisition_delay: 0.01,
|
acquisition_delay: 0.01,
|
||||||
stale_lock_expiration: 3600,
|
stale_lock_expiration: 3600,
|
||||||
resources: 1
|
resources: 1,
|
||||||
|
ttl: 60,
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
BLANK_STR = "".freeze
|
BLANK_STR = "".freeze
|
||||||
@@ -26,8 +27,8 @@ module Suo
|
|||||||
super() # initialize Monitor mixin for thread safety
|
super() # initialize Monitor mixin for thread safety
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock
|
def lock(custom_token = nil)
|
||||||
token = acquire_lock
|
token = acquire_lock(custom_token)
|
||||||
|
|
||||||
if block_given? && token
|
if block_given? && token
|
||||||
begin
|
begin
|
||||||
@@ -55,16 +56,13 @@ module Suo
|
|||||||
retry_with_timeout do
|
retry_with_timeout do
|
||||||
val, cas = get
|
val, cas = get
|
||||||
|
|
||||||
if val.nil?
|
cas = initial_set if val.nil?
|
||||||
initial_set
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
refresh_lock(cleared_locks, token)
|
refresh_lock(cleared_locks, token)
|
||||||
|
|
||||||
break if set(serialize_locks(cleared_locks), cas)
|
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -81,7 +79,7 @@ module Suo
|
|||||||
acquisition_lock = remove_lock(cleared_locks, token)
|
acquisition_lock = remove_lock(cleared_locks, token)
|
||||||
|
|
||||||
break unless acquisition_lock
|
break unless acquisition_lock
|
||||||
break if set(serialize_locks(cleared_locks), cas)
|
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
|
||||||
end
|
end
|
||||||
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
|
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
|
||||||
# ignore - assume success due to optimistic locking
|
# ignore - assume success due to optimistic locking
|
||||||
@@ -95,16 +93,13 @@ module Suo
|
|||||||
|
|
||||||
attr_accessor :retry_count
|
attr_accessor :retry_count
|
||||||
|
|
||||||
def acquire_lock
|
def acquire_lock(token = nil)
|
||||||
token = SecureRandom.base64(16)
|
token ||= SecureRandom.base64(16)
|
||||||
|
|
||||||
retry_with_timeout do
|
retry_with_timeout do
|
||||||
val, cas = get
|
val, cas = get
|
||||||
|
|
||||||
if val.nil?
|
cas = initial_set if val.nil?
|
||||||
initial_set
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
cleared_locks = deserialize_and_clear_locks(val)
|
cleared_locks = deserialize_and_clear_locks(val)
|
||||||
|
|
||||||
|
|||||||
@@ -7,21 +7,29 @@ module Suo
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clear
|
def clear
|
||||||
@client.delete(@key)
|
@client.with { |client| client.delete(@key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get
|
def get
|
||||||
@client.get_cas(@key)
|
@client.with { |client| client.get_cas(@key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(newval, cas)
|
def set(newval, cas, expire: false)
|
||||||
@client.set_cas(@key, newval, cas)
|
if expire
|
||||||
|
@client.with { |client| client.set_cas(@key, newval, cas, @options[:ttl]) }
|
||||||
|
else
|
||||||
|
@client.with { |client| client.set_cas(@key, newval, cas) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(val = BLANK_STR)
|
def initial_set(val = BLANK_STR)
|
||||||
@client.set(@key, val)
|
@client.with do |client|
|
||||||
|
client.set(@key, val)
|
||||||
|
_val, cas = client.get_cas(@key)
|
||||||
|
cas
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,9 +18,13 @@ module Suo
|
|||||||
[@client.get(@key), nil]
|
[@client.get(@key), nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(newval, _)
|
def set(newval, _, expire: false)
|
||||||
ret = @client.multi do |multi|
|
ret = @client.multi do |multi|
|
||||||
multi.set(@key, newval)
|
if expire
|
||||||
|
multi.setex(@key, @options[:ttl], newval)
|
||||||
|
else
|
||||||
|
multi.set(@key, newval)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ret && ret[0] == OK_STR
|
ret && ret[0] == OK_STR
|
||||||
@@ -35,7 +39,8 @@ module Suo
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initial_set(val = BLANK_STR)
|
def initial_set(val = BLANK_STR)
|
||||||
@client.set(@key, val)
|
set(val, nil)
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Suo
|
module Suo
|
||||||
VERSION = "0.3.1"
|
VERSION = "0.3.4".freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
|
|||||||
|
|
||||||
spec.files = `git ls-files -z`.split("\x0")
|
spec.files = `git ls-files -z`.split("\x0")
|
||||||
spec.bindir = "bin"
|
spec.bindir = "bin"
|
||||||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
||||||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
||||||
spec.require_paths = ["lib"]
|
spec.require_paths = ["lib"]
|
||||||
|
|
||||||
@@ -26,9 +25,9 @@ Gem::Specification.new do |spec|
|
|||||||
spec.add_dependency "redis"
|
spec.add_dependency "redis"
|
||||||
spec.add_dependency "msgpack"
|
spec.add_dependency "msgpack"
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 1.5"
|
spec.add_development_dependency "bundler"
|
||||||
spec.add_development_dependency "rake", "~> 10.0"
|
spec.add_development_dependency "rake", "~> 10.0"
|
||||||
spec.add_development_dependency "rubocop", "~> 0.30.0"
|
spec.add_development_dependency "rubocop", "~> 0.49.0"
|
||||||
spec.add_development_dependency "minitest", "~> 5.5.0"
|
spec.add_development_dependency "minitest", "~> 5.5.0"
|
||||||
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,6 +31,12 @@ module ClientTests
|
|||||||
assert_equal false, locked
|
assert_equal false, locked
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_lock_with_custom_token
|
||||||
|
token = 'foo-bar'
|
||||||
|
lock = @client.lock token
|
||||||
|
assert_equal lock, token
|
||||||
|
end
|
||||||
|
|
||||||
def test_empty_lock_on_invalid_data
|
def test_empty_lock_on_invalid_data
|
||||||
@client.send(:initial_set, "bad value")
|
@client.send(:initial_set, "bad value")
|
||||||
assert_equal false, @client.locked?
|
assert_equal false, @client.locked?
|
||||||
|
|||||||
Reference in New Issue
Block a user