renoir 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: bdd9e26d7c799ce66755d415eb1fcb5f54c0f3c7
4
+ data.tar.gz: bd3eedd262e8bb76d81b2808346c59221229a667
5
+ SHA512:
6
+ metadata.gz: 090813818dcccb2e6606629c2296d4104057a12548d7bb45bc909dec159153acde2811e5c43745ec6f5677e08dcff2f313af4e5ff0ae67d75b690f7bc0216d36
7
+ data.tar.gz: 4c230945ee38e7db2e6ef546c71467805aedfb911ba89471668c2aa622e040176e1e45a64ed38f96b840514637075a5d236085a6231a93f18e384a4d543a8e58
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install:
6
+ - gem install bundler -v 1.13.6
7
+ - git clone https://github.com/antirez/redis $TRAVIS_BUILD_DIR/redis && cd $TRAVIS_BUILD_DIR/redis && make
8
+ before_script:
9
+ - cd $TRAVIS_BUILD_DIR/redis/utils/create-cluster && ./create-cluster start && yes yes | ./create-cluster create
10
+ - cd $TRAVIS_BUILD_DIR
11
+ after_script:
12
+ - cd $TRAVIS_BUILD_DIR/redis/utils/create-cluster && ./create-cluster stop && ./create-cluster clean
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at saito.die@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in renoir.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Salvatore Sanfilippo <antirez@gmail.com>
4
+ Copyright (c) 2016 Hiroshi Saito
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Renoir
2
+
3
+ [![Build Status](https://travis-ci.org/saidie/renoir.svg?branch=master)](https://travis-ci.org/saidie/renoir)
4
+
5
+ A production ready Redis cluster client for Ruby.
6
+
7
+ Renoir provides compatible interface with [redis-rb](https://github.com/redis/redis-rb/).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'renoir'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install renoir
24
+
25
+ ## Usage
26
+
27
+ You can request to a Redis cluster by instantiating `Renoir::Client` with `cluster_nodes` option.
28
+
29
+ ```ruby
30
+ require 'renoir'
31
+
32
+ rc = Renoir::Client.new(cluster_nodes: ['127.0.0.1:30001'])
33
+
34
+ puts rc.set('hoge', 123) #=> OK
35
+ puts rc.set('fuga', 456) #=> OK
36
+ puts rc.get('hoge') #=> 123
37
+ puts rc.get('fuga') #=> 456
38
+ ```
39
+
40
+ Options for [redis-rb](https://github.com/redis/redis-rb/) could also be passed as an argument:
41
+
42
+ ```ruby
43
+ rc = Renoir::Client.new(
44
+ cluster_nodes: ['127.0.0.1:30001'],
45
+
46
+ # redis-rb options
47
+ timeout: 100,
48
+ password: 'password'
49
+ )
50
+ ```
51
+
52
+ ### Dispatch command to nodes directly
53
+
54
+ Renoir dispatches a command only if a slot is determined by the command. This also includes no-keys commands like `KEYS`, `BGSAVE` and so on.
55
+
56
+ If you would like to dispatch such commands, `Renoir::Client#each_node` could be used:
57
+
58
+ ```ruby
59
+ keys = []
60
+ rc.each_node do |node|
61
+ keys += node.keys('test_*')
62
+ end
63
+ p keys
64
+ ```
65
+
66
+ ## Configuration
67
+
68
+ Following options could be passed to `Renoir::Client#new`:
69
+
70
+ ### `cluster_nodes` (required)
71
+
72
+ `Array` of cluster node locations. At least one location must be specified.
73
+
74
+ A location could be `String`, `Array` or `Hash`:
75
+
76
+ ```ruby
77
+ cluster_nodes: [
78
+ '127.0.0.1:30001',
79
+ ['127.0.0.1', 30002],
80
+ { host: 127.0.0.1, port: 30003 }
81
+ ]
82
+ ```
83
+
84
+ ### `max_redirection`
85
+
86
+ Max number of redirections. Defaults to `10`.
87
+
88
+ ### `max_connection_error`
89
+
90
+ Max number of acceptable connection errors. Defaults to `5`.
91
+
92
+ ### `connect_retry_interval`, `connect_retry_random_factor`
93
+
94
+ Options for adjusting an interval of retry that a client tries to reconnect to same node when connection error is occurred.
95
+ Defaults to `0.001` (sec) and `0.1` respectively.
96
+
97
+ A retry interval is proportional to a random value sampled from `[connect_retry_interval - connect_retry_random_factor, connect_retry_interval + connect_retry_random_factor]`.
98
+
99
+ ### `connection_adapter`
100
+
101
+ Adapter name of internal connection that client uses to connect to Redis node. Defaults to `:redis`.
102
+
103
+ Available adapter is `:redis` so far.
104
+
105
+ ## Development
106
+
107
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
108
+
109
+ 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).
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/saidie/renoir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
114
+
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
119
+
120
+ The original code is from [antirez/redis-rb-cluster](https://github.com/antirez/redis-rb-cluster).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
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 "renoir"
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
data/lib/renoir.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "renoir/version"
2
+ require "renoir/client"
3
+
4
+ module Renoir
5
+ class BaseError < RuntimeError
6
+ end
7
+
8
+ class RedirectionError < BaseError
9
+ end
10
+ end
@@ -0,0 +1,183 @@
1
+ require 'thread'
2
+ require "renoir/cluster_info"
3
+ require "renoir/connection_adapters"
4
+ require "renoir/crc16"
5
+
6
+ module Renoir
7
+ class Client
8
+ REDIS_CLUSTER_HASH_SLOTS = 16_384
9
+
10
+ DEFAULT_OPTIONS = {
11
+ cluster_nodes: [
12
+ ["127.0.0.1", 6379]
13
+ ],
14
+ max_redirection: 10,
15
+ max_connection_error: 5,
16
+ connect_retry_random_factor: 0.1,
17
+ connect_retry_interval: 0.001, # 1 ms
18
+ connection_adapter: :redis,
19
+ }.freeze
20
+
21
+ def initialize(options)
22
+ @connections = {}
23
+ @cluster_info = ClusterInfo.new
24
+ @refresh_slots = true
25
+
26
+ options = options.map { |k, v| [k.to_sym, v] }.to_h
27
+ @options = DEFAULT_OPTIONS.merge(options)
28
+ @logger = @options[:logger]
29
+ @adapter_class = ConnectionAdapters.const_get(@options[:connection_adapter].to_s.capitalize)
30
+
31
+ cluster_nodes = @options.delete(:cluster_nodes)
32
+ fail "the cluster_nodes option must contain at least one node" if cluster_nodes.empty?
33
+ cluster_nodes.each do |node|
34
+ host, port = case node
35
+ when Array
36
+ node
37
+ when Hash
38
+ [node[:host], node[:port]]
39
+ when String
40
+ node.split(":")
41
+ else
42
+ fail "invalid entry in cluster_nodes option: #{node}"
43
+ end
44
+ port ||= 6379
45
+ @cluster_info.add_node(host, port.to_i)
46
+ end
47
+
48
+ @connections_mutex = Mutex.new
49
+ @refresh_slots_mutex = Mutex.new
50
+ end
51
+
52
+ def call(*command, &block)
53
+ keys = @adapter_class.get_keys_from_command(command)
54
+ slots = keys.map { |key| key_slot(key) }.uniq
55
+ fail "No way to dispatch this command to Redis Cluster." if slots.size != 1
56
+ slot = slots.first
57
+
58
+ refresh = @refresh_slots_mutex.synchronize do
59
+ refresh = @refresh_slots
60
+ @refresh_slots = false
61
+ refresh
62
+ end
63
+ refresh_slots if refresh
64
+
65
+ call_with_redirection(slot, command, &block)
66
+ end
67
+
68
+ def close
69
+ while entry = @connections.shift
70
+ entry[1].close
71
+ end
72
+ end
73
+
74
+ def each_node
75
+ return enum_for(:each_node) unless block_given?
76
+
77
+ @cluster_info.nodes.each do |node|
78
+ fetch_connection(node).with_raw_connection do |conn|
79
+ yield conn
80
+ end
81
+ end
82
+ end
83
+
84
+ def method_missing(command, *args, &block)
85
+ call(command, *args, &block)
86
+ end
87
+
88
+ private
89
+
90
+ def key_slot(key)
91
+ s = key.index("{")
92
+ if s
93
+ e = key.index("}", s + 1)
94
+ if e && e != s + 1
95
+ key = key[s + 1..e - 1]
96
+ end
97
+ end
98
+ CRC16.crc16(key) % REDIS_CLUSTER_HASH_SLOTS
99
+ end
100
+
101
+ def call_with_redirection(slot, command, &block)
102
+ nodes = @cluster_info.nodes.dup
103
+ node = @cluster_info.slot_node(slot) || nodes.sample
104
+
105
+ redirect_count = 0
106
+ connect_error_count = 0
107
+ connect_retry_count = 0
108
+ asking = false
109
+ loop do
110
+ nodes.delete(node)
111
+
112
+ conn = fetch_connection(node)
113
+ reply = conn.call(command, asking, &block)
114
+ case reply
115
+ when ConnectionAdapters::Reply::RedirectionError
116
+ asking = reply.ask
117
+ node = @cluster_info.add_node(reply.ip, reply.port)
118
+ @refresh_slots ||= !asking
119
+
120
+ redirect_count += 1
121
+ raise RedirectionError, "Too many redirections" if @options[:max_redirection] < redirect_count
122
+ when ConnectionAdapters::Reply::ConnectionError
123
+ connect_error_count += 1
124
+ raise reply.cause if @options[:max_connection_error] < connect_error_count
125
+ if nodes.empty?
126
+ connect_retry_count += 1
127
+ sleep(sleep_interval(connect_retry_count))
128
+ else
129
+ asking = false
130
+ node = nodes.sample
131
+ end
132
+ else
133
+ return reply
134
+ end
135
+ end
136
+ end
137
+
138
+ def refresh_slots
139
+ slots = nil
140
+ @cluster_info.nodes.each do |node|
141
+ conn = fetch_connection(node)
142
+ reply = conn.call(["cluster", "slots"])
143
+ case reply
144
+ when ConnectionAdapters::Reply::RedirectionError
145
+ fail "never reach here"
146
+ when ConnectionAdapters::Reply::ConnectionError
147
+ if @logger
148
+ @logger.warn("CLUSTER SLOTS command failed: node_name=#{node[:name]}, message=#{reply.cause}")
149
+ end
150
+ else
151
+ slots = reply
152
+ break
153
+ end
154
+ end
155
+ return unless slots
156
+
157
+ @cluster_info = ClusterInfo.new.tap do |cluster_info|
158
+ cluster_info.load_slots(slots)
159
+ end
160
+
161
+ (@connections.keys - @cluster_info.node_names).each do |key|
162
+ conn = @connections.delete(key)
163
+ conn.close if conn
164
+ end
165
+ end
166
+
167
+ def fetch_connection(node)
168
+ name = node[:name]
169
+ if conn = @connections[name]
170
+ conn
171
+ else
172
+ @connections_mutex.synchronize do
173
+ @connections[name] ||= @adapter_class.new(node[:host], node[:port], @options)
174
+ end
175
+ end
176
+ end
177
+
178
+ def sleep_interval(retry_count)
179
+ factor = 1 + 2 * (rand - 0.5) * @options[:connect_retry_random_factor]
180
+ factor * @options[:connect_retry_interval] * 2**(retry_count - 1)
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,45 @@
1
+ module Renoir
2
+ class ClusterInfo
3
+ class << self
4
+ def node_name(host, port)
5
+ "#{host}:#{port}"
6
+ end
7
+ end
8
+
9
+ def initialize
10
+ @slots = {}
11
+ @nodes = {}
12
+ end
13
+
14
+ def load_slots(slots)
15
+ slots.each do |s, e, master, *slaves|
16
+ ip, port, = master
17
+ name = add_node(ip, port)
18
+ (s..e).each do |slot|
19
+ @slots[slot] = name
20
+ end
21
+ end
22
+ end
23
+
24
+ def slot_node(slot)
25
+ @nodes[@slots[slot]]
26
+ end
27
+
28
+ def node_names
29
+ @nodes.keys
30
+ end
31
+
32
+ def nodes
33
+ @nodes.values
34
+ end
35
+
36
+ def add_node(host, port)
37
+ name = self.class.node_name(host, port)
38
+ @nodes[name] = {
39
+ host: host,
40
+ port: port,
41
+ name: name,
42
+ }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require "renoir/connection_adapters/reply"
2
+
3
+ module Renoir
4
+ module ConnectionAdapters
5
+ autoload :Redis, "renoir/connection_adapters/redis"
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module Renoir
2
+ module ConnectionAdapters
3
+ class Base
4
+ class << self
5
+ def get_keys_from_command(command)
6
+ fail "a connection adapter must override #get_keys_from_command"
7
+ end
8
+ end
9
+
10
+ def call(command, asking=false, &block)
11
+ fail "a connection adapter must override #call"
12
+ end
13
+
14
+ def close
15
+ fail "a connection adapter must override #close"
16
+ end
17
+
18
+ def with_raw_connection
19
+ fail "a connection adapter must override #with_raw_connection"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,95 @@
1
+ require "redis"
2
+ require "renoir/connection_adapters/base"
3
+
4
+ module Renoir
5
+ module ConnectionAdapters
6
+ class Redis < Base
7
+ class << self
8
+ def get_keys_from_command(command)
9
+ case command[0].to_s.downcase.to_sym
10
+ when :append, :bitcount, :bitfield, :bitpos, :decr, :decrby, :dump,
11
+ :expire, :expireat, :geoadd, :geodist, :geohash, :geopos, :get,
12
+ :getbit, :getrange, :getset, :hdel, :hexists, :hget, :hgetall,
13
+ :hincrby, :hincrbyfloat, :hkeys, :hlen, :hmget, :hmset, :hscan,
14
+ :hset, :hsetnx, :hstrlen, :hvals, :incr, :incrby, :incrbyfloat,
15
+ :lindex, :linsert, :llen, :lpop, :lpush, :lpushx, :lrange, :lrem,
16
+ :lset, :ltrim, :move, :persist, :pexpire, :pexpireat, :pfadd,
17
+ :psetex, :pttl, :restore, :rpop, :rpush, :rpushx, :sadd, :scard,
18
+ :set, :setbit, :setex, :setnx, :setrange, :sismember, :smembers,
19
+ :spop, :srandmember, :srem, :sscan, :strlen, :ttl, :type, :zadd,
20
+ :zcard, :zcount, :zincrby, :zlexcount, :zrange, :zrangebylex,
21
+ :zrangebyscore, :zrank, :zrem, :zremrangebylex, :zremrangebyrank,
22
+ :zremrangebyscore, :zrevrange, :zrevrangebylex,
23
+ :zrevrangebyscore, :zrevrank, :zscan, :zscore
24
+ [command[1]]
25
+ when :del, :exists, :mget, :pfcount, :pfmerge, :sdiff, :sdiffstore,
26
+ :sinter, :sinterstore, :sunion, :sunionstore, :touch, :unlink,
27
+ :watch, :rename, :renamenx, :rpoplpush
28
+ command[1..-1]
29
+ when :smove
30
+ command[1..-2]
31
+ when :blpop, :brpop, :brpoplpush
32
+ command[-1].is_a?(Hash) ? command[1..-2] : command[1..-1]
33
+ when :bitop
34
+ command[2..-1]
35
+ when :eval, :evalsha
36
+ (command[2].is_a?(Hash) ? command[2][:keys] : command[2]) || []
37
+ when :georadius, :georadiusbymember
38
+ store_index = command.index { |arg| [:store, :storedist].include?(arg.to_s.downcase.to_sym) }
39
+ [command[1]] + (store_index ? [command[store_index+1]] : [])
40
+ when :migrate
41
+ if command[1].empty?
42
+ # TODO: support multiple keys when the redis-rb gem supports that
43
+ else
44
+ [command[1]]
45
+ end
46
+ when :mset, :msetnx
47
+ ((command.size - 1) / 2).times.map { |count| command[1 + count*2] }
48
+ when :sort
49
+ [command[1]] + (command[2].is_a?(Hash) ? [command[2][:store]] : [])
50
+ when :zinterstore, :zunionstore
51
+ [command[1]] + command[2]
52
+ else
53
+ []
54
+ end
55
+ end
56
+ end
57
+
58
+ def initialize(host, port, options={})
59
+ @conn = ::Redis.new(options.merge(host: host, port: port))
60
+ end
61
+
62
+ def call(command, asking=false, &block)
63
+ command, *args = command
64
+ if asking
65
+ @conn.multi do |tx|
66
+ tx.asking
67
+ tx.send(command, *args, &block)
68
+ end
69
+ else
70
+ @conn.send(command, *args, &block)
71
+ end
72
+ rescue ::Redis::CommandError => e
73
+ errv = e.to_s.split
74
+ type = errv[0].downcase.to_sym
75
+ raise unless [:moved, :ask].include?(type)
76
+
77
+ ip, port = errv[2].split(":")
78
+ Reply::RedirectionError.new(e, type == :ask, ip, port)
79
+ rescue ::Redis::TimeoutError, ::Redis::CannotConnectError => e
80
+ Reply::ConnectionError.new(e)
81
+ end
82
+
83
+ def close
84
+ # Redis#disconnect! is deprecated since redis-rb 3.3.0
85
+ @conn.mon_synchronize do
86
+ @conn.respond_to?(:close) ? @conn.close : @conn.disconnect!
87
+ end
88
+ end
89
+
90
+ def with_raw_connection
91
+ yield @conn
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,27 @@
1
+ module Renoir
2
+ module ConnectionAdapters
3
+ module Reply
4
+ class Base
5
+ attr_reader :cause
6
+
7
+ def initialize(cause)
8
+ @cause = cause
9
+ end
10
+ end
11
+
12
+ class RedirectionError < Base
13
+ attr_reader :ask, :ip, :port
14
+
15
+ def initialize(cause, ask, ip, port)
16
+ super(cause)
17
+ @ask = ask
18
+ @ip = ip
19
+ @port = port
20
+ end
21
+ end
22
+
23
+ class ConnectionError < Base
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,60 @@
1
+ # This is the CRC16 algorithm used by Redis Cluster to hash keys.
2
+ # Implementation according to CCITT standards.
3
+ #
4
+ # This is actually the XMODEM CRC 16 algorithm, using the
5
+ # following parameters:
6
+ #
7
+ # Name : "XMODEM", also known as "ZMODEM", "CRC-16/ACORN"
8
+ # Width : 16 bit
9
+ # Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1)
10
+ # Initialization : 0000
11
+ # Reflect Input byte : False
12
+ # Reflect Output CRC : False
13
+ # Xor constant to output CRC : 0000
14
+ # Output for "123456789" : 31C3
15
+ module Renoir
16
+ module CRC16
17
+ def self.crc16(bytes)
18
+ crc = 0
19
+ bytes.each_byte do |b|
20
+ crc = ((crc << 8) & 0xffff) ^ XMODEM_CRC16_LOOKUP[((crc >> 8) ^ b) & 0xff]
21
+ end
22
+ crc
23
+ end
24
+
25
+ XMODEM_CRC16_LOOKUP = [
26
+ 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
27
+ 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
28
+ 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
29
+ 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
30
+ 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
31
+ 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
32
+ 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
33
+ 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
34
+ 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
35
+ 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
36
+ 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
37
+ 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
38
+ 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
39
+ 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
40
+ 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
41
+ 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
42
+ 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
43
+ 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
44
+ 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
45
+ 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
46
+ 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
47
+ 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
48
+ 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
49
+ 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
50
+ 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
51
+ 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
52
+ 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
53
+ 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
54
+ 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
55
+ 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
56
+ 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
57
+ 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
58
+ ].freeze
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Renoir
2
+ VERSION = "0.1.0"
3
+ end
data/renoir.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 'renoir/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "renoir"
8
+ spec.version = Renoir::VERSION
9
+ spec.authors = ["Hiroshi Saito"]
10
+ spec.email = ["saito.die@gmail.com"]
11
+
12
+ spec.summary = %q{Reliable Redis Cluster client library}
13
+ spec.description = %q{Reliable Redis Cluster client library.}
14
+ spec.homepage = "https://github.com/saidie/renoir"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'redis', "~> 3.2"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: renoir
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroshi Saito
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
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.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Reliable Redis Cluster client library.
70
+ email:
71
+ - saito.die@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/renoir.rb
86
+ - lib/renoir/client.rb
87
+ - lib/renoir/cluster_info.rb
88
+ - lib/renoir/connection_adapters.rb
89
+ - lib/renoir/connection_adapters/base.rb
90
+ - lib/renoir/connection_adapters/redis.rb
91
+ - lib/renoir/connection_adapters/reply.rb
92
+ - lib/renoir/crc16.rb
93
+ - lib/renoir/version.rb
94
+ - renoir.gemspec
95
+ homepage: https://github.com/saidie/renoir
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Reliable Redis Cluster client library
119
+ test_files: []