redis-sentinel 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +14 -0
- data/Rakefile +8 -0
- data/lib/redis-sentinel.rb +1 -0
- data/lib/redis-sentinel/client.rb +32 -2
- data/lib/redis-sentinel/redis.rb +13 -0
- data/lib/redis-sentinel/version.rb +1 -1
- data/spec/redis-sentinel/client_spec.rb +36 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85e2d012825c2c5f740cd1f33ea4f1f6ac5e8c4a
|
4
|
+
data.tar.gz: 3d21614c18f64cf3e2912521f5390a1eee03fb01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5a62c1d6196a9ae88ee0609af812c98345087ec287f0860f9c5f7da61d5cb420ac7d61849257048e49c2c540cfcd17d75440f6d786e29f121cbe83536747167
|
7
|
+
data.tar.gz: b2186c8627bea8d15615401648b78f15180ffbadf3fa4e25731bc6c54c263cd658b4685c4da3349583f3f7e19a8f80605b409782d0848fdea22c18b11cafd1f7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -46,6 +46,20 @@ There are two additional options:
|
|
46
46
|
2. `:failover_reconnect_wait` (seconds) how long to sleep after each
|
47
47
|
failed reconnect during a failover event. Defaults to 0.1s.
|
48
48
|
|
49
|
+
## Slaves clients
|
50
|
+
|
51
|
+
If you need it, you can get an array of Redis clients, each pointing to one of the slaves:
|
52
|
+
|
53
|
+
client = Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])
|
54
|
+
client.slaves
|
55
|
+
# => [#<Redis client v3.0.7 for redis://127.0.0.1:6380/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6381/0>]
|
56
|
+
|
57
|
+
You can also get an array of all the clients (master + slaves):
|
58
|
+
|
59
|
+
client = Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])
|
60
|
+
client.all_clients
|
61
|
+
# => [#<Redis client v3.0.7 for redis://127.0.0.1:6379/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6380/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6381/0>]
|
62
|
+
|
49
63
|
## Example
|
50
64
|
|
51
65
|
Start redis master server, listen on port 16379
|
data/Rakefile
CHANGED
@@ -16,4 +16,12 @@ RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
|
16
16
|
spec.pattern = "spec/**/*_spec.rb"
|
17
17
|
end
|
18
18
|
|
19
|
+
task :console do
|
20
|
+
require 'irb'
|
21
|
+
require 'irb/completion'
|
22
|
+
require 'redis-sentinel'
|
23
|
+
ARGV.clear
|
24
|
+
IRB.start
|
25
|
+
end
|
26
|
+
|
19
27
|
task :default => :spec
|
data/lib/redis-sentinel.rb
CHANGED
@@ -87,6 +87,36 @@ class Redis::Client
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
+
def discover_slaves
|
91
|
+
while true
|
92
|
+
try_next_sentinel
|
93
|
+
|
94
|
+
begin
|
95
|
+
slaves_info = current_sentinel.sentinel("slaves", @master_name)
|
96
|
+
@slaves = slaves_info.map do |info|
|
97
|
+
info = Hash[*info]
|
98
|
+
::Redis.new :host => info['ip'], :port => info['port'], :driver => info[:driver]
|
99
|
+
end
|
100
|
+
|
101
|
+
break
|
102
|
+
rescue Redis::CommandError => e
|
103
|
+
raise unless e.message.include?("IDONTKNOW")
|
104
|
+
rescue Redis::CannotConnectError
|
105
|
+
# failed to connect to current sentinel server
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def slaves
|
111
|
+
discover_slaves
|
112
|
+
@slaves
|
113
|
+
end
|
114
|
+
|
115
|
+
def all_clients
|
116
|
+
clients = slaves
|
117
|
+
clients.unshift ::Redis.new @options
|
118
|
+
end
|
119
|
+
|
90
120
|
def disconnect_with_sentinels
|
91
121
|
current_sentinel.client.disconnect if current_sentinel
|
92
122
|
disconnect_without_sentinels
|
@@ -140,8 +170,8 @@ class Redis::Client
|
|
140
170
|
else
|
141
171
|
uri = URI.parse(opts)
|
142
172
|
sentinel_options << {
|
143
|
-
host
|
144
|
-
port
|
173
|
+
:host => uri.host,
|
174
|
+
:port => uri.port
|
145
175
|
}
|
146
176
|
end
|
147
177
|
end
|
@@ -12,11 +12,17 @@ describe Redis::Client do
|
|
12
12
|
]
|
13
13
|
end
|
14
14
|
|
15
|
+
let(:slaves_reply) do
|
16
|
+
[
|
17
|
+
["ip", "slave-0", "port", "6379"],
|
18
|
+
["ip", "slave-1", "port", "6380"],
|
19
|
+
["ip", "slave-2", "port", "6381"]
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
15
23
|
subject { Redis::Client.new(:master_name => "master", :master_password => "foobar",
|
16
24
|
:sentinels => sentinels) }
|
17
25
|
|
18
|
-
before { allow(Redis).to receive(:new).and_return(current_sentinel) }
|
19
|
-
|
20
26
|
context "new instances" do
|
21
27
|
it "should parse sentinel options" do
|
22
28
|
expect(subject.instance_variable_get(:@sentinels_options)).to eq [
|
@@ -58,6 +64,8 @@ describe Redis::Client do
|
|
58
64
|
end
|
59
65
|
|
60
66
|
context "#try_next_sentinel" do
|
67
|
+
before { allow(Redis).to receive(:new).and_return(current_sentinel) }
|
68
|
+
|
61
69
|
it "returns next sentinel server" do
|
62
70
|
expect(subject.try_next_sentinel).to eq current_sentinel
|
63
71
|
end
|
@@ -122,6 +130,32 @@ describe Redis::Client do
|
|
122
130
|
end
|
123
131
|
end
|
124
132
|
|
133
|
+
context "#discover_slaves" do
|
134
|
+
before do
|
135
|
+
allow(subject).to receive(:try_next_sentinel)
|
136
|
+
allow(subject).to receive(:refresh_sentinels_list)
|
137
|
+
allow(subject).to receive(:current_sentinel).and_return(current_sentinel)
|
138
|
+
expect(current_sentinel).to receive(:sentinel).with("slaves", "master").and_return(slaves_reply)
|
139
|
+
|
140
|
+
slaves = slaves_reply.map do |info|
|
141
|
+
info = Hash[*info]
|
142
|
+
double("Redis", :host => info['ip'], :port => info['port'])
|
143
|
+
end
|
144
|
+
|
145
|
+
allow(Redis).to receive(:new).and_return(*slaves)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "discovers slaves correctly" do
|
149
|
+
slaves = subject.slaves
|
150
|
+
|
151
|
+
expect(slaves.size).to eq 3
|
152
|
+
3.times do |i|
|
153
|
+
expect(slaves[i].host).to eq "slave-#{i.to_s}"
|
154
|
+
expect(slaves[i].port.to_i).to eq (6379 + i)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
125
159
|
context "#auto_retry_with_timeout" do
|
126
160
|
context "no failover reconnect timeout set" do
|
127
161
|
subject { Redis::Client.new }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-sentinel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -122,6 +122,7 @@ files:
|
|
122
122
|
- lib/em-synchrony/redis-sentinel.rb
|
123
123
|
- lib/redis-sentinel.rb
|
124
124
|
- lib/redis-sentinel/client.rb
|
125
|
+
- lib/redis-sentinel/redis.rb
|
125
126
|
- lib/redis-sentinel/version.rb
|
126
127
|
- redis-sentinel.gemspec
|
127
128
|
- spec/redis-sentinel/client_spec.rb
|