lib/rails_failover/redis/handler.rb in rails_failover-0.8.1 vs lib/rails_failover/redis/handler.rb in rails_failover-1.0.0

- old
+ new

@@ -1,20 +1,20 @@ # frozen_string_literal: true -require 'monitor' -require 'singleton' -require 'concurrent' +require "monitor" +require "singleton" +require "concurrent" module RailsFailover class Redis class Handler include Singleton include MonitorMixin PRIMARY_ROLE_STATUS = "role:master" PRIMARY_LOADED_STATUS = "loading:0" - VERIFY_FREQUENCY_BUFFER_PRECENT = 20 + VERIFY_FREQUENCY_BUFFER_PERCENT = 20 SOFT_DISCONNECT_TIMEOUT_SECONDS = 1 SOFT_DISCONNECT_POLL_SECONDS = 0.05 def initialize @primaries_down = Concurrent::Map.new @@ -65,11 +65,11 @@ end end def try_fallback_to_primary frequency = RailsFailover::Redis.verify_primary_frequency_seconds - sleep(frequency * ((rand(VERIFY_FREQUENCY_BUFFER_PRECENT) + 100) / 100.0)) + sleep(frequency * ((rand(VERIFY_FREQUENCY_BUFFER_PERCENT) + 100) / 100.0)) active_primaries_keys = {} primaries_down.each do |key, options| info = nil @@ -112,14 +112,15 @@ RailsFailover::Redis.on_failover_callback!(options[:id]) if !already_down end def primaries_down ancestor_pids = nil - value = @primaries_down.compute_if_absent(Process.pid) do - ancestor_pids = @primaries_down.keys - @primaries_down.values.first || Concurrent::Map.new - end + value = + @primaries_down.compute_if_absent(Process.pid) do + ancestor_pids = @primaries_down.keys + @primaries_down.values.first || Concurrent::Map.new + end ancestor_pids&.each do |pid| @primaries_down.delete(pid)&.each { |id, options| verify_primary(options) } end @@ -130,14 +131,15 @@ clients.compute_if_absent(id) { Concurrent::Map.new } end def clients ancestor_pids = nil - clients_for_pid = @clients.compute_if_absent(Process.pid) do - ancestor_pids = @clients.keys - Concurrent::Map.new - end + clients_for_pid = + @clients.compute_if_absent(Process.pid) do + ancestor_pids = @clients.keys + Concurrent::Map.new + end ancestor_pids&.each { |k| @clients.delete(k) } clients_for_pid end def ensure_primary_clients_disconnected @@ -147,12 +149,11 @@ end def disconnect_clients(options, role) id = options[:id] - matched_clients = clients_for_id(id)&.keys - &.filter { |c| c.connection.rails_failover_role == role } - &.to_set + matched_clients = + clients_for_id(id)&.keys&.filter { |c| c.connection.rails_failover_role == role }&.to_set return if matched_clients.nil? || matched_clients.empty? # This is not ideal, but the mutex we need is contained # in the ::Redis instance, not the Redis::Client