redic-sentinels 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/redic/sentinels.rb +13 -4
- data/lib/redic/sentinels/version.rb +1 -1
- data/spec/sentinels_spec.rb +29 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0e8550b89021d10e045dad3f86d743c37ee9fb2
|
4
|
+
data.tar.gz: 39f57b52ec8f223410a66ef7626417ce8813c828
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a433bf3cdd1d3dcca0c97c9b496b38ddc84e327dedeb47b0e709de68afa517acc972ed3f3d001b3fa693abf669f4ea7dbc853fa78678433e57147caf00070f0
|
7
|
+
data.tar.gz: e0a165dedee7ec34a46df0ba04e4b0c4dc98405342d4f1798121bfd795df8c775c7e7c2ab369f20016dee1f362005596520aa805ca6d75de82ef62f6e724807a
|
data/README.md
CHANGED
@@ -38,7 +38,8 @@ hosts = [
|
|
38
38
|
redis = Redic::Sentinels.new hosts: hosts,
|
39
39
|
master_name: 'mymaster',
|
40
40
|
db: 1, # optional (default: 0)
|
41
|
-
password: 'pass' # optional
|
41
|
+
password: 'pass', # optional
|
42
|
+
max_retries: 5 # optional (default: 3): Number of attempts to reconnect to master
|
42
43
|
|
43
44
|
redis.call 'PING' # => 'PONG'
|
44
45
|
```
|
data/lib/redic/sentinels.rb
CHANGED
@@ -16,6 +16,7 @@ class Redic
|
|
16
16
|
@password = options[:password]
|
17
17
|
@db = options.fetch(:db, 0)
|
18
18
|
@timeout = options[:timeout]
|
19
|
+
@max_retries = options.fetch(:max_retries, 3)
|
19
20
|
|
20
21
|
establish_connection
|
21
22
|
end
|
@@ -66,10 +67,18 @@ class Redic
|
|
66
67
|
attr_reader :redis
|
67
68
|
|
68
69
|
def forward
|
69
|
-
yield
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
yield.tap do |result|
|
71
|
+
@retry_attempts = 0
|
72
|
+
end
|
73
|
+
rescue => ex
|
74
|
+
@retry_attempts ||= 0
|
75
|
+
if @retry_attempts < @max_retries
|
76
|
+
establish_connection
|
77
|
+
@retry_attempts += 1
|
78
|
+
retry
|
79
|
+
else
|
80
|
+
raise ex
|
81
|
+
end
|
73
82
|
end
|
74
83
|
|
75
84
|
def establish_connection
|
data/spec/sentinels_spec.rb
CHANGED
@@ -21,15 +21,18 @@ describe Redic::Sentinels do
|
|
21
21
|
redis.client.configure "redis://#{INVALID_HOSTS.sample}", 10_000_000
|
22
22
|
end
|
23
23
|
|
24
|
-
after do
|
25
|
-
master = Redic.new 'redis://localhost:16379'
|
26
|
-
master.call! 'FLUSHDB'
|
27
|
-
end
|
28
|
-
|
29
24
|
describe 'Connection success' do
|
30
25
|
|
31
26
|
let(:redis) { Redic::Sentinels.new hosts: HOSTS, master_name: MASTER_NAME }
|
32
27
|
|
28
|
+
let(:sentinel) { Redic.new "redis://#{VALID_HOSTS.first}" }
|
29
|
+
|
30
|
+
after do
|
31
|
+
_, port = sentinel.call 'SENTINEL', 'get-master-addr-by-name', MASTER_NAME
|
32
|
+
master = Redic.new "redis://localhost:#{port}"
|
33
|
+
master.call! 'FLUSHDB'
|
34
|
+
end
|
35
|
+
|
33
36
|
it 'call' do
|
34
37
|
redis.call('PING').must_equal 'PONG'
|
35
38
|
redis.call('INVALID_COMMAND').must_be_instance_of RuntimeError
|
@@ -80,18 +83,36 @@ describe Redic::Sentinels do
|
|
80
83
|
redis.commit.must_equal ['PONG', 'PONG']
|
81
84
|
end
|
82
85
|
|
86
|
+
it 'Retry on Master change' do
|
87
|
+
redis.call('PING').must_equal 'PONG'
|
88
|
+
|
89
|
+
redis.call('PING').must_equal 'PONG'
|
90
|
+
redis.queue('PING').must_equal [['PING']]
|
91
|
+
|
92
|
+
sentinel.call 'SENTINEL', 'failover', 'mymaster'
|
93
|
+
|
94
|
+
redis.call('PING').must_equal 'PONG'
|
95
|
+
redis.queue('PING').must_equal [['PING'], ['PING']]
|
96
|
+
|
97
|
+
redis.queue('PING').must_equal [['PING'], ['PING'], ['PING']]
|
98
|
+
redis.commit.must_equal ['PONG', 'PONG', 'PONG']
|
99
|
+
end
|
100
|
+
|
83
101
|
it 'Default DB' do
|
84
|
-
|
102
|
+
ip, port = sentinel.call 'SENTINEL', 'get-master-addr-by-name', MASTER_NAME
|
103
|
+
redis.url.must_equal "redis://#{ip}:#{port}/0"
|
85
104
|
end
|
86
105
|
|
87
106
|
it 'Custom DB' do
|
107
|
+
ip, port = sentinel.call 'SENTINEL', 'get-master-addr-by-name', MASTER_NAME
|
88
108
|
redis = Redic::Sentinels.new hosts: HOSTS, master_name: MASTER_NAME, db: 7
|
89
|
-
redis.url.must_equal
|
109
|
+
redis.url.must_equal "redis://#{ip}:#{port}/7"
|
90
110
|
end
|
91
111
|
|
92
112
|
it 'Auth' do
|
113
|
+
ip, port = sentinel.call 'SENTINEL', 'get-master-addr-by-name', MASTER_NAME
|
93
114
|
redis = Redic::Sentinels.new hosts: HOSTS, master_name: MASTER_NAME, password: 'pass'
|
94
|
-
redis.url.must_equal
|
115
|
+
redis.url.must_equal "redis://:pass@#{ip}:#{port}/0"
|
95
116
|
end
|
96
117
|
|
97
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redic-sentinels
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redic
|