dogtag 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3873a444c41218413a933994fd93c9683588e6f5
4
- data.tar.gz: 561a47c6e821efc9d602d04b985ae1e05e1f1809
3
+ metadata.gz: f8cab26a320943028ef31bc9c2d2f9822a7b39e9
4
+ data.tar.gz: 0dcf654f1701ee9fdab5226334a3b891c2763d02
5
5
  SHA512:
6
- metadata.gz: 0ddf7a7f873c0c3b6a31c80b189cde967aea42569afbec71aeb95ae3361049176d43a525d4270da2ad4dc701fdab2f64b2f9c9aa69da67cbfbeda5f10a79e9db
7
- data.tar.gz: dd72cbd167d3c91f5fd530108a17f1ab4ecf6fe50fd4efca81aaee763bbd5c69a8904d98ab947f2dd7e070a12b828b9bedbec78e543330013a7e544bf319cdb8
6
+ metadata.gz: cb0b80cd108a4d764b2c3687a023d3b98c6c5e94b41589bf225f8248e6faa35d7ec6db4874235f05ceeb3ccaae93d7d9dadb502f4747d36790c2173c74a5147c
7
+ data.tar.gz: e42789e6244c87af765a0d235e874c089a3d33e0c90f230fb7796645a6a8e15b18998f6e7ec87dbb33e314f49bbc61ea3e71da33c9beaff4372b9d27f37a9d75
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ Dogtag
2
+ ======
3
+
4
+ A Redis-powered Ruby client for generating unique IDs with Redis for use in distributed systems. Based heavily off of [Icicle](https://github.com/intenthq/icicle/) and [Twitter Snowflake](https://github.com/intenthq/icicle/)
5
+
6
+ Requirements
7
+ ------------
8
+ - [Ruby](https://www.ruby-lang.org)
9
+ - [Redis](https://redis.io/)
10
+
11
+ Installation
12
+ ------------
13
+
14
+ Simply add `gem 'dogtag'` to your `Gemfile` and run `bundle`
15
+
16
+ Configuration
17
+ -------------
18
+
19
+ To configure the connection to your Redis server simply set the `REDIS_URL` environment variable. By default it will connect to `redis://localhost:6379`.
20
+
21
+ To set the range of logical shard IDs this server should manage, run the command below replacing the number range with a range of numbers, of which none can be shared with another Redis server. If a logical shard ID is shared a separate Redis instance, you may get ID collisions.
22
+
23
+ ```shell
24
+ bundle exec ruby -e 'require "dogtag"; Dogtag.logical_shard_id_range = 0..31'
25
+ ```
26
+
27
+ *Note: The available shard ID numbers are current 0 - 31.*
28
+
29
+ Usage
30
+ -------
31
+
32
+ ```ruby
33
+ data_type = 0
34
+ Dogtag.generate_id data_type
35
+ ```
36
+
37
+ ```ruby
38
+ data_type = 42
39
+ count = 100
40
+ Dogtag.generate_id data_type, count
41
+ ```
42
+
43
+ *Note: The available data type ID numbers are current 0 - 255.*
44
+
45
+ Related Projects
46
+ ----------------
47
+ - __[dogtag-web](https://github.com/zillyinc/dogtag-web)__ - Web API for dogtag
48
+
49
+ TODO
50
+ ----
51
+ - Support multiple Redis servers
data/lib/dogtag.rb CHANGED
@@ -69,6 +69,7 @@ end
69
69
 
70
70
  require 'dogtag/generator'
71
71
  require 'dogtag/id'
72
+ require 'dogtag/lua_script'
72
73
  require 'dogtag/request'
73
74
  require 'dogtag/response'
74
75
  require 'dogtag/timestamp'
@@ -0,0 +1,13 @@
1
+ module Dogtag
2
+ module LuaScript
3
+ LUA_SCRIPT_PATH = 'lua/id-generation.lua.erb'.freeze
4
+
5
+ def self.to_s
6
+ @rendered_script ||= ERB.new(
7
+ File.read(
8
+ File.expand_path "../../#{LUA_SCRIPT_PATH}", File.dirname(__FILE__)
9
+ )
10
+ ).result
11
+ end
12
+ end
13
+ end
@@ -2,7 +2,6 @@ module Dogtag
2
2
  class Request
3
3
  include Dogtag::Mixins::Redis
4
4
 
5
- LUA_SCRIPT_PATH = 'lua/id-generation.lua'.freeze
6
5
  MAX_TRIES = 5
7
6
 
8
7
  def initialize(data_type, count = 1)
@@ -26,14 +25,12 @@ module Dogtag
26
25
 
27
26
  attr_reader :data_type, :count
28
27
 
29
- def lua_script
30
- File.read(
31
- File.expand_path "../../#{LUA_SCRIPT_PATH}", File.dirname(__FILE__)
32
- )
28
+ def lua_script_sha
29
+ @@lua_script_sha ||= redis.script(:load, LuaScript.to_s)
33
30
  end
34
31
 
35
- def lua_args
36
- lua_args = [ MAX_SEQUENCE, data_type, count ]
32
+ def lua_keys
33
+ @lua_keys ||= [ data_type, count ]
37
34
  end
38
35
 
39
36
  # NOTE: If too many requests come in inside of a millisecond the Lua script
@@ -54,7 +51,7 @@ module Dogtag
54
51
  end
55
52
 
56
53
  def redis_response
57
- @redis_response ||= redis.eval(lua_script, keys: lua_args)
54
+ @redis_response ||= redis.evalsha(lua_script_sha, keys: lua_keys)
58
55
  end
59
56
  end
60
57
  end
@@ -1,3 +1,3 @@
1
1
  module Dogtag
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
@@ -1,9 +1,9 @@
1
- local logical_shard_id_range_key = 'dogtag-generator-logical-shard-id-range'
1
+ local logical_shard_id_range_key = '<%= Dogtag::LOGICAL_SHARD_ID_RANGE_KEY %>'
2
2
  local last_logical_shard_id_key = 'dogtag-generator-last-logical-shard-id'
3
+ local max_sequence = <%= Dogtag::MAX_SEQUENCE %>
3
4
 
4
- local max_sequence = tonumber(KEYS[1])
5
- local data_type = tonumber(KEYS[2])
6
- local num_ids = tonumber(KEYS[3])
5
+ local data_type = tonumber(KEYS[1])
6
+ local num_ids = tonumber(KEYS[2])
7
7
 
8
8
  -- Allow one server to acts as multiple shards
9
9
  local logical_shard_id_range = redis.call('SORT', logical_shard_id_range_key)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dogtag
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Crownoble
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-12 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -45,15 +45,17 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - README.md
48
49
  - lib/dogtag.rb
49
50
  - lib/dogtag/generator.rb
50
51
  - lib/dogtag/id.rb
52
+ - lib/dogtag/lua_script.rb
51
53
  - lib/dogtag/mixins/redis.rb
52
54
  - lib/dogtag/request.rb
53
55
  - lib/dogtag/response.rb
54
56
  - lib/dogtag/timestamp.rb
55
57
  - lib/dogtag/version.rb
56
- - lua/id-generation.lua
58
+ - lua/id-generation.lua.erb
57
59
  homepage: https://github.com/zillyinc/dogtag
58
60
  licenses: []
59
61
  metadata: {}