diplo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 858413b493705cbe6e1d61d91eaad60a9ae63727
4
+ data.tar.gz: 6dc3dc4b2d3729429c2697c0d0907d016169bd96
5
+ SHA512:
6
+ metadata.gz: 50faa28edbdd5314bad19dc45ba730151f1ae71cdea9147779aa2c695217bf4fc91358b8971ac787ebdec6fd86522870523926dc374fe99f80cfb7d97980067c
7
+ data.tar.gz: 9a7d4b4252eb7e4c408065018ec2f566379a2d20a6851f800a9d2433286da536f31f378e7139f83a31248b1bc89e1da9bcf7bcc2eacee1e81ca1c7eafa23c4b2
data/.env ADDED
@@ -0,0 +1,8 @@
1
+ export DIPLO_LOCK_DRIFT_FACTOR=0.01
2
+ export DIPLO_LOCK_RETRY_COUNT=3
3
+ export DIPLO_LOCK_RETRY_DELAY=200
4
+ export DIPLO_LOCK_TOKEN_BYTES=20
5
+ export DIPLO_LOCK_TTL=200
6
+ export DIPLO_LOCK_QUORUM=1
7
+
8
+ export DIPLO_REDIS=redis://127.0.0.1:6379
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Victor Rodrigues
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,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'diplo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'diplo'
8
+ spec.version = Diplo::VERSION
9
+ spec.authors = ['Victor Rodrigues']
10
+ spec.email = ['victorc.rodrigues@gmail.com']
11
+ spec.summary = 'Make distributed Ruby applications'
12
+ spec.homepage = 'https://github.com/rodrigues/diplo'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'pry'
24
+
25
+ spec.add_dependency 'redis'
26
+ spec.add_dependency 'hiredis'
27
+ spec.add_dependency 'dotenv'
28
+ end
@@ -0,0 +1,9 @@
1
+ require 'redis'
2
+ require 'hiredis'
3
+ require 'dotenv'
4
+ DotEnv.load
5
+
6
+ require 'diplo/version'
7
+
8
+ module Diplo
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'dotenv'
2
+
3
+ Dotenv.load
4
+
5
+ module Diplo
6
+ module Env
7
+ module_function
8
+
9
+ def fetch(variable)
10
+ ENV["DIPLO_#{variable.to_s.upcase}"] || begin
11
+ yield if block_given?
12
+ end
13
+ end
14
+
15
+ def fetch_int(variable, error = nil, &block)
16
+ fetch_converted variable, :Integer, error, &block
17
+ end
18
+
19
+ def fetch_float(variable, error = nil, &block)
20
+ fetch_converted variable, :Float, error, &block
21
+ end
22
+
23
+ def fetch_converted(variable, conversion, error = nil)
24
+ value = fetch(variable)
25
+ return yield if value.nil? && block_given?
26
+
27
+ send conversion, value
28
+ rescue
29
+ error ? raise(error) : raise
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,125 @@
1
+ require 'redis'
2
+
3
+ require 'diplo/env'
4
+
5
+ module Diplo
6
+ module Lock
7
+ module_function
8
+
9
+ def create(resource, ttl = nil)
10
+ ttl ||= Env.fetch_int(:lock_ttl)
11
+ token = create_lock_token
12
+ lock = OpenStruct.new(resource: resource, token: token)
13
+
14
+ retry_count.times do
15
+ locked_nodes, start_time = 0, Time.now
16
+
17
+ servers.each do |server|
18
+ locked_nodes += 1 if lock_server(server, resource, token, ttl)
19
+ end
20
+
21
+ validity = validity_time(start_time, ttl)
22
+
23
+ if locked_nodes >= quorum && validity > 0
24
+ lock.validity = validity
25
+ return lock
26
+ else
27
+ remove lock
28
+ end
29
+
30
+ delay
31
+ end
32
+
33
+ false
34
+ end
35
+
36
+ def remove(lock)
37
+ servers.each do |server|
38
+ unlock_server server, lock.resource, lock.token
39
+ end
40
+ end
41
+
42
+ def servers
43
+ @servers || begin
44
+ config = Env.fetch(:redis)
45
+ uris = config.gsub(' ', '').split(',')
46
+ uris.map { |u| ::Redis.new url: u }
47
+ end
48
+ end
49
+
50
+ def lock_server(redis, resource, token, ttl)
51
+ redis.client.call [:set, resource, token, :nx, :px, ttl]
52
+ rescue
53
+ false
54
+ end
55
+
56
+ def unlock_server(redis, resource, token)
57
+ @unlock_script ||= <<-LUA
58
+ if redis.call("get", KEYS[1]) == ARGV[1] then
59
+ return redis.call("del", KEYS[1])
60
+ else
61
+ return 0
62
+ end
63
+ LUA
64
+
65
+ redis.client.call [:eval, @unlock_script, 1, resource, token]
66
+ rescue
67
+ # Nothing to do, unlocking is just a best-effort attempt.
68
+ end
69
+
70
+ def retry_count
71
+ @retry_count || Env.fetch_int(:lock_retry_count)
72
+ end
73
+
74
+ def retry_count=(count)
75
+ @retry_count = Integer(count)
76
+ end
77
+
78
+ def retry_delay
79
+ @retry_delay || Env.fetch_int(:lock_retry_delay)
80
+ end
81
+
82
+ def retry_delay=(miliseconds)
83
+ @retry_delay = Integer(miliseconds)
84
+ end
85
+
86
+ def drift_factor
87
+ @drift_factor || Env.fetch_float(:lock_drift_factor)
88
+ end
89
+
90
+ def drift_factor=(factor)
91
+ @drift_factor = Float(factor)
92
+ end
93
+
94
+ def lock_token_bytes
95
+ @lock_token_bytes || Env.fetch_int(:lock_token_bytes)
96
+ end
97
+
98
+ def lock_token_bytes=(count)
99
+ @lock_token_bytes = Integer(count)
100
+ end
101
+
102
+ def quorum
103
+ @quorum || Env.fetch_int(:lock_quorum)
104
+ end
105
+
106
+ def quorum=(nodes)
107
+ @quorum = Integer(nodes)
108
+ end
109
+
110
+ def create_lock_token
111
+ SecureRandom.hex lock_token_bytes
112
+ end
113
+
114
+ def delay
115
+ sleep rand(retry_delay).to_f / 1000
116
+ end
117
+
118
+ def validity_time(start_time, ttl)
119
+ diff_time = Time.now - start_time
120
+ drift = (ttl* drift_factor).to_i + 2
121
+ ttl - (diff_time * 1000).to_i - drift
122
+ end
123
+
124
+ end
125
+ end
@@ -0,0 +1,3 @@
1
+ module Diplo
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diplo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Rodrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-14 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: hiredis
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: dotenv
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - victorc.rodrigues@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".env"
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - Rakefile
123
+ - diplo.gemspec
124
+ - lib/diplo.rb
125
+ - lib/diplo/env.rb
126
+ - lib/diplo/lock.rb
127
+ - lib/diplo/version.rb
128
+ homepage: https://github.com/rodrigues/diplo
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.2.2
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Make distributed Ruby applications
152
+ test_files: []
153
+ has_rdoc: