redis_mutex 0.0.1.pre.alpha.pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTRmZjlhMDAzYjAyYTQ5YTIyOGRlNjliOGFlNDUzZTNkNTQ5N2E0NA==
5
+ data.tar.gz: !binary |-
6
+ MDkwYTJhMzk4ZDg0MTM0NzFiOWI5ZjlkMjU0NjFiZWYyN2Q3MDZjZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ M2E5MjU3MjlkOGVjMDdjZDNjZTE1Yzg4ODQ3ZGZmOGY5NzcwZmZlOGUyZDQ2
10
+ ZWVmYmIyNzk3YTJhYWY0MmE4ZWE1ZmQ5YzE3YjU2OTEwMjc0MDcxYWY1Mjc0
11
+ YWFjZjQ3YjViMWFlZmFiYThmMTc2YmE1MTY4ZmRmYTk1YjY0NjE=
12
+ data.tar.gz: !binary |-
13
+ ZGVhNmUyOGYxMDM5OWFlMzljYTRlNmQ5OTQyYWE3NzI3YmQ0OWRiZDEzYTVh
14
+ NDg5YjFiMjU0OTQ2N2Y2N2Q0ZDEzZmM4NzA2NmI3ZWNlOWI1NmE4OGVmYmJj
15
+ MzFlYTZhODRiNzc2NGQzOWMxOTZkMGI2NDVhNjQ2ZWViZWQ5YTk=
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3-p551
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - jruby-18mode
8
+ - jruby-19mode
9
+ - jruby-20mode
10
+ - jruby-21mode
11
+ - rbx
12
+ before_script:
13
+ - sudo add-apt-repository ppa:chris-lea/redis-server -y
14
+ - sudo apt-get update -q
15
+ - sudo apt-get install redis-server
16
+ deploy:
17
+ provider: rubygems
18
+ api_key:
19
+ secure: gbBqI1p8WM6THQeZIIEWtATjQ1EQCSkujVPCPxjczh7BSINP2pWrtv+z24EUyMzkDz5lQS4cxnsRqRMI/PPnkcJDcFoVGGT/orc65/2KEUHb76DmcNZxLndW1usTy3MIKYcv4X3iVReG5whGWVwzTNy717HE5l8CB1uh90jJljk=
20
+ gem: redis_mutex
21
+ on:
22
+ repo: GoodGuide/redis_mutex
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_mutex.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ clearing :on
2
+
3
+ guard :minitest, cli: '--pride' do
4
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
5
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
6
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ryan Taylor Long
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # RedisMutex
2
+
3
+ This gem provides very simple distributed pessimistic locking using Redis.
4
+
5
+ It provides no API, at present, for a blocking lock operation. For our purposes, we don't require blocking, as we're using this within a Sidekiq work fetcher, which already has polling semantics (and in which the order of fetchers attempting to lock isn't important).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'redis_mutex'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install redis_mutex
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. [Fork it](https://github.com/GoodGuide/redis_mutex/fork)
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: [:test]
@@ -0,0 +1,3 @@
1
+ class RedisMutex
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,69 @@
1
+ require "redis_mutex/version"
2
+ require 'securerandom'
3
+
4
+ class RedisMutex
5
+ SHAs = {}
6
+ MutexNotLockedError = Class.new(StandardError)
7
+
8
+ def initialize(redis, name, token=nil)
9
+ @redis = redis
10
+ @name = name
11
+ @token = token || SecureRandom.base64(16)
12
+ end
13
+
14
+ def key
15
+ ['mutex', @name].join(':')
16
+ end
17
+
18
+ attr_accessor :token
19
+
20
+ # returns true if you got the lock, false otherwise
21
+ def acquire_lock(expire)
22
+ @redis.set(key, token, nx: true, ex: expire)
23
+ end
24
+
25
+ def release
26
+ sha = SHAs[:delete_if_locked] ||= @redis.script(:load, <<-LUA)
27
+ if ARGV[1] ~= redis.call('get', KEYS[1]) then
28
+ return false
29
+ end
30
+
31
+ return redis.call('del', KEYS[1])
32
+ LUA
33
+
34
+ @redis.evalsha(sha, [key], [token]) or raise MutexNotLockedError
35
+ end
36
+
37
+ def renew(expire)
38
+ sha = SHAs[:set_expiration_if_locked] ||= @redis.script(:load, <<-LUA)
39
+ if ARGV[1] ~= redis.call('get', KEYS[1]) then
40
+ return false
41
+ end
42
+
43
+ return redis.call('expire', KEYS[1], ARGV[2])
44
+ LUA
45
+
46
+ @redis.evalsha(sha, [key], [token, expire]) or raise MutexNotLockedError
47
+ end
48
+
49
+ def set_token(new_token)
50
+ sha = SHAs[:compare_and_swap] ||= @redis.script(:load, <<-LUA)
51
+ if ARGV[1] ~= redis.call('get', KEYS[1]) then
52
+ return false
53
+ end
54
+
55
+ return redis.call('set', KEYS[1], ARGV[2])
56
+ LUA
57
+
58
+ @redis.evalsha(sha, [key], [token, new_token]) or raise MutexNotLockedError
59
+ @token = new_token
60
+ end
61
+
62
+ def locked?
63
+ @redis.get(key) == token
64
+ end
65
+
66
+ def verify!
67
+ locked? or raise MutexNotLockedError
68
+ end
69
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_mutex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_mutex"
8
+ if ENV['TRAVIS']
9
+ spec.version = "#{RedisMutex::VERSION}-alpha-#{ENV['TRAVIS_BUILD_NUMBER']}"
10
+ else
11
+ spec.version = RedisMutex::VERSION
12
+ end
13
+ spec.authors = ["Ryan Taylor Long"]
14
+ spec.email = ["ryan.long@goodguide.com"]
15
+ spec.summary = %q{Simple distributed mutex using Redis.}
16
+ spec.description = %q{}
17
+ spec.homepage = "https://github.com/GoodGuide/redis_mutex"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.5.0"
28
+ spec.add_development_dependency "guard-minitest", "~> 2.4.0"
29
+ spec.add_development_dependency "guard", "~> 2.11.1"
30
+ spec.add_runtime_dependency 'redis', '~> 3.2.0'
31
+ end
@@ -0,0 +1,120 @@
1
+ require "minitest/autorun"
2
+ require 'redis_mutex'
3
+ require 'redis'
4
+
5
+ class TestRedisMutex < Minitest::Test
6
+ DEFAULT_TIMEOUT = 1
7
+ LONGER_TIMEOUT = 10
8
+
9
+ def setup
10
+ # NB: this test suite requires a redis server to be available. set ENV['REDIS_URL'] to change the default hostname/port
11
+ @redis = Redis.new
12
+ @redis.info # check if connection is working and die early if not
13
+
14
+ @mutex_name = :test_mutex
15
+ @mutex_a = RedisMutex.new(@redis, @mutex_name)
16
+ @mutex_b = RedisMutex.new(@redis, @mutex_name, 'foo')
17
+ @key = @mutex_a.key
18
+ @redis.del(@key)
19
+ end
20
+
21
+ attr_reader :mutex_a, :mutex_b, :key, :redis, :mutex_name
22
+
23
+ def teardown
24
+ redis.del(key)
25
+ end
26
+
27
+ def test_acquire_lock_succeeds_when_unlocked
28
+ assert mutex_a.acquire_lock(DEFAULT_TIMEOUT)
29
+ assert redis.get(key) == mutex_a.token
30
+ assert redis.pttl(key) <= DEFAULT_TIMEOUT * 1000 # verify expiration was set
31
+ end
32
+
33
+ def test_acquire_lock_fails_when_locked
34
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
35
+ assert !mutex_b.acquire_lock(DEFAULT_TIMEOUT)
36
+ assert redis.get(key) == mutex_a.token
37
+ end
38
+
39
+ def test_token_setter_doesnt_have_side_effects
40
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
41
+ original_token = mutex_a.token
42
+ mutex_a.token = 'foo'
43
+ assert redis.get(key) == original_token
44
+ end
45
+
46
+ def test_locked_when_unlocked
47
+ assert !mutex_a.locked?
48
+ end
49
+
50
+ def test_locked_when_locked
51
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
52
+ assert mutex_a.locked?
53
+ end
54
+
55
+ def test_release_succeeds_when_holding_lock
56
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
57
+ assert mutex_a.release
58
+ end
59
+
60
+ def test_release_fails_when_not_holding_lock
61
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
62
+
63
+ assert_raises(RedisMutex::MutexNotLockedError) do
64
+ mutex_b.release
65
+ end
66
+ end
67
+
68
+ def test_renew_succeeds_when_holding_lock
69
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
70
+ assert mutex_a.renew(LONGER_TIMEOUT)
71
+ assert redis.ttl(@key) > 9
72
+ end
73
+
74
+ def test_renew_fails_when_not_holding_lock
75
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
76
+
77
+ assert_raises(RedisMutex::MutexNotLockedError) do
78
+ mutex_b.renew(LONGER_TIMEOUT)
79
+ end
80
+ end
81
+
82
+ def test_set_token_succeeds_when_holding_lock
83
+ new_token = 'bar'
84
+
85
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
86
+
87
+ assert redis.get(key) == mutex_a.token
88
+ assert mutex_a.set_token(new_token)
89
+ assert redis.get(key) == new_token
90
+ assert mutex_a.token == new_token
91
+ end
92
+
93
+ def test_set_token_fails_when_not_holding_lock
94
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
95
+
96
+ assert_raises(RedisMutex::MutexNotLockedError) do
97
+ mutex_b.set_token('bar')
98
+ end
99
+ end
100
+
101
+ def test_verify_succeeds_when_holding_lock
102
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
103
+ assert mutex_a.verify!
104
+ end
105
+
106
+ def test_verify_fails_when_not_holding_lock
107
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
108
+
109
+ assert_raises(RedisMutex::MutexNotLockedError) do
110
+ mutex_b.verify!
111
+ end
112
+ end
113
+
114
+ def test_handoff
115
+ mutex_a.acquire_lock(DEFAULT_TIMEOUT)
116
+
117
+ assert RedisMutex.new(redis, mutex_name, mutex_a.token).release
118
+ end
119
+
120
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_mutex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.alpha.pre.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Taylor Long
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.4.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.11.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.11.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: redis
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ description: ''
98
+ email:
99
+ - ryan.long@goodguide.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .travis.yml
106
+ - Gemfile
107
+ - Guardfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/redis_mutex.rb
112
+ - lib/redis_mutex/version.rb
113
+ - redis_mutex.gemspec
114
+ - test/test_redis_mutex.rb
115
+ homepage: https://github.com/GoodGuide/redis_mutex
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>'
131
+ - !ruby/object:Gem::Version
132
+ version: 1.3.1
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Simple distributed mutex using Redis.
139
+ test_files:
140
+ - test/test_redis_mutex.rb