elected 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09c82b9bd8412effb3d88ad09894e6c5d7739868
4
+ data.tar.gz: 44a5123b80fad1ca635b655855ee040855075565
5
+ SHA512:
6
+ metadata.gz: 30fa980526ecc9f8d6280c7c22cd4143a317cb271622f613946a79ff1a71ad00897d013a90712742ec0b67bf7beba1699693ea8e12500333ff1de681bd3588a6
7
+ data.tar.gz: 3e72ef5c02f94b9269bd32f96f320689c565dc81cda4bd51b29b60fd1967f6c01b6429295eab57223e6653aad2560557f93d5bd1b897c45c27bb7cce820be9e1
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
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elected.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Adrian Madrid
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,80 @@
1
+ # Elected - A ruby distributed leader election through Redis locks.
2
+
3
+ > Elect a leader out of many processes and run code only in one of your servers at a time.
4
+ >
5
+ > This library depends heavily on the [redlock](https://github.com/leandromoreira/redlock-rb) ruby gem to obtain distributed locks through Redis.
6
+
7
+ Elected is a Spanglish-fluent gem so expect Spanish and English names all over. Pardon my French!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'elected'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install elected
24
+
25
+ ## Documentation
26
+
27
+ [RubyDoc](http://www.rubydoc.info/gems/elected/frames)
28
+
29
+ ## Usage example
30
+
31
+ ```ruby
32
+ # Config your redis urls or use the default REDIS_URL environment variable by default.
33
+ Elected.redis_urls = ['redis://localhost:6379', 'redis://someotherhost:6379']
34
+ # Create a leader manager with a key and timeout (in milliseconds)
35
+ mgr = Elected::Senado.new 'my_app_name:stage', 30_000
36
+ # Are we the leaders?
37
+ mgr.leader?
38
+ => true
39
+
40
+ # Then in some other app/shell
41
+ mgr = Elected::Senado.new 'my_app_name:stage', 30_000
42
+ mgr.leader?
43
+ => false
44
+ # But if we wait over 30 seconds
45
+ mgr.leader?
46
+ => true
47
+ ```
48
+
49
+ Elected, like Redlock, works seamlessly with [redis sentinel](http://redis.io/topics/sentinel), which is supported in redis 3.2+. It also allows clients to set any other arbitrary options on the Redis connection, e.g. password, driver, and more.
50
+
51
+ ```ruby
52
+ Elected.redis_urls= [ 'redis://localhost:6379', Redis.new(:url => 'redis://someotherhost:6379') ]
53
+ Elected.electorado
54
+ ```
55
+
56
+ ## Run tests
57
+
58
+ Make sure you have at least 1 redis instances up.
59
+
60
+ $ rake rspec
61
+
62
+ ## Disclaimer
63
+
64
+ This code, like Redlock in which is based, implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments. You can see discussion about this approach at [reddit](http://www.reddit.com/r/programming/comments/2nt0nq/distributed_lock_using_redis_implemented_in_ruby/).
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/simple-finance/elected. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
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,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'elected'
5
+
6
+ E = Elected
7
+ S = Elected::Senado
8
+
9
+ require 'pry'
10
+ ARGV.clear
11
+ Pry.start
data/bin/setup ADDED
@@ -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
data/elected.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elected/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elected"
8
+ spec.version = Elected::VERSION
9
+ spec.authors = ['Adrian Madrid']
10
+ spec.email = ['aemadrid@gmail.com']
11
+
12
+ spec.summary = %q{Select and keep a leader through Redis locks.}
13
+ spec.description = %q{Select a leader through a Redis lock and keep it for a time.}
14
+ spec.homepage = "https://github.com/simple-finance/elected"
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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_dependency 'redlock', '0.1.3'
23
+ spec.add_dependency 'pry'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.10'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'timecop'
29
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+
3
+ module Elected
4
+ class Lider
5
+
6
+ attr_reader :info, :validity, :resource, :value, :created, :deadline, :diff
7
+
8
+ def initialize(info, timeout)
9
+ @info = info
10
+ @validity = info[:validity]
11
+ @resource = info[:resource]
12
+ @value = info[:value]
13
+ @created = Time.now
14
+ @deadline = created + (timeout / 1000.0).to_i
15
+ @diff = @deadline - @created
16
+ end
17
+
18
+ def current?
19
+ @deadline > Time.now
20
+ end
21
+
22
+ def to_s(type = :short)
23
+ case type
24
+ when :full
25
+ "#<#{self.class.name} " +
26
+ "resource=#{resource.inspect} " +
27
+ "value=#{value.inspect} " +
28
+ "validity=#{validity.inspect} " +
29
+ "deadline=#{deadline}" +
30
+ ">"
31
+ when :yaml
32
+ to_yaml
33
+ else
34
+ %{#<Elected::Lider|#{resource}:#{value[0, 8]}:#{validity}>}
35
+ end
36
+ end
37
+
38
+ alias :inspect :to_s
39
+
40
+ end
41
+ end
@@ -0,0 +1,72 @@
1
+ require 'thread'
2
+
3
+ module Elected
4
+ class Senado
5
+
6
+ attr_accessor :key, :timeout
7
+
8
+ def initialize(key = nil, timeout = nil)
9
+ @key, @timeout = key, timeout
10
+ end
11
+
12
+ def key
13
+ @key || Elected.key
14
+ end
15
+
16
+ # In ms (milliseconds), defaults to 5 seconds
17
+ def timeout
18
+ @timeout || Elected.timeout
19
+ end
20
+
21
+ def leader
22
+ mutex.synchronize { get_leader || set_leader }
23
+ end
24
+
25
+ def leader?
26
+ !!leader
27
+ end
28
+
29
+ def release
30
+ return false unless @leader
31
+
32
+ Elected.electorado.unlock @leader.info if @leader.current?
33
+ @leader = false
34
+ end
35
+
36
+ def to_s
37
+ %{#<#{self.class.name} key=#{key.inspect} timeout=#{timeout} leader?=#{leader?}>}
38
+ end
39
+
40
+ alias :inspect :to_s
41
+
42
+ private
43
+
44
+ def mutex
45
+ @mutex ||= Mutex.new
46
+ end
47
+
48
+ def get_leader
49
+ return false unless @leader
50
+ return @leader if @leader.current?
51
+
52
+ release
53
+ end
54
+
55
+ def set_leader
56
+ found = Elected.electorado.lock key, timeout
57
+ return false unless found
58
+
59
+ @leader = Lider.new found, timeout
60
+ end
61
+
62
+ end
63
+
64
+ extend self
65
+
66
+ def senado
67
+ @senate ||= Senado.new
68
+ end
69
+
70
+ senado
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module Elected
2
+ VERSION = "0.1.0"
3
+ end
data/lib/elected.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'elected/version'
2
+ require 'redlock'
3
+
4
+ module Elected
5
+
6
+ extend self
7
+
8
+ def redis_urls
9
+ @redis_urls || ENV['REDIS_URL'].split('|')
10
+ end
11
+
12
+ # Split strings by pipes to allow for one ENV['REDIS_URL'] to hold many urls
13
+ def redis_urls=(urls)
14
+ @redis_urls = Array(urls).flatten.
15
+ map { |x| x.is_a?(String) ? x.split('|') : x }.
16
+ flatten
17
+ end
18
+
19
+ def electorado
20
+ @electorado ||= ::Redlock::Client.new redis_urls
21
+ end
22
+
23
+ attr_writer :key, :timeout
24
+
25
+ def key
26
+ @key || 'elected'
27
+ end
28
+
29
+ # In ms (milliseconds), defaults to 5 seconds
30
+ def timeout
31
+ @timeout || 5_000
32
+ end
33
+
34
+ end
35
+
36
+ require 'elected/lider'
37
+ require 'elected/senado'
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elected
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Madrid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redlock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
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
+ description: Select a leader through a Redis lock and keep it for a time.
98
+ email:
99
+ - aemadrid@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - elected.gemspec
115
+ - lib/elected.rb
116
+ - lib/elected/lider.rb
117
+ - lib/elected/senado.rb
118
+ - lib/elected/version.rb
119
+ homepage: https://github.com/simple-finance/elected
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.14
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Select and keep a leader through Redis locks.
143
+ test_files: []