distributed-mutex 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e44a407db9f761aa7a3ec15c93f6ef0402fc10e4
4
+ data.tar.gz: 2c20deff1389313e681f8d87109db642892d7b0c
5
+ SHA512:
6
+ metadata.gz: b4ce245fac74823c0e4e50d0886030fadbc5c3f74334ba63d429b0895cbcb0da1beae4b1c9510eb81341c9a3d9dc1f4a621d34029a46b7140317c0345ea6850e
7
+ data.tar.gz: c14bb03f42208a083a89f043b8c2c805c3ebf5b05028258bca6f9c5d4f84c7d11096c11b6a3680a1890e9d0bd0c33a17974c2d97b5e86bed797c403309f28734
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in distributed-mutex.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016
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.
@@ -0,0 +1,49 @@
1
+ # DistributedMutex
2
+
3
+ [![Build Status](https://travis-ci.org/unchartedcode/distributed-mutex.svg?branch=master)](https://travis-ci.org/unchartedcode/distributed-mutex)
4
+ [![Coverage Status](https://coveralls.io/repos/github/unchartedcode/distributed-mutex/badge.svg?branch=master)](https://coveralls.io/github/unchartedcode/distributed-mutex?branch=master)
5
+
6
+ This gem allows you to lock specific chunks of code based on a shared key. It's a Mutex, but locks beyond a single machine by utilizing [Redis](http://redis.io/)'s [SETNX](http://redis.io/commands/setnx). This code was originally extracted from Discourse into Uncharted Scheduler and later extracted from there into it's own repository.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'distributed-mutex'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install distributed-mutex
23
+
24
+ ## Usage
25
+
26
+ You simply need to wrap your code in a block and give it a key.
27
+
28
+ ```ruby
29
+ require 'distributed-mutex'
30
+
31
+ DistributedMutex.new("custom-key").syncronize do
32
+ puts "Custom Code"
33
+ end
34
+
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/uncharted-code/distributed-mutex.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "distributed_mutex"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'distributed_mutex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "distributed-mutex"
8
+ spec.version = DistributedMutex::VERSION
9
+ spec.authors = ["Nathan Palmer"]
10
+ spec.email = ["nathan@nathanpalmer.com"]
11
+
12
+ spec.summary = %q{Distributed Mutex to allow mutual-exclusion via acquiring locks between multiple processes or machines}
13
+ spec.description = %q{
14
+ This gem allows you to lock specific chunks of code based on a shared key. It's a Mutex, but locks beyond a single machine by utilizing Redis's SETNX.
15
+ }
16
+ spec.homepage = "https://github.com/unchartedcode/distributed-mutex"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # delete this section to allow pushing this gem to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.10"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", '~> 3'
35
+ spec.add_development_dependency "mock_redis", '~> 0'
36
+ spec.add_development_dependency "byebug", '~> 9'
37
+ spec.add_development_dependency "coveralls", '~> 0'
38
+
39
+ spec.add_dependency "redis", '~> 3'
40
+ end
@@ -0,0 +1,49 @@
1
+ # Cross-process locking using Redis.
2
+ class DistributedMutex
3
+ def self.synchronize(key, redis=nil, &blk)
4
+ self.new(key, redis).synchronize(&blk)
5
+ end
6
+
7
+ def initialize(key, redis=nil)
8
+ @key = key
9
+ @redis = redis || $redis
10
+ @mutex = Mutex.new
11
+ end
12
+
13
+ # NOTE wrapped in mutex to maintain its semantics
14
+ def synchronize
15
+ @mutex.lock
16
+ while !try_to_get_lock
17
+ sleep 0.001
18
+ end
19
+
20
+ yield
21
+
22
+ ensure
23
+ @redis.del @key
24
+ @mutex.unlock
25
+ end
26
+
27
+ private
28
+ def try_to_get_lock
29
+ got_lock = false
30
+ if @redis.setnx @key, Time.now.to_i + 60
31
+ @redis.expire @key, 60
32
+ got_lock = true
33
+ else
34
+ begin
35
+ @redis.watch @key
36
+ time = @redis.get @key
37
+ if time && time.to_i < Time.now.to_i
38
+ got_lock = @redis.multi do
39
+ @redis.set @key, Time.now.to_i + 60
40
+ end
41
+ end
42
+ ensure
43
+ @redis.unwatch
44
+ end
45
+ end
46
+
47
+ got_lock
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ class DistributedMutex
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: distributed-mutex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Palmer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-20 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mock_redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3'
111
+ description: "\n This gem allows you to lock specific chunks of code based on a
112
+ shared key. It's a Mutex, but locks beyond a single machine by utilizing Redis's
113
+ SETNX.\n "
114
+ email:
115
+ - nathan@nathanpalmer.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - distributed-mutex.gemspec
130
+ - lib/distributed_mutex.rb
131
+ - lib/distributed_mutex/version.rb
132
+ homepage: https://github.com/unchartedcode/distributed-mutex
133
+ licenses:
134
+ - MIT
135
+ metadata:
136
+ allowed_push_host: https://rubygems.org
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Distributed Mutex to allow mutual-exclusion via acquiring locks between multiple
157
+ processes or machines
158
+ test_files: []