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
@@ -49,24 +51,24 @@ def resolve_address
49
51
end
50
52
51
53
def resolve_master
52
- @sentinel_endpoints . each do |sentinel_endpoint |
53
- client = Client . new ( sentinel_endpoint , protocol : @protocol )
54
+ @endpoints . each do |endpoint |
55
+ client = Client . new ( endpoint )
54
56
55
57
begin
56
58
address = client . call ( 'sentinel' , 'get-master-addr-by-name' , @master_name )
57
59
rescue Errno ::ECONNREFUSED
58
60
next
59
61
end
60
62
61
- return :: IO :: Endpoint . tcp ( address [ 0 ] , address [ 1 ] ) if address
63
+ return Endpoint . remote ( address [ 0 ] , address [ 1 ] ) if address
62
64
end
63
65
64
- nil
66
+ return nil
65
67
end
66
68
67
69
def resolve_slave
68
- @sentinel_endpoints . each do |sentinel_endpoint |
69
- client = Client . new ( sentinel_endpoint , protocol : @protocol )
70
+ @endpoints . each do |endpoint |
71
+ client = Client . new ( endpoint )
70
72
71
73
begin
72
74
reply = client . call ( 'sentinel' , 'slaves' , @master_name )
@@ -78,10 +80,10 @@ def resolve_slave
78
80
next if slaves . empty?
79
81
80
82
slave = select_slave ( slaves )
81
- return :: IO :: Endpoint . tcp ( slave [ 'ip' ] , slave [ 'port' ] )
83
+ return Endpoint . remote ( slave [ 'ip' ] , slave [ 'port' ] )
82
84
end
83
85
84
- nil
86
+ return nil
85
87
end
86
88
87
89
def available_slaves ( reply )
0 commit comments