simple_redis_lock 0.1.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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27defc46c963a46fee2e0fe00644760e482628ed
4
+ data.tar.gz: 6ab18186ccd4c6acc7ec3de0146965c5831b68b0
5
+ SHA512:
6
+ metadata.gz: 7a42cc9712b96a3543552316d3302ce30d895d56da33da50d598c16679bee64642c852c927d18bdc1cb5d4cc6de9140b2b8bada62629b0c1c4c5212e24ae7e3b
7
+ data.tar.gz: bad7be72052aaea479c4d8f6f3b95310331602655fd4fe352cdc02cd8a1d51b0c0e2e8c19dfbae94602e1487d0e4b21659fd046431e421185cbc9601094099e4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nitzanav@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_redis_lock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nitzan Aviram
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Simple Redis Lock
2
+
3
+ Simple and fast lock using one single redis call of 'SET k v NX EX'
4
+ It is non blocking, meaning that if you cannot acquire the lock, the given block will not be executed.
5
+
6
+ While the alternatives are awesome, I wanted a simple solution, kind of 5 lines of code, and a simple single redis solution.
7
+
8
+ This is the code of the gem, nice and knit:
9
+ ```ruby
10
+ if @redis.set(key, Time.now, nx: true, px: timeout)
11
+ begin
12
+ yield
13
+ ensure
14
+ @redis.del key
15
+ end
16
+ end
17
+ ```
18
+
19
+ And here is a more extensive usage:
20
+ ```ruby
21
+ simple_lock = SimpleRedisLock.create(Redis.new) # or just SimpleRedisLock.create
22
+ Thread.new do
23
+ simple_lock.lock('task_384', 10) do
24
+ sleep 0.100
25
+ # Run critical code section...
26
+ 'return value'
27
+ end # => 'return value'
28
+ end
29
+ sleep 0.001 # wait for threads to sync
30
+ simple_lock.acquired_at('task_384') # => 2016-10-12 03:25:19.287 +0300
31
+ simple_lock.ttl('task_384') # => 9.989 # remainging seconds
32
+ simple_lock.lock('task_384', 10) # => nil # lock is still held.
33
+ sleep 0.100 # wait for block to completed and release lock
34
+
35
+ # block execution completed, lock released.
36
+ simple_lock.lock('task_384', 0.100) # => true, no block given, expecting it to expire in 100ms even if not released.
37
+ simple_lock.lock('task_384', 10) # => nil # cannot acquire lock, it is still held
38
+ sleep 0.100 # wait for expiration to pass
39
+
40
+ # Expiration passed, lock released
41
+ simple_lock.lock('task_384', 10) # => true
42
+ # Run critical code section...
43
+ simple_lock.release('task_384')
44
+ simple_lock.acquired_at('task_384') # => nil
45
+ simple_lock.ttl('task_384') # => nil
46
+ ```
47
+
48
+
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'simple_redis_lock'
56
+ ```
57
+
58
+ And then execute:
59
+
60
+ $ bundle
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install simple_redis_lock
65
+
66
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_redis_lock"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,81 @@
1
+ require 'redis'
2
+ require 'simple_redis_lock/version'
3
+
4
+ # @author Nitzan Aviram
5
+ # Locks a critical code using redis SET NX|EX/PX, it is fast and simple.
6
+ #
7
+
8
+ module SimpleRedisLock
9
+
10
+ def self.create(redis = Redis.new)
11
+ SimpleLock.new(redis)
12
+ end
13
+
14
+ class SimpleLock
15
+
16
+ def initialize(redis = Redis.new)
17
+ @redis = redis
18
+ end
19
+
20
+ # Tries to acquire a lock using redis and execute the given block.
21
+ # if lock was acquired
22
+ # when a block was given, it will execute the given block.
23
+ # when no block given it will hold the lock until [release] is called.
24
+ # if lock cannot be acquired, given block.
25
+ #
26
+ # == Returns:
27
+ # if lock was acquired, the returned value of the given block or [true] if no block given.
28
+ # if lock cannot be acquired, nil is returned
29
+ #
30
+ # Locks on a key using redis SET NX|EX/PX.
31
+ # Lock will be released when:
32
+ # 1. Block execution completed
33
+ # 2. Block raise an exception
34
+ # 3. Expiration reached
35
+ #
36
+ # == Parameters:
37
+ # key::
38
+ # The key of the lock, two threads/processes should use the same key.
39
+ # expiration::
40
+ # Expiration to release the lock, useful when using sidekiq/rescue workers that can be just killed.
41
+ # expected value is in seconds, milliseconds are accepted as float. 0.001 is 1ms. The lock will be released by the [expiration] interval.
42
+ # block:
43
+ # A block to be executed when lock is acquired.
44
+ #
45
+ def lock(key, expiration)
46
+ timeout = (expiration * 1000).to_i
47
+ if @redis.set(key, Time.now.strftime('%Y-%m-%d %H:%M:%S.%L %z'), nx: true, px: timeout)
48
+ if block_given?
49
+ begin
50
+ yield
51
+ ensure
52
+ release key
53
+ end
54
+ else
55
+ true
56
+ end
57
+ end
58
+ end
59
+
60
+ def release(key)
61
+ @redis.del key
62
+ end
63
+
64
+ # time
65
+ def acquired_at(key)
66
+ time_string = @redis.get(key)
67
+ return nil unless time_string
68
+
69
+ Time.strptime(time_string, '%Y-%m-%d %H:%M:%S.%L %z')
70
+ end
71
+
72
+ # remaining time till lock expiration
73
+ def ttl(key)
74
+ pttl = @redis.pttl(key)
75
+ return nil if pttl == -2
76
+
77
+ pttl.to_f / 1000
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleRedisLock
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_redis_lock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_redis_lock"
8
+ spec.version = SimpleRedisLock::VERSION
9
+ spec.authors = ["Nitzan Aviram"]
10
+
11
+ spec.summary = "Simple and fast lock using one single redis call of 'SET k v NX EX'"
12
+ spec.homepage = "https://github.com/nitzanav/simple_redis_lock"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.13"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "redis", "~> 3.3"
26
+ spec.add_development_dependency "fakeredis", "~> 0.5"
27
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_redis_lock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nitzan Aviram
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-12 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeredis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ description:
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".rspec"
91
+ - ".travis.yml"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/simple_redis_lock.rb
100
+ - lib/simple_redis_lock/version.rb
101
+ - simple_redis_lock.gemspec
102
+ homepage: https://github.com/nitzanav/simple_redis_lock
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.6.7
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Simple and fast lock using one single redis call of 'SET k v NX EX'
126
+ test_files: []