Class: RailsFailover::Redis::Handler

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/rails_failover/redis/handler.rb

Constant Summary collapse

PRIMARY_ROLE_STATUS =
"role:master"
PRIMARY_LOADED_STATUS =
"loading:0"
VERIFY_FREQUENCY_BUFFER_PERCENT =
20
SOFT_DISCONNECT_TIMEOUT_SECONDS =
1
SOFT_DISCONNECT_POLL_SECONDS =
0.05

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



19
20
21
22
23
24
# File 'lib/rails_failover/redis/handler.rb', line 19

def initialize
  @primaries_down = Concurrent::Map.new
  @clients = Concurrent::Map.new

  super() # Monitor#initialize
end

Instance Method Details

#deregister_client(client) ⇒ Object



41
42
43
44
# File 'lib/rails_failover/redis/handler.rb', line 41

def deregister_client(client)
  id = client.options[:id]
  clients_for_id(id).delete(client)
end

#primaries_down_countObject



50
51
52
# File 'lib/rails_failover/redis/handler.rb', line 50

def primaries_down_count
  primaries_down.size
end

#primary_down?(options) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rails_failover/redis/handler.rb', line 46

def primary_down?(options)
  primaries_down[options[:id]]
end

#register_client(client) ⇒ Object



36
37
38
39
# File 'lib/rails_failover/redis/handler.rb', line 36

def register_client(client)
  id = client.options[:id]
  clients_for_id(id).put_if_absent(client, true)
end

#verify_primary(options) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rails_failover/redis/handler.rb', line 26

def verify_primary(options)
  primary_down(options)

  mon_synchronize do
    return if @thread&.alive?
    logger&.warn "Failover for Redis has been initiated"
    @thread = Thread.new { loop_until_all_up }
  end
end