rails_failover 0.1.0 → 0.2.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/.gitignore +2 -0
- data/README.md +1 -1
- data/bin/rspec +2 -0
- data/lib/rails_failover.rb +1 -0
- data/lib/rails_failover/redis.rb +2 -0
- data/lib/rails_failover/redis/connector.rb +9 -5
- data/lib/rails_failover/redis/{fallback_handler.rb → failover_handler.rb} +18 -4
- data/lib/rails_failover/version.rb +1 -1
- data/lib/redis/patches/client.rb +19 -0
- data/makefile +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b47ce7eef70576e83fe137949d50e280677f6f5e2444790a9bdf7cc1708f540
|
4
|
+
data.tar.gz: eea78a86a311a24926c83ab4450eae6ed59b0f39a60faafdf9affe050074aa7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46aea75592369b8a0af7a48ee1898d2fdfecb8a80ebc777ba386052709678258d719a7e2e2d4d00f1c6c3ca7d158747eecb0a329373ff3a8f6423c6f70824f27
|
7
|
+
data.tar.gz: aa006f95128b6ec9fe600652c62d20c32b948aeff049cf2155d5366b97a57d851631e8de6c128f2f0587c0e4d97a797252b5c2f196a98d7d245e3b0a539d8b53
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -41,7 +41,7 @@ end
|
|
41
41
|
|
42
42
|
## Development
|
43
43
|
|
44
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
45
|
|
46
46
|
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).
|
47
47
|
|
data/bin/rspec
ADDED
data/lib/rails_failover.rb
CHANGED
data/lib/rails_failover/redis.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require_relative 'failover_handler'
|
3
4
|
|
4
5
|
module RailsFailover
|
5
6
|
class Redis
|
@@ -21,8 +22,7 @@ module RailsFailover
|
|
21
22
|
Errno::ETIMEDOUT,
|
22
23
|
Errno::EINVAL => e
|
23
24
|
|
24
|
-
|
25
|
-
FallbackHandler.instance.verify_master(options.dup)
|
25
|
+
FailoverHandler.instance.verify_master(options.dup)
|
26
26
|
raise e
|
27
27
|
end
|
28
28
|
end
|
@@ -33,11 +33,15 @@ module RailsFailover
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def resolve
|
36
|
-
|
36
|
+
FailoverHandler.instance.master ? @options : @replica_options
|
37
37
|
end
|
38
38
|
|
39
39
|
def check(client)
|
40
|
-
|
40
|
+
FailoverHandler.instance.register_client(client)
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_disconnect(client)
|
44
|
+
FailoverHandler.instance.deregister_client(client)
|
41
45
|
end
|
42
46
|
|
43
47
|
private
|
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'monitor'
|
3
4
|
require 'singleton'
|
4
5
|
|
5
6
|
module RailsFailover
|
6
7
|
class Redis
|
7
|
-
class
|
8
|
+
class FailoverHandler
|
8
9
|
include Singleton
|
9
10
|
include MonitorMixin
|
10
11
|
|
@@ -22,13 +23,15 @@ module RailsFailover
|
|
22
23
|
mon_synchronize do
|
23
24
|
return if @thread&.alive?
|
24
25
|
|
26
|
+
self.master = false
|
27
|
+
disconnect_clients
|
25
28
|
RailsFailover::Redis.master_down_callbacks.each { |callback| callback.call }
|
26
29
|
|
27
30
|
@thread = Thread.new do
|
28
31
|
loop do
|
29
32
|
thread = Thread.new { initiate_fallback_to_master(options) }
|
30
33
|
thread.join
|
31
|
-
break if
|
34
|
+
break if self.master
|
32
35
|
sleep (RailsFailover::Redis.verify_master_frequency_seconds + (Time.now.to_i % RailsFailover::Redis.verify_master_frequency_seconds))
|
33
36
|
ensure
|
34
37
|
thread.kill
|
@@ -64,6 +67,16 @@ module RailsFailover
|
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
70
|
+
def deregister_client(client)
|
71
|
+
mon_synchronize do
|
72
|
+
@clients.delete(client)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def clients
|
77
|
+
mon_synchronize { @clients }
|
78
|
+
end
|
79
|
+
|
67
80
|
def master
|
68
81
|
mon_synchronize { @master }
|
69
82
|
end
|
@@ -76,8 +89,9 @@ module RailsFailover
|
|
76
89
|
|
77
90
|
def disconnect_clients
|
78
91
|
mon_synchronize do
|
79
|
-
@clients.each
|
80
|
-
|
92
|
+
@clients.dup.each do |c|
|
93
|
+
c.disconnect
|
94
|
+
end
|
81
95
|
end
|
82
96
|
end
|
83
97
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
# See https://github.com/redis/redis-rb/pull/908
|
6
|
+
class Redis::Client
|
7
|
+
def disconnect
|
8
|
+
if connected?
|
9
|
+
result = connection.disconnect
|
10
|
+
@connector.on_disconnect(self)
|
11
|
+
result
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Redis::Client::Connector
|
17
|
+
def on_disconnect(client)
|
18
|
+
end
|
19
|
+
end
|
data/makefile
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_failover
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alan Tan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -42,12 +42,14 @@ files:
|
|
42
42
|
- README.md
|
43
43
|
- Rakefile
|
44
44
|
- bin/console
|
45
|
+
- bin/rspec
|
45
46
|
- bin/setup
|
46
47
|
- lib/rails_failover.rb
|
47
48
|
- lib/rails_failover/redis.rb
|
48
49
|
- lib/rails_failover/redis/connector.rb
|
49
|
-
- lib/rails_failover/redis/
|
50
|
+
- lib/rails_failover/redis/failover_handler.rb
|
50
51
|
- lib/rails_failover/version.rb
|
52
|
+
- lib/redis/patches/client.rb
|
51
53
|
- makefile
|
52
54
|
- rails_failover.gemspec
|
53
55
|
homepage:
|