redic-sentinels 0.2.0 → 0.3.0

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: 00f2c98ea246c4dee97dca3fe3757fec01f0b1e3
4
- data.tar.gz: 5c871add547ae0d15a034886e5480f284f000e7c
3
+ metadata.gz: f0e8550b89021d10e045dad3f86d743c37ee9fb2
4
+ data.tar.gz: 39f57b52ec8f223410a66ef7626417ce8813c828
5
5
  SHA512:
6
- metadata.gz: 6740acd8892fe316e54a0af8847cb1a110e9e618c461e2c2cd08e68458b06f389e49a2a1cbf24e85146269c50f08d61b922073988e7e74626146534f95fae982
7
- data.tar.gz: 06188323e825f5db6efe3aac9d2546d1c20491c89ff6b6cccdd1ae543c21a09312b780d4fe7f52e235a43cf276b4143f68cf1b7f4214c126dd3dbf52aedaf9f6
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
  ```
@@ -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
- rescue Errno::ECONNREFUSED
71
- establish_connection
72
- retry
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
@@ -1,5 +1,5 @@
1
1
  class Redic
2
2
  class Sentinels
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -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
- redis.url.must_equal 'redis://127.0.0.1:16379/0'
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 'redis://127.0.0.1:16379/7'
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 'redis://:pass@127.0.0.1:16379/0'
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.2.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-05-12 00:00:00.000000000 Z
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redic