Class: RailsFailover::Redis::Connector
- Inherits:
-
Redis::Client::Connector
- Object
- Redis::Client::Connector
- RailsFailover::Redis::Connector
- Defined in:
- lib/rails_failover/redis/connector.rb
Instance Method Summary collapse
- #check(client) ⇒ Object
-
#initialize(options) ⇒ Connector
constructor
A new instance of Connector.
- #on_disconnect(client) ⇒ Object
- #resolve ⇒ Object
Constructor Details
#initialize(options) ⇒ Connector
Returns a new instance of Connector.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_failover/redis/connector.rb', line 8 def initialize() original_driver = [:driver] [:primary_host] = [:host] [:primary_port] = [:port] [:driver] = Class.new([:driver]) do def self.connect() is_primary = ([:host] == [:primary_host]) && ([:port] == [:primary_port]) super().tap { |conn| conn.rails_failover_role = is_primary ? PRIMARY : REPLICA } rescue ::Redis::TimeoutError, SocketError, Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENOENT, Errno::ETIMEDOUT, Errno::EINVAL => e Handler.instance.verify_primary() if is_primary raise e end attr_accessor :rails_failover_role def shutdown_socket @sock&.shutdown rescue Errno::ENOTCONN end end [:original_driver] = original_driver .delete(:connector) [:id] ||= "#{[:host]}:#{[:port]}" @replica_options = () @options = .dup end |
Instance Method Details
#check(client) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/rails_failover/redis/connector.rb', line 52 def check(client) Handler.instance.register_client(client) expected_role = Handler.instance.primary_down?(@options) ? REPLICA : PRIMARY if client.connection.rails_failover_role != expected_role raise ::Redis::CannotConnectError, "Opened with unexpected failover role" end end |
#on_disconnect(client) ⇒ Object
60 61 62 |
# File 'lib/rails_failover/redis/connector.rb', line 60 def on_disconnect(client) Handler.instance.deregister_client(client) end |
#resolve ⇒ Object
48 49 50 |
# File 'lib/rails_failover/redis/connector.rb', line 48 def resolve Handler.instance.primary_down?(@options) ? @replica_options : @options end |