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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 307c740884b5d0f566b0013a076133f2e951cf2c3b24336f83ff8e9510ccf95f
4
- data.tar.gz: 8a4cc918de939e67c6665c45416b05a4230f79bef9b9efa9f5520cd880e2c3f2
3
+ metadata.gz: 6b47ce7eef70576e83fe137949d50e280677f6f5e2444790a9bdf7cc1708f540
4
+ data.tar.gz: eea78a86a311a24926c83ab4450eae6ed59b0f39a60faafdf9affe050074aa7d
5
5
  SHA512:
6
- metadata.gz: ec87d7a536a603645d5b4d1c509f1203156c39b1b2bdeb023629727bf73255b46fda97f3d4bd0e363a01871287cc5571653b1d4a53fc1052759c565dab68d441
7
- data.tar.gz: 4b104f60e06dafd0c8c1170c289113fb844bc3b48b6af74c3eedb09fae1814e0cc0421f29949d7f93322e8edf9fa9779a958de3538940f45ce6418bc8a1cfb0c
6
+ metadata.gz: 46aea75592369b8a0af7a48ee1898d2fdfecb8a80ebc777ba386052709678258d719a7e2e2d4d00f1c6c3ca7d158747eecb0a329373ff3a8f6423c6f70824f27
7
+ data.tar.gz: aa006f95128b6ec9fe600652c62d20c32b948aeff049cf2155d5366b97a57d851631e8de6c128f2f0587c0e4d97a797252b5c2f196a98d7d245e3b0a539d8b53
data/.gitignore CHANGED
@@ -14,3 +14,5 @@
14
14
 
15
15
  # Redis database files
16
16
  *.rdb
17
+
18
+ *.gem
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 `make test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env bash
2
+ make RSPEC_PATH=$1
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rails_failover/version"
4
+ require "redis/patches/client"
4
5
  require "rails_failover/redis"
5
6
 
6
7
  module RailsFailover
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'redis/connector'
2
4
 
3
5
  module RailsFailover
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require_relative 'fallback_handler'
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
- FallbackHandler.instance.master = false
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
- FallbackHandler.instance.master ? @options : @replica_options
36
+ FailoverHandler.instance.master ? @options : @replica_options
37
37
  end
38
38
 
39
39
  def check(client)
40
- FallbackHandler.instance.register_client(client)
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 FallbackHandler
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 mon_synchronize { @master }
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(&:disconnect)
80
- @clients.clear
92
+ @clients.dup.each do |c|
93
+ c.disconnect
94
+ end
81
95
  end
82
96
  end
83
97
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsFailover
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -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
@@ -13,7 +13,7 @@ default:
13
13
  all: start test stop
14
14
 
15
15
  test:
16
- bundle exec rspec
16
+ bundle exec rspec ${RSPEC_PATH}
17
17
 
18
18
  start: start_master start_replica
19
19
  stop: stop_replica stop_master
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.1.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-20 00:00:00.000000000 Z
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/fallback_handler.rb
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: