4
4
# Copyright, 2020, by David Ortiz.
5
5
# Copyright, 2023-2024, by Samuel Williams.
6
6
7
+ require_relative 'client'
7
8
require 'io/stream'
8
9
9
10
module Async
10
11
module Redis
11
- class SentinelsClient < Client
12
- def initialize ( master_name , sentinels , role = :master , protocol = Protocol ::RESP2 , **options )
12
+ class SentinelClient
13
+ DEFAULT_MASTER_NAME = 'mymaster'
14
+
15
+ include ::Protocol ::Redis ::Methods
16
+ include Client ::Methods
17
+
18
+ def initialize ( endpoints , master_name : DEFAULT_MASTER_NAME , role : :master , protocol : Protocol ::RESP2 , **options )
19
+ @endpoints = endpoints
13
20
@master_name = master_name
14
-
15
- @sentinel_endpoints = sentinels . map do |sentinel |
16
- ::IO ::Endpoint . tcp ( sentinel [ :host ] , sentinel [ :port ] )
17
- end
18
-
19
21
@role = role
20
22
@protocol = protocol
23
+
21
24
@pool = connect ( **options )
22
25
end
23
26
24
- private
27
+ protected
25
28
26
- # Override the parent method. The only difference is that this one needs
27
- # to resolve the master/slave address.
29
+ # Override the parent method. The only difference is that this one needs to resolve the master/slave address.
28
30
def connect ( **options )
29
31
Async ::Pool ::Controller . wrap ( **options ) do
30
32
endpoint = resolve_address
@@ -45,28 +47,30 @@ def resolve_address
45
47
raise ArgumentError , "Unknown instance role #{ @role } "
46
48
end => address
47
49
50
+ Console . info ( self , "Resolved #{ @role } address: #{ address } " )
51
+
48
52
address or raise RuntimeError , "Unable to fetch #{ @role } via Sentinel."
49
53
end
50
54
51
55
def resolve_master
52
- @sentinel_endpoints . each do |sentinel_endpoint |
53
- client = Client . new ( sentinel_endpoint , protocol : @protocol )
56
+ @endpoints . each do |endpoint |
57
+ client = Client . new ( endpoint )
54
58
55
59
begin
56
60
address = client . call ( 'sentinel' , 'get-master-addr-by-name' , @master_name )
57
61
rescue Errno ::ECONNREFUSED
58
62
next
59
63
end
60
64
61
- return :: IO :: Endpoint . tcp ( address [ 0 ] , address [ 1 ] ) if address
65
+ return Endpoint . remote ( address [ 0 ] , address [ 1 ] ) if address
62
66
end
63
67
64
- nil
68
+ return nil
65
69
end
66
70
67
71
def resolve_slave
68
- @sentinel_endpoints . each do |sentinel_endpoint |
69
- client = Client . new ( sentinel_endpoint , protocol : @protocol )
72
+ @endpoints . each do |endpoint |
73
+ client = Client . new ( endpoint )
70
74
71
75
begin
72
76
reply = client . call ( 'sentinel' , 'slaves' , @master_name )
@@ -78,10 +82,10 @@ def resolve_slave
78
82
next if slaves . empty?
79
83
80
84
slave = select_slave ( slaves )
81
- return :: IO :: Endpoint . tcp ( slave [ 'ip' ] , slave [ 'port' ] )
85
+ return Endpoint . remote ( slave [ 'ip' ] , slave [ 'port' ] )
82
86
end
83
87
84
- nil
88
+ return nil
85
89
end
86
90
87
91
def available_slaves ( reply )
0 commit comments