barrage 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +13 -0
- data/barrage.gemspec +29 -0
- data/lib/barrage.rb +52 -0
- data/lib/barrage/generators.rb +4 -0
- data/lib/barrage/generators/base.rb +23 -0
- data/lib/barrage/generators/msec.rb +17 -0
- data/lib/barrage/generators/pid.rb +13 -0
- data/lib/barrage/generators/redis_worker_id.rb +88 -0
- data/lib/barrage/generators/sequence.rb +20 -0
- data/lib/barrage/generators/timestamp.rb +8 -0
- data/lib/barrage/version.rb +3 -0
- data/spec/barrage_spec.rb +7 -0
- data/spec/spec_helper.rb +7 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3642f34f25aae142789e33a2cf4c1d5a01648212
|
4
|
+
data.tar.gz: 3e71ac939f14b17c6c9d1d1407efda603392ea77
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b6f8a47d16b21d6160a79b007e45622564953fc680a5f98d4acc9ca437af42fbb42d2f8c545409968c6b72b26002b9cc210646cea2068c1ff797dff3d4c1d1f
|
7
|
+
data.tar.gz: 5f182c40631fe62015e9365eda727011a24943da8e1280a6bf7dc19aac1bcd959f3c5e2e79232a76d2874cd06f6714c734304abdf926ee083bcc085266f99689
|
data/.gitignore
ADDED
@@ -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/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Drecom Co., Ltd.
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Barrage
|
2
|
+
|
3
|
+
Distributed ID generator(like twitter/snowflake)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'barrage'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install barrage
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Example
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# 39bit: msec (17.4 years from start_at)
|
25
|
+
# 16bit: worker_id
|
26
|
+
# 9bit: sequence
|
27
|
+
require 'barrage'
|
28
|
+
|
29
|
+
barrage = Barrage.new(
|
30
|
+
"generators" => [
|
31
|
+
{"name" => "msec", "length" => 39, "start_at" => 1396278000000},
|
32
|
+
{"name" => "redis_worker_id", "length" => 16, "ttl" => 300},
|
33
|
+
{"name" => "sequence", "length" => 9}
|
34
|
+
]
|
35
|
+
)
|
36
|
+
barrage.next
|
37
|
+
# => Generated ID
|
38
|
+
```
|
39
|
+
|
40
|
+
### Generators
|
41
|
+
|
42
|
+
#### msec
|
43
|
+
#### redis_worker_id
|
44
|
+
#### sequence
|
45
|
+
|
46
|
+
### Creating your own generator
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
module Barrage::Generators
|
50
|
+
class YourOwnGenerator < Base
|
51
|
+
def generate
|
52
|
+
# generated code
|
53
|
+
end
|
54
|
+
|
55
|
+
def your_option_value
|
56
|
+
options["your_option_value"]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
barrage = Barrage.new("generators" => [{"name"=>"your_own", "length" => 8, "your_option_value"=>"xxx"}])
|
62
|
+
```
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it ( https://github.com/drecom/barrage/fork )
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/barrage.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 'barrage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "barrage"
|
8
|
+
spec.version = Barrage::VERSION
|
9
|
+
spec.authors = ["gussan"]
|
10
|
+
spec.email = ["egussan@gmail.com"]
|
11
|
+
spec.summary = %q{Distributed id generator}
|
12
|
+
spec.description = %q{Distributed id generator}
|
13
|
+
spec.homepage = "http://github.com/drecom/barrage"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", "3.0.0.beta2"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
spec.add_development_dependency "guard-rspec"
|
28
|
+
spec.add_development_dependency "redis"
|
29
|
+
end
|
data/lib/barrage.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "barrage/version"
|
2
|
+
require "active_support/core_ext/string/inflections"
|
3
|
+
|
4
|
+
class Barrage
|
5
|
+
class InvalidOption < StandardError; end
|
6
|
+
attr_reader :generators
|
7
|
+
|
8
|
+
#
|
9
|
+
# generators:
|
10
|
+
# - name: msec
|
11
|
+
# length: 40
|
12
|
+
# start_at: 1396278000000 # 2014-04-01 00:00:00 +0900 (msec)
|
13
|
+
# - name: pid
|
14
|
+
# length: 16
|
15
|
+
# - name: sequence
|
16
|
+
# length: 8
|
17
|
+
|
18
|
+
def initialize(options = {})
|
19
|
+
@options = options
|
20
|
+
@lock = Mutex.new
|
21
|
+
@generators = @options["generators"].map do |h|
|
22
|
+
generator_name = h["name"]
|
23
|
+
require "barrage/generators/#{generator_name}"
|
24
|
+
"Barrage::Generators::#{generator_name.classify}".constantize.new(h)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate
|
29
|
+
@lock.synchronize {
|
30
|
+
shift_size = length
|
31
|
+
@generators.inject(0) do |result, generator|
|
32
|
+
shift_size = shift_size - generator.length
|
33
|
+
result += generator.generate << shift_size
|
34
|
+
end
|
35
|
+
}
|
36
|
+
end
|
37
|
+
alias_method :next, :generate
|
38
|
+
|
39
|
+
def current
|
40
|
+
@lock.synchronize {
|
41
|
+
shift_size = length
|
42
|
+
@generators.inject(0) do |result, generator|
|
43
|
+
shift_size = shift_size - generator.length
|
44
|
+
result += generator.current << shift_size
|
45
|
+
end
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def length
|
50
|
+
@generators.inject(0) {|sum, g| sum += g.length }
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Barrage
|
2
|
+
module Generators
|
3
|
+
class Base
|
4
|
+
attr_reader :options
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def length
|
11
|
+
options["length"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate
|
15
|
+
raise NotImplemented, "Please Override"
|
16
|
+
end
|
17
|
+
|
18
|
+
def current
|
19
|
+
raise NotImplemented, "Please Override"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'barrage/generators/base'
|
2
|
+
|
3
|
+
class Barrage
|
4
|
+
module Generators
|
5
|
+
class Msec < Base
|
6
|
+
def generate
|
7
|
+
((Time.now.to_f * 1000).round - start_at) & (2 ** length - 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method :current, :generate
|
11
|
+
|
12
|
+
def start_at
|
13
|
+
options["start_at"]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'barrage/generators/base'
|
3
|
+
|
4
|
+
class Barrage
|
5
|
+
module Generators
|
6
|
+
class RedisWorkerId < Base
|
7
|
+
RACE_CONDITION_TTL = 30
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@worker_id = nil
|
11
|
+
@worker_ttl = 0
|
12
|
+
super
|
13
|
+
@data = []
|
14
|
+
@finalizer_proc = Finalizer.new(@data)
|
15
|
+
ObjectSpace.define_finalizer(self, @finalizer_proc)
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate
|
19
|
+
now = Time.now.to_i
|
20
|
+
if @worker_ttl - now <= 0
|
21
|
+
@data[1] = @worker_id = renew_worker_id
|
22
|
+
# check redis when passed half of real ttl
|
23
|
+
@worker_ttl = now + ttl / 2
|
24
|
+
end
|
25
|
+
@worker_id
|
26
|
+
end
|
27
|
+
alias_method :current, :generate
|
28
|
+
|
29
|
+
def redis
|
30
|
+
@redis ||= @data[0] = Redis.new(options["redis"] || {})
|
31
|
+
end
|
32
|
+
|
33
|
+
def ttl
|
34
|
+
options["ttl"]
|
35
|
+
end
|
36
|
+
|
37
|
+
class Finalizer
|
38
|
+
def initialize(data)
|
39
|
+
@pid = $$
|
40
|
+
@data = data
|
41
|
+
end
|
42
|
+
|
43
|
+
def call(*args)
|
44
|
+
return if @pid != $$
|
45
|
+
redis, worker_id = *@data
|
46
|
+
|
47
|
+
if redis.is_a?(Redis) and redis.connected?
|
48
|
+
redis.del("barrage:worker:#{worker_id}")
|
49
|
+
redis.client.disconnect
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def renew_worker_id
|
57
|
+
new_worker_id = redis.evalsha(
|
58
|
+
script_sha,
|
59
|
+
argv: [2 ** length, rand(2 ** length), @worker_id, ttl, RACE_CONDITION_TTL]
|
60
|
+
).to_i
|
61
|
+
new_worker_id
|
62
|
+
end
|
63
|
+
|
64
|
+
def script_sha
|
65
|
+
@script_sha ||=
|
66
|
+
redis.script(:load, <<-EOF.gsub(/^ {12}/, ''))
|
67
|
+
local max_value = ARGV[1]
|
68
|
+
local new_worker_id = ARGV[2]
|
69
|
+
local old_worker_id = ARGV[3]
|
70
|
+
local ttl = ARGV[4]
|
71
|
+
local race_condition_ttl = ARGV[5]
|
72
|
+
|
73
|
+
if type(old_worker_id) == "string" and string.len(old_worker_id) > 0 and redis.call('EXISTS', "barrage:worker:" .. old_worker_id) == 1 then
|
74
|
+
redis.call("EXPIRE", "barrage:worker:" .. old_worker_id, ttl + race_condition_ttl)
|
75
|
+
new_worker_id = old_worker_id
|
76
|
+
else
|
77
|
+
while redis.call("SETNX", "barrage:worker:" .. new_worker_id, 1) == 0
|
78
|
+
do
|
79
|
+
new_worker_id = (new_worker_id + 1) % max_value
|
80
|
+
end
|
81
|
+
redis.call("EXPIRE", "barrage:worker:" .. new_worker_id, ttl + race_condition_ttl)
|
82
|
+
end
|
83
|
+
return new_worker_id
|
84
|
+
EOF
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'barrage/generators/base'
|
2
|
+
|
3
|
+
class Barrage
|
4
|
+
module Generators
|
5
|
+
class Sequence < Base
|
6
|
+
def initialize(options)
|
7
|
+
@sequence = 0
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate
|
12
|
+
@sequence = (@sequence + 1) & (2 ** length - 1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def current
|
16
|
+
@sequence
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barrage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gussan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0.beta2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0.beta2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
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: guard-rspec
|
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: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Distributed id generator
|
112
|
+
email:
|
113
|
+
- egussan@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CHANGELOG.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- barrage.gemspec
|
127
|
+
- lib/barrage.rb
|
128
|
+
- lib/barrage/generators.rb
|
129
|
+
- lib/barrage/generators/base.rb
|
130
|
+
- lib/barrage/generators/msec.rb
|
131
|
+
- lib/barrage/generators/pid.rb
|
132
|
+
- lib/barrage/generators/redis_worker_id.rb
|
133
|
+
- lib/barrage/generators/sequence.rb
|
134
|
+
- lib/barrage/generators/timestamp.rb
|
135
|
+
- lib/barrage/version.rb
|
136
|
+
- spec/barrage_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
homepage: http://github.com/drecom/barrage
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.2.2
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Distributed id generator
|
162
|
+
test_files:
|
163
|
+
- spec/barrage_spec.rb
|
164
|
+
- spec/spec_helper.rb
|