5 Commits

Author SHA1 Message Date
Nick Elser
af1c476f08 Bump version. 2016-10-06 10:22:36 -07:00
Nick Elser
58fae54022 Minor style fixes. 2016-10-06 10:22:29 -07:00
Nick Elser
2088fd90b3 Merge pull request #1 from Shuttlerock/master
Allow to use custom token for lock
2016-10-06 10:20:35 -07:00
Vokhmin Alexey V
05661e143c Allow to use custom token for lock 2016-10-05 13:47:10 +03:00
Nick Elser
a23282dcc6 don't go around allocating empty strings willy-nilly 2015-05-07 00:16:28 -07:00
9 changed files with 40 additions and 16 deletions

View File

@@ -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

View File

@@ -1,6 +1,7 @@
language: ruby language: ruby
rvm: rvm:
- 2.2.0 - 2.2.0
- 2.3.1
services: services:
- memcached - memcached
- redis-server - redis-server

View File

@@ -1,3 +1,11 @@
## 0.3.2
- Custom lock tokens (thanks to avokhmin).
## 0.3.1
- Slight memory leak fix.
## 0.3.0 ## 0.3.0
- Dramatically simplify the interface by forcing clients to specify the key & resources at lock initialization instead of every method call. - Dramatically simplify the interface by forcing clients to specify the key & resources at lock initialization instead of every method call.

View File

@@ -46,7 +46,7 @@ suo = Suo::Client::Memcached.new("protected_key", client: some_dalli_client, acq
# manually locking/unlocking # manually locking/unlocking
# the return value from lock without a block is a unique token valid only for the current lock # the return value from lock without a block is a unique token valid only for the current lock
# which must be unlocked manually # which must be unlocked manually
token = suo token = suo.lock
foo.baz! foo.baz!
suo.unlock(token) suo.unlock(token)
@@ -77,7 +77,7 @@ end
## History ## History
View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md) View the [changelog](https://github.com/nickelser/suo/blob/master/CHANGELOG.md).
## Contributing ## Contributing

View File

@@ -8,22 +8,26 @@ module Suo
resources: 1 resources: 1
}.freeze }.freeze
BLANK_STR = "".freeze
attr_accessor :client, :key, :resources, :options attr_accessor :client, :key, :resources, :options
include MonitorMixin include MonitorMixin
def initialize(key, options = {}) def initialize(key, options = {})
fail "Client required" unless options[:client] fail "Client required" unless options[:client]
@options = DEFAULT_OPTIONS.merge(options) @options = DEFAULT_OPTIONS.merge(options)
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil @retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
@client = @options[:client] @client = @options[:client]
@resources = @options[:resources].to_i @resources = @options[:resources].to_i
@key = key @key = key
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
@@ -91,8 +95,8 @@ 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
@@ -124,7 +128,7 @@ module Suo
fail NotImplementedError fail NotImplementedError
end end
def initial_set(val = "") # rubocop:disable Lint/UnusedMethodArgument def initial_set(val = BLANK_STR) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError fail NotImplementedError
end end
@@ -158,7 +162,7 @@ module Suo
end end
def deserialize_locks(val) def deserialize_locks(val)
unpacked = (val.nil? || val == "") ? [] : MessagePack.unpack(val) unpacked = (val.nil? || val == BLANK_STR) ? [] : MessagePack.unpack(val)
unpacked.map do |time, token| unpacked.map do |time, token|
[Time.at(time), token] [Time.at(time), token]

View File

@@ -20,7 +20,7 @@ module Suo
@client.set_cas(@key, newval, cas) @client.set_cas(@key, newval, cas)
end end
def initial_set(val = "") def initial_set(val = BLANK_STR)
@client.set(@key, val) @client.set(@key, val)
end end
end end

View File

@@ -1,6 +1,8 @@
module Suo module Suo
module Client module Client
class Redis < Base class Redis < Base
OK_STR = "OK".freeze
def initialize(key, options = {}) def initialize(key, options = {})
options[:client] ||= ::Redis.new(options[:connection] || {}) options[:client] ||= ::Redis.new(options[:connection] || {})
super super
@@ -21,7 +23,7 @@ module Suo
multi.set(@key, newval) multi.set(@key, newval)
end end
ret && ret[0] == "OK" ret && ret[0] == OK_STR
end end
def synchronize def synchronize
@@ -32,7 +34,7 @@ module Suo
@client.unwatch @client.unwatch
end end
def initial_set(val = "") def initial_set(val = BLANK_STR)
@client.set(@key, val) @client.set(@key, val)
end end
end end

View File

@@ -1,3 +1,3 @@
module Suo module Suo
VERSION = "0.3.0" VERSION = "0.3.2".freeze
end end

View File

@@ -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?