Class: RedisClient::SentinelConfig

Inherits:
Object
  • Object
show all
Includes:
Config::Common
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb

Constant Summary collapse

SENTINEL_DELAY =
0.25
DEFAULT_RECONNECT_ATTEMPTS =
2

Instance Attribute Summary

Attributes included from Config::Common

#circuit_breaker, #command_builder, #connect_timeout, #connection_prelude, #custom, #db, #driver, #id, #inherit_socket, #middlewares_stack, #password, #protocol, #read_timeout, #ssl, #ssl_params, #write_timeout

Instance Method Summary collapse

Methods included from Config::Common

#new_client, #new_pool, #server_url, #ssl_context, #username

Constructor Details

#initialize(name:, sentinels:, role: :master, **client_config) ⇒ SentinelConfig

Returns a new instance of SentinelConfig.



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
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 10

def initialize(name:, sentinels:, role: :master, **client_config)
  unless %i(master replica slave).include?(role)
    raise ArgumentError, "Expected role to be either :master or :replica, got: #{role.inspect}"
  end

  @to_list_of_hash = @to_hash = nil
  extra_config = {}
  if client_config[:protocol] == 2
    extra_config[:protocol] = client_config[:protocol]
    @to_list_of_hash = lambda do |may_be_a_list|
      if may_be_a_list.is_a?(Array)
        may_be_a_list.map { |l| l.each_slice(2).to_h }
      else
        may_be_a_list
      end
    end
  end

  @name = name
  @sentinel_configs = sentinels.map do |s|
    case s
    when String
      Config.new(**extra_config, url: s)
    else
      Config.new(**extra_config, **s)
    end
  end
  @sentinels = {}.compare_by_identity
  @role = role
  @mutex = Mutex.new
  @config = nil

  client_config[:reconnect_attempts] ||= DEFAULT_RECONNECT_ATTEMPTS
  @client_config = client_config || {}
  super(**client_config)
end

Instance Method Details

#check_role!(role) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 80

def check_role!(role)
  if @role == :master
    unless role == "master"
      sleep SENTINEL_DELAY
      raise FailoverError, "Expected to connect to a master, but the server is a replica"
    end
  else
    unless role == "slave"
      sleep SENTINEL_DELAY
      raise FailoverError, "Expected to connect to a replica, but the server is a master"
    end
  end
end

#hostObject



59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 59

def host
  config.host
end

#pathObject



67
68
69
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 67

def path
  nil
end

#portObject



63
64
65
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 63

def port
  config.port
end

#resetObject



53
54
55
56
57
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 53

def reset
  @mutex.synchronize do
    @config = nil
  end
end

#retry_connecting?(attempt, error) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 71

def retry_connecting?(attempt, error)
  reset unless error.is_a?(TimeoutError)
  super
end

#sentinel?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 76

def sentinel?
  true
end

#sentinelsObject



47
48
49
50
51
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/redis-client-0.12.0/lib/redis_client/sentinel_config.rb', line 47

def sentinels
  @mutex.synchronize do
    @sentinel_configs.dup
  end
end