redlock_for_collection 0.1.0

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: f133882c52150f7dba3d271668ba56b88bedc970
4
+ data.tar.gz: 4830986378d7e3e8bb15805dc229f62012f92056
5
+ SHA512:
6
+ metadata.gz: 5212c1c6fa3079d54cc3c6ea9d4c39d9efcf8b63a4c1715146078f47ff02aa8e94fe35971a0a57c81b9934f09019e72da52af97c98c3c3d33943eb4f40ee7c39
7
+ data.tar.gz: 1d42a75e9c9dbd42d80665849c513e8e5c799ee26f64f6f15a3e46820f98e28caf3371cf819a5be4fd9c5a6740e9d38415c484c037fd2028a831392d313b318d
data/.codeclimate.yml ADDED
@@ -0,0 +1,17 @@
1
+ engines:
2
+ duplication:
3
+ enabled: true
4
+ config:
5
+ languages:
6
+ - ruby
7
+ fixme:
8
+ enabled: true
9
+ rubocop:
10
+ enabled: true
11
+ reek:
12
+ enabled: true
13
+ ratings:
14
+ paths:
15
+ - "**.rb"
16
+ exclude_paths:
17
+ - test/
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/.reek ADDED
@@ -0,0 +1,12 @@
1
+ ---
2
+ IrresponsibleModule:
3
+ enabled: false
4
+
5
+ TooManyStatements:
6
+ max_statements: 15
7
+
8
+ ### Excluding directories
9
+
10
+ # Directories below will not be scanned at all
11
+ exclude_paths:
12
+ - test
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ cache: bundler
3
+ services:
4
+ - redis-server
5
+ addons:
6
+ code_climate:
7
+ repo_token: b035b032138b99f091cb122d4e7c75c045f40e9aca3a338399712156e51f2f9b
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redlock_manager.gemspec
4
+ gemspec
5
+
6
+
7
+ gem 'connection_pool'
8
+ gem 'redlock'
9
+
10
+ group :development do
11
+ gem 'reek'
12
+ gem 'rubocop'
13
+ end
14
+
15
+ group :test do
16
+ gem 'minitest'
17
+ gem 'codeclimate-test-reporter'
18
+ end
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ [![Build Status](https://travis-ci.org/CoolElvis/redlock_for_collection.svg?branch=master)](https://travis-ci.org/CoolElvis/redlock_for_collection)
2
+ [![Code Climate](https://codeclimate.com/github/CoolElvis/redlock_for_collection/badges/gpa.svg)](https://codeclimate.com/github/CoolElvis/redlock_for_collection)
3
+ [![Test Coverage](https://codeclimate.com/github/CoolElvis/redlock_for_collection/badges/coverage.svg)](https://codeclimate.com/github/CoolElvis/redlock_for_collection/coverage)
4
+ [![Issue Count](https://codeclimate.com/github/CoolElvis/redlock_for_collection/badges/issue_count.svg)](https://codeclimate.com/github/CoolElvis/redlock_for_collection)
5
+ # RedlockForCollection
6
+
7
+ This is just a [Redlock](http://redis.io/topics/distlock) wrapper for collection of objects.
8
+
9
+ Also it used a connection pool for restrict the redis connections.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'redlock_for_collection'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install redlock_for_collection
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ collection_manager = RedlockForCollection::Manager.new
31
+ options = { key_method: :to_s, , key_prefix: 'pref', ttl: 20_000, min_validity: 10_00}
32
+
33
+ collection_manager.with_lock(objects, options: options) do |locked_objects, unlocked_objects|
34
+ ... do some things with locked_objects
35
+ ... do some things with unlocked_objects
36
+ end
37
+ ```
38
+ #### options
39
+ + `:ttl` time to live of lock.
40
+ + `:key_method` invoked on `object` of `objects_collection` to generate shared key.
41
+ + `:key_prefix` prepended to the shared key
42
+ + if lock validity < `:min_validity` then lock is treated as expired
43
+ + `locked_objects` is successfully locked objects of collections
44
+ + `unlocked_objects` is unsuccessfully locked objects of collections for any reasons
45
+
46
+ ### Configuration
47
+
48
+ ```ruby
49
+ collection_manager = RedlockForCollection::Manager.new
50
+
51
+ collection_manager.configure do |configuration|
52
+ config.pool_size = 2
53
+ config.redis_urls = ['redis://localhost:6379']
54
+ config.retry_delay = 2
55
+ config.retry_count = 2
56
+ end
57
+ ```
58
+
59
+ Also you can provide a pool
60
+
61
+ ```ruby
62
+ manager = RedlockForCollection::Manager.new
63
+ redlock_pool = ConnectionPool.new { Redlock::Client.new([redis_urls]) }
64
+
65
+ manager.configure do |configuration|
66
+ config.pool = redlock_pool
67
+ end
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ 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).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CoolElvis/redlock_for_collection.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/*.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redlock_for_collection"
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,72 @@
1
+ module RedlockForCollection
2
+ class Collection
3
+ DEFAULT_TTL = 10_000
4
+ DEFAULT_MIN_VALIDITY = 5_000
5
+ DEFAULT_KEY_METHOD = 'key'.freeze
6
+ DEFAULT_KEY_PREFIX = 'prefix'.freeze
7
+
8
+ attr_reader :objects, :options, :pool
9
+
10
+ def initialize(objects, options, pool)
11
+ @objects = objects
12
+ @options = options
13
+ @pool = pool
14
+
15
+ init_options
16
+ end
17
+
18
+
19
+ def lock(&block)
20
+ locked_objects = []
21
+ unlocked_objects = []
22
+
23
+ locks_info = []
24
+ expired_locks_info = []
25
+
26
+ @objects.each do |object|
27
+ @pool.with do |redlock|
28
+ lock_info = redlock.lock(build_key_for(object), @options[:ttl])
29
+
30
+ # Check if ttl is enough
31
+ if lock_info && (lock_info[:validity] > (@options[:min_validity]))
32
+ locked_objects << object
33
+ locks_info << lock_info
34
+ else
35
+ expired_locks_info << lock_info if lock_info
36
+ unlocked_objects << object
37
+ end
38
+ end
39
+ end
40
+
41
+ begin
42
+ block.yield(locked_objects, unlocked_objects)
43
+ ensure
44
+ # Release the all acquired locks
45
+ unlock(expired_locks_info)
46
+ unlock(locks_info)
47
+ end
48
+ end
49
+
50
+
51
+ protected
52
+
53
+ def unlock(locks_info)
54
+ @pool.with do |redlock|
55
+ locks_info.each { |lock_info| redlock.unlock(lock_info) }
56
+ end
57
+ end
58
+
59
+ def init_options
60
+ @options[:key_prefix] ||= DEFAULT_KEY_PREFIX
61
+ @options[:key_method] ||= DEFAULT_KEY_METHOD
62
+ @options[:min_validity] ||= DEFAULT_MIN_VALIDITY
63
+ @options[:ttl] ||= DEFAULT_TTL
64
+ end
65
+
66
+ def build_key_for(object)
67
+ (@options[:key_prefix]) + object.send(@options[:key_method]).to_s
68
+ end
69
+
70
+ end
71
+ end
72
+
@@ -0,0 +1,33 @@
1
+ require 'connection_pool'
2
+ require 'redlock'
3
+
4
+ module RedlockForCollection
5
+ class Configuration
6
+ attr_accessor :pool_size,
7
+ :pool_timeout,
8
+ :redis_urls,
9
+ :retry_delay,
10
+ :retry_count,
11
+ :pool
12
+
13
+ def initialize
14
+ @pool_size = 5
15
+ @pool_timeout = 5
16
+ @redis_urls = ['redis://localhost:6379']
17
+ @retry_delay = 5
18
+ @retry_count = 5
19
+ @pool = nil
20
+ end
21
+
22
+
23
+ def pool
24
+ @pool|| begin
25
+ @pool = ConnectionPool.new(size: @pool_size, timeout: @pool_timeout) do
26
+ Redlock::Client.new(@redis_urls, retry_delay: @retry_delay, retry_count: @retry_count)
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+
@@ -0,0 +1,3 @@
1
+ module RedlockForCollection
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'redlock_for_collection/version'
2
+ require_relative 'redlock_for_collection/configuration'
3
+ require_relative 'redlock_for_collection/collection'
4
+
5
+ module RedlockForCollection
6
+ class Manager
7
+ attr_reader :configuration
8
+
9
+ def initialize
10
+ @configuration = Configuration.new
11
+
12
+ yield @configuration if block_given?
13
+ end
14
+
15
+ # @example
16
+ # configure do |configuration|
17
+ # config.pool_size = 2
18
+ # config.redis_urls = ['redis://localhost:6379']
19
+ # config.retry_delay = 2
20
+ # config.retry_count = 2
21
+ # end
22
+ #
23
+ # redlock_pool = ConnectionPool.new { Redlock::Client.new([redis_urls]) }
24
+ #
25
+ # configure do |configuration|
26
+ # config.pool = redlock_pool
27
+ # end
28
+ #
29
+ # @yield [Configuration]
30
+ def configure(&block)
31
+ block.yield @configuration
32
+ end
33
+
34
+
35
+ # @param objects_collection [#each]
36
+ # @param options [Hash], consist of `:key_prefix`, `:key_method`, `:min_validity`, `:ttl`
37
+ # `:key_method` invoked on `object` of `objects_collection` to generate shared key.
38
+ # `:key_prefix` prepended to the shared key
39
+ # if lock validity < `:min_validity` then lock is treated as expired
40
+ # @yield [locked_objects, unlocked_objects]
41
+ # `locked_objects` is successfully locked objects of collections
42
+ # `unlocked_objects` is unsuccessfully locked objects of collections for any reasons
43
+ def with_lock(objects_collection, options: {}, &block)
44
+ collection = Collection.new(objects_collection, options, @configuration.pool)
45
+
46
+ collection.lock(&block)
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redlock_for_collection/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'redlock_for_collection'
8
+ spec.version = RedlockForCollection::VERSION
9
+ spec.authors = ['CoolElvis']
10
+ spec.email = ['elvisplus2@gmail.com']
11
+
12
+ spec.summary = %q{This is just a Redlock wrapper for collection of objects.}
13
+ spec.description = %q{This is just a Redlock wrapper for collection of objects. Also it used a connection pool for restrict the redis connections.}
14
+ spec.homepage = 'https://github.com/CoolElvis/redlock_for_collection'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'connection_pool', '~> 2.2.0'
24
+ spec.add_development_dependency 'redlock', '~> 0.1.1'
25
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redlock_for_collection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - CoolElvis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-11 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: connection_pool
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: redlock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.1
69
+ description: This is just a Redlock wrapper for collection of objects. Also it used
70
+ a connection pool for restrict the redis connections.
71
+ email:
72
+ - elvisplus2@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".codeclimate.yml"
78
+ - ".gitignore"
79
+ - ".reek"
80
+ - ".ruby-version"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/redlock_for_collection.rb
88
+ - lib/redlock_for_collection/collection.rb
89
+ - lib/redlock_for_collection/configuration.rb
90
+ - lib/redlock_for_collection/version.rb
91
+ - redlock_for_collection.gemspec
92
+ homepage: https://github.com/CoolElvis/redlock_for_collection
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: This is just a Redlock wrapper for collection of objects.
115
+ test_files: []
116
+ has_rdoc: