redislock 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redislock.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tobias Lutke
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.
@@ -0,0 +1,29 @@
1
+ # Redislock
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'redislock'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install redislock
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << '.' << 'lib' << 'test'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.verbose = false
9
+ end
@@ -0,0 +1,72 @@
1
+ require "redis"
2
+ require "speedytime"
3
+ require "redislock/version"
4
+
5
+ module Redislock
6
+
7
+ class Error < StandardError
8
+ end
9
+
10
+ def self.redis=(redis)
11
+ @redis = redis
12
+ end
13
+
14
+ def self.redis
15
+ @redis || raise(Error, "Please set Redislock.redis before using this class")
16
+ end
17
+
18
+ def self.aquire(name, seconds, &block)
19
+ current = Speedytime.current
20
+ expiry = current + seconds + 1
21
+ key = "lock.#{name}"
22
+
23
+ # Try to aquire the lock, if we can do this
24
+ # we can simply execute the block with the
25
+ # help of the Lock strucutre
26
+ if Redislock.redis.setnx(key, expiry)
27
+ return Lock.new(key, &block).call
28
+ end
29
+
30
+ # we didn't get the lock, lets see if there is a deadlock
31
+ # that we can resolve via an expired lock
32
+ upstream_value = Redislock.redis.get(key)
33
+ if upstream_value.to_i < current
34
+ # ok we are expired. Let's see if we can aquire this
35
+ #
36
+ # Note, there are likely multiple processes competing
37
+ # to aquire this lock, therefore we have to use
38
+ # atomic primitives here to avoid mistakes
39
+
40
+ if Redislock.redis.getset(key, expiry) == upstream_value
41
+ return Lock.new(key, &block).call
42
+ end
43
+ end
44
+
45
+ # in all other cases we fail to aquire the lock
46
+ return false
47
+ end
48
+
49
+ class Lock
50
+ def initialize(key, &block)
51
+ @key, @block = key, block
52
+ end
53
+
54
+ def relock(seconds)
55
+ Redislock.redis.set(@key, Speedytime.current + seconds + 1)
56
+ end
57
+
58
+ def extend(seconds)
59
+ Redislock.redis.incr(@key, seconds)
60
+ end
61
+
62
+ def call
63
+ @block.call(self)
64
+ return true
65
+ ensure
66
+ if Redislock.redis.del(@key) != 1
67
+ raise Error, "Lock was lost during run"
68
+ end
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module Redislock
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redislock/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "redislock"
8
+ gem.version = Redislock::VERSION
9
+ gem.authors = ["Tobias Lutke"]
10
+ gem.email = ["tobi@shopify.com"]
11
+ gem.description = "A redis lock as suggested by redis documentation"
12
+ gem.summary = "Lock for distributed systems coordinated by redis"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency("redis")
20
+ gem.add_dependency("speedytime")
21
+ end
@@ -0,0 +1,75 @@
1
+ require "test/unit"
2
+
3
+ require "redislock"
4
+
5
+ class TestRedisLock < Test::Unit::TestCase
6
+
7
+ def setup
8
+ Redislock.redis = Redis.new()
9
+ Redislock.redis.flushall()
10
+ end
11
+
12
+ def test_redis_cmds_assumptions
13
+ assert_equal true, Redislock.redis.setnx("does.not.exist", 123)
14
+ assert_equal false, Redislock.redis.setnx("does.not.exist", 123)
15
+ assert_equal "123", Redislock.redis.getset("does.not.exist", 124)
16
+ assert_equal "124", Redislock.redis.get("does.not.exist")
17
+ end
18
+
19
+ def test_aquire
20
+ ok = false
21
+ Redislock.aquire "foo", 10 do
22
+ ok = true
23
+ end
24
+
25
+ assert ok
26
+ end
27
+
28
+ def test_lock_bumping
29
+ ok = false
30
+ Redislock.aquire "foo", 10 do |lock|
31
+ lock.relock(10)
32
+ ok = true
33
+ assert_equal Speedytime.current + 10 + 1, Redislock.redis.get("lock.foo").to_i
34
+ end
35
+
36
+ assert ok
37
+ end
38
+
39
+ def test_aquire_outdated_lock
40
+ ok = false
41
+ Redislock.redis.set "lock.foo", Speedytime.current - 15
42
+ Redislock.aquire "foo", 10 do |lock|
43
+ ok = true
44
+ end
45
+
46
+ assert ok
47
+ end
48
+
49
+ def test_dont_aquire_live_lock
50
+ ok = false
51
+ Redislock.redis.set "lock.foo", Speedytime.current + 1
52
+ Redislock.aquire "foo", 10 do |lock|
53
+ ok = true
54
+ end
55
+
56
+ assert !ok
57
+ end
58
+
59
+
60
+
61
+ def test_lock_once
62
+ num = 0
63
+ incr = -> { num += 1 }
64
+
65
+ Redislock.aquire "foo", 10 do
66
+ incr.call
67
+
68
+ assert !Redislock.aquire("foo", 0, &incr)
69
+ assert !Redislock.aquire("foo", 0, &incr)
70
+ assert !Redislock.aquire("foo", 0, &incr)
71
+ end
72
+
73
+ assert_equal 1, num
74
+ end
75
+ end
@@ -0,0 +1,26 @@
1
+ require "redis"
2
+ require "redislock"
3
+ require 'timeout'
4
+ num = 0
5
+
6
+
7
+ redis = Redis.new
8
+ redis.flushall
9
+
10
+ 10.times do
11
+ fork do
12
+ Redislock.redis = Redis.new
13
+ Timeout.timeout 1 do
14
+ loop do
15
+ Redislock.aquire 'lock', 1 do |lock|
16
+ Redislock.redis.incr "count"
17
+ sleep(0.1)
18
+ end
19
+ end
20
+ end rescue Timeout::Error
21
+ end
22
+ end
23
+
24
+
25
+ sleep(1.1)
26
+ puts redis.get("count") + " runs in 1 second"
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redislock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tobias Lutke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: speedytime
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A redis lock as suggested by redis documentation
47
+ email:
48
+ - tobi@shopify.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/redislock.rb
59
+ - lib/redislock/version.rb
60
+ - redislock.gemspec
61
+ - test/redislock_test.rb
62
+ - test/run.rb
63
+ homepage: ''
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Lock for distributed systems coordinated by redis
87
+ test_files:
88
+ - test/redislock_test.rb
89
+ - test/run.rb