mirror of
https://github.com/dkam/suo.git
synced 2025-01-29 07:42:43 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59d3385d8a | ||
|
|
3d5c102c08 | ||
|
|
7bb28cc007 | ||
|
|
65cae9aa58 | ||
|
|
6b6eb4e590 | ||
|
|
485f2bff37 | ||
|
|
3a37a74982 | ||
|
|
b02c256c25 | ||
|
|
d57f6a15ac | ||
|
|
d293ef6fcf | ||
|
|
6e74322ff1 | ||
|
|
b9d3f1b7a1 | ||
|
|
270c05b80e | ||
|
|
60e167e146 | ||
|
|
ad08c8b5ea | ||
|
|
9b8ef6c244 | ||
|
|
b8a1d7d9ac | ||
|
|
c58a247156 | ||
|
|
8c37c24ee6 | ||
|
|
29da8cf090 | ||
|
|
8ed488f071 | ||
|
|
152b6acf9c | ||
|
|
5e10afe534 | ||
|
|
0423eb9e12 | ||
|
|
ca46f5f369 | ||
|
|
1022a6f9d3 | ||
|
|
6be3a5bdda | ||
|
|
aa4da5d739 | ||
|
|
fdb0b7f9d5 | ||
|
|
a13edcf7d1 | ||
|
|
af1c476f08 | ||
|
|
58fae54022 | ||
|
|
2088fd90b3 | ||
|
|
05661e143c | ||
|
|
a23282dcc6 |
38
.github/workflows/CI.yml
vendored
Normal file
38
.github/workflows/CI.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
ruby:
|
||||
- '2.5'
|
||||
- '2.6'
|
||||
- '2.7'
|
||||
- '3.0'
|
||||
- ruby-head
|
||||
continue-on-error: ${{ matrix.ruby == 'ruby-head' }}
|
||||
services:
|
||||
memcached:
|
||||
image: memcached
|
||||
ports:
|
||||
- 11211:11211
|
||||
redis:
|
||||
image: redis
|
||||
ports:
|
||||
- 6379:6379
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: ${{ matrix.ruby }}
|
||||
bundler-cache: true
|
||||
- run: |
|
||||
bundle exec rake
|
||||
@@ -74,7 +74,7 @@ Style/SpaceInsideBrackets:
|
||||
Style/AndOr:
|
||||
Enabled: false
|
||||
|
||||
Style/TrailingComma:
|
||||
Style/TrailingCommaInLiteral:
|
||||
Enabled: true
|
||||
|
||||
Style/SpaceBeforeComma:
|
||||
@@ -98,7 +98,7 @@ Style/SpaceAfterColon:
|
||||
Style/SpaceAfterComma:
|
||||
Enabled: true
|
||||
|
||||
Style/SpaceAfterControlKeyword:
|
||||
Style/SpaceAroundKeyword:
|
||||
Enabled: true
|
||||
|
||||
Style/SpaceAfterNot:
|
||||
@@ -163,7 +163,7 @@ Style/StringLiterals:
|
||||
EnforcedStyle: double_quotes
|
||||
|
||||
Metrics/CyclomaticComplexity:
|
||||
Max: 8
|
||||
Max: 10
|
||||
|
||||
Metrics/LineLength:
|
||||
Max: 128
|
||||
@@ -214,3 +214,6 @@ Metrics/ParameterLists:
|
||||
|
||||
Metrics/PerceivedComplexity:
|
||||
Enabled: false
|
||||
|
||||
Style/Documentation:
|
||||
Enabled: false
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
language: ruby
|
||||
rvm:
|
||||
- 2.2.0
|
||||
services:
|
||||
- memcached
|
||||
- redis-server
|
||||
24
CHANGELOG.md
24
CHANGELOG.md
@@ -1,3 +1,27 @@
|
||||
## 0.4.0
|
||||
|
||||
- Monotonic clock for locks, avoiding issues with DST (thanks @doits)
|
||||
- Pooled connection support (thanks @mlarraz)
|
||||
- Switch to Github actions for tests (thanks @mlarraz)
|
||||
- Update supported Ruby versions (thanks @mlarraz & @pat)
|
||||
|
||||
## 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
|
||||
|
||||
- Slight memory leak fix.
|
||||
|
||||
## 0.3.0
|
||||
|
||||
- Dramatically simplify the interface by forcing clients to specify the key & resources at lock initialization instead of every method call.
|
||||
|
||||
15
README.md
15
README.md
@@ -1,4 +1,4 @@
|
||||
# Suo [](https://travis-ci.org/nickelser/suo) [](https://codeclimate.com/github/nickelser/suo) [](https://codeclimate.com/github/nickelser/suo) [](http://badge.fury.io/rb/suo)
|
||||
# Suo [](https://github.com/nickelser/suo/actions?query=workflow%3ACI) [](https://codeclimate.com/github/nickelser/suo) [](http://badge.fury.io/rb/suo)
|
||||
|
||||
:lock: Distributed semaphores using Memcached or Redis in Ruby.
|
||||
|
||||
@@ -46,7 +46,7 @@ suo = Suo::Client::Memcached.new("protected_key", client: some_dalli_client, acq
|
||||
# manually locking/unlocking
|
||||
# the return value from lock without a block is a unique token valid only for the current lock
|
||||
# which must be unlocked manually
|
||||
token = suo
|
||||
token = suo.lock
|
||||
foo.baz!
|
||||
suo.unlock(token)
|
||||
|
||||
@@ -72,12 +72,21 @@ suo.lock do |token|
|
||||
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
|
||||
- more race condition tests
|
||||
|
||||
## 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
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ require "securerandom"
|
||||
require "monitor"
|
||||
|
||||
require "dalli"
|
||||
require "dalli/cas/client"
|
||||
|
||||
require "redis"
|
||||
|
||||
|
||||
@@ -5,25 +5,30 @@ module Suo
|
||||
acquisition_timeout: 0.1,
|
||||
acquisition_delay: 0.01,
|
||||
stale_lock_expiration: 3600,
|
||||
resources: 1
|
||||
resources: 1,
|
||||
ttl: 60,
|
||||
}.freeze
|
||||
|
||||
BLANK_STR = "".freeze
|
||||
|
||||
attr_accessor :client, :key, :resources, :options
|
||||
|
||||
include MonitorMixin
|
||||
|
||||
def initialize(key, options = {})
|
||||
fail "Client required" unless options[:client]
|
||||
|
||||
@options = DEFAULT_OPTIONS.merge(options)
|
||||
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
|
||||
@client = @options[:client]
|
||||
@resources = @options[:resources].to_i
|
||||
@key = key
|
||||
|
||||
super() # initialize Monitor mixin for thread safety
|
||||
end
|
||||
|
||||
def lock
|
||||
token = acquire_lock
|
||||
def lock(custom_token = nil)
|
||||
token = acquire_lock(custom_token)
|
||||
|
||||
if block_given? && token
|
||||
begin
|
||||
@@ -51,16 +56,13 @@ module Suo
|
||||
retry_with_timeout do
|
||||
val, cas = get
|
||||
|
||||
if val.nil?
|
||||
initial_set
|
||||
next
|
||||
end
|
||||
cas = initial_set if val.nil?
|
||||
|
||||
cleared_locks = deserialize_and_clear_locks(val)
|
||||
|
||||
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
|
||||
|
||||
@@ -77,7 +79,7 @@ module Suo
|
||||
acquisition_lock = remove_lock(cleared_locks, token)
|
||||
|
||||
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
|
||||
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
|
||||
# ignore - assume success due to optimistic locking
|
||||
@@ -91,16 +93,13 @@ module Suo
|
||||
|
||||
attr_accessor :retry_count
|
||||
|
||||
def acquire_lock
|
||||
token = SecureRandom.base64(16)
|
||||
def acquire_lock(token = nil)
|
||||
token ||= SecureRandom.base64(16)
|
||||
|
||||
retry_with_timeout do
|
||||
val, cas = get
|
||||
|
||||
if val.nil?
|
||||
initial_set
|
||||
next
|
||||
end
|
||||
cas = initial_set if val.nil?
|
||||
|
||||
cleared_locks = deserialize_and_clear_locks(val)
|
||||
|
||||
@@ -124,7 +123,7 @@ module Suo
|
||||
fail NotImplementedError
|
||||
end
|
||||
|
||||
def initial_set(val = "") # rubocop:disable Lint/UnusedMethodArgument
|
||||
def initial_set(val = BLANK_STR) # rubocop:disable Lint/UnusedMethodArgument
|
||||
fail NotImplementedError
|
||||
end
|
||||
|
||||
@@ -133,10 +132,10 @@ module Suo
|
||||
end
|
||||
|
||||
def retry_with_timeout
|
||||
start = Time.now.to_f
|
||||
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
|
||||
retry_count.times do
|
||||
elapsed = Time.now.to_f - start
|
||||
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
||||
break if elapsed >= options[:acquisition_timeout]
|
||||
|
||||
synchronize do
|
||||
@@ -158,7 +157,7 @@ module Suo
|
||||
end
|
||||
|
||||
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|
|
||||
[Time.at(time), token]
|
||||
|
||||
@@ -7,21 +7,29 @@ module Suo
|
||||
end
|
||||
|
||||
def clear
|
||||
@client.delete(@key)
|
||||
@client.with { |client| client.delete(@key) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get
|
||||
@client.get_cas(@key)
|
||||
@client.with { |client| client.get_cas(@key) }
|
||||
end
|
||||
|
||||
def set(newval, cas)
|
||||
@client.set_cas(@key, newval, cas)
|
||||
def set(newval, cas, expire: false)
|
||||
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
|
||||
|
||||
def initial_set(val = "")
|
||||
@client.set(@key, val)
|
||||
def initial_set(val = BLANK_STR)
|
||||
@client.with do |client|
|
||||
client.set(@key, val)
|
||||
_val, cas = client.get_cas(@key)
|
||||
cas
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,39 +1,54 @@
|
||||
module Suo
|
||||
module Client
|
||||
class Redis < Base
|
||||
OK_STR = "OK".freeze
|
||||
|
||||
def initialize(key, options = {})
|
||||
options[:client] ||= ::Redis.new(options[:connection] || {})
|
||||
super
|
||||
end
|
||||
|
||||
def clear
|
||||
@client.del(@key)
|
||||
with { |r| r.del(@key) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with(&block)
|
||||
if @client.respond_to?(:with)
|
||||
@client.with(&block)
|
||||
else
|
||||
yield @client
|
||||
end
|
||||
end
|
||||
|
||||
def get
|
||||
[@client.get(@key), nil]
|
||||
[with { |r| r.get(@key) }, nil]
|
||||
end
|
||||
|
||||
def set(newval, _)
|
||||
ret = @client.multi do |multi|
|
||||
multi.set(@key, newval)
|
||||
def set(newval, _, expire: false)
|
||||
ret = with do |r|
|
||||
r.multi do |rr|
|
||||
if expire
|
||||
rr.setex(@key, @options[:ttl], newval)
|
||||
else
|
||||
rr.set(@key, newval)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ret && ret[0] == "OK"
|
||||
ret && ret[0] == OK_STR
|
||||
end
|
||||
|
||||
def synchronize
|
||||
@client.watch(@key) do
|
||||
yield
|
||||
end
|
||||
with { |r| r.watch(@key) { yield } }
|
||||
ensure
|
||||
@client.unwatch
|
||||
with { |r| r.unwatch }
|
||||
end
|
||||
|
||||
def initial_set(val = "")
|
||||
@client.set(@key, val)
|
||||
def initial_set(val = BLANK_STR)
|
||||
set(val, nil)
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Suo
|
||||
VERSION = "0.3.0"
|
||||
VERSION = "0.4.0".freeze
|
||||
end
|
||||
|
||||
@@ -16,19 +16,18 @@ Gem::Specification.new do |spec|
|
||||
|
||||
spec.files = `git ls-files -z`.split("\x0")
|
||||
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.require_paths = ["lib"]
|
||||
|
||||
spec.required_ruby_version = "~> 2.0"
|
||||
spec.required_ruby_version = ">= 2.5"
|
||||
|
||||
spec.add_dependency "dalli"
|
||||
spec.add_dependency "redis"
|
||||
spec.add_dependency "msgpack"
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 1.5"
|
||||
spec.add_development_dependency "rake", "~> 10.0"
|
||||
spec.add_development_dependency "rubocop", "~> 0.30.0"
|
||||
spec.add_development_dependency "bundler"
|
||||
spec.add_development_dependency "rake", "~> 13.0"
|
||||
spec.add_development_dependency "rubocop", "~> 0.49.0"
|
||||
spec.add_development_dependency "minitest", "~> 5.5.0"
|
||||
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
||||
end
|
||||
|
||||
@@ -31,6 +31,12 @@ module ClientTests
|
||||
assert_equal false, locked
|
||||
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
|
||||
@client.send(:initial_set, "bad value")
|
||||
assert_equal false, @client.locked?
|
||||
|
||||
Reference in New Issue
Block a user