1- using ServiceStack ;
2- using ServiceStack . Logging ;
3- using ServiceStack . Redis ;
1+ using ServiceStack . Logging ;
42using System ;
53using System . Collections . Generic ;
6- using System . Diagnostics ;
7- using System . IO ;
84using System . Linq ;
95using System . Text ;
106using System . Threading . Tasks ;
11- using System . Web ;
127
138namespace ServiceStack . Redis
149{
1510 internal class RedisSentinelWorker : IDisposable
1611 {
1712 protected static readonly ILog Log = LogManager . GetLogger ( typeof ( RedisSentinelWorker ) ) ;
1813
19- private RedisClient sentinelClient ;
20- private RedisClient sentinelPubSubClient ;
21- private PooledRedisClientManager clientsManager ;
22- private IRedisSubscription sentinelSubscription ;
23- private string sentinelName ;
14+ private readonly RedisSentinel redisSentinel ;
15+ private readonly RedisClient sentinelClient ;
16+ private readonly RedisClient sentinelPubSubClient ;
17+ private readonly IRedisSubscription sentinelSubscription ;
18+ private readonly string sentinelName ;
2419 private string host ;
20+ private IRedisClientsManager redisManager ;
2521
2622 public event EventHandler SentinelError ;
2723
28- public RedisSentinelWorker ( string host , string sentinelName , PooledRedisClientManager clientsManager = null )
24+ public RedisSentinelWorker ( RedisSentinel redisSentinel , string host , string sentinelName )
2925 {
26+ this . redisSentinel = redisSentinel ;
27+ this . redisManager = redisSentinel . redisManager ;
3028 this . sentinelName = sentinelName ;
3129 this . sentinelClient = new RedisClient ( host ) ;
3230 this . sentinelPubSubClient = new RedisClient ( host ) ;
3331 this . sentinelSubscription = this . sentinelPubSubClient . CreateSubscription ( ) ;
3432 this . sentinelSubscription . OnMessage = SentinelMessageReceived ;
35- this . clientsManager = clientsManager ;
3633
3734 Log . Info ( "Set up Redis Sentinel on {0}" . Fmt ( host ) ) ;
3835 }
@@ -81,33 +78,19 @@ private void ConfigureRedisFromSentinel()
8178 var masters = ConvertMasterArrayToList ( this . sentinelClient . Sentinel ( "master" , this . sentinelName ) ) ;
8279 var slaves = ConvertSlaveArrayToList ( this . sentinelClient . Sentinel ( "slaves" , this . sentinelName ) ) ;
8380
84- if ( this . clientsManager == null )
81+ if ( redisManager == null )
8582 {
86- if ( slaves . Count ( ) > 0 )
87- {
88- this . clientsManager = new PooledRedisClientManager ( masters , slaves ) ;
89- }
90- else
91- {
92- this . clientsManager = new PooledRedisClientManager ( masters . ToArray ( ) ) ;
93- }
83+ redisManager = redisSentinel . RedisManagerFactory . Create ( masters , slaves ) ;
9484 }
9585 else
9686 {
97- if ( slaves . Count ( ) > 0 )
98- {
99- this . clientsManager . FailoverTo ( masters , slaves ) ;
100- }
101- else
102- {
103- this . clientsManager . FailoverTo ( masters . ToArray ( ) ) ;
104- }
87+ ( ( IRedisFailover ) redisManager ) . FailoverTo ( masters , slaves ) ;
10588 }
10689 }
10790
10891 private Dictionary < string , string > ParseDataArray ( object [ ] items )
10992 {
110- Dictionary < string , string > data = new Dictionary < string , string > ( ) ;
93+ var data = new Dictionary < string , string > ( ) ;
11194 bool isKey = false ;
11295 string key = null ;
11396 string value = null ;
@@ -142,7 +125,7 @@ private Dictionary<string, string> ParseDataArray(object[] items)
142125 /// </summary>
143126 /// <param name="items"></param>
144127 /// <returns></returns>
145- private IEnumerable < string > ConvertSlaveArrayToList ( object [ ] slaves )
128+ private List < string > ConvertSlaveArrayToList ( object [ ] slaves )
146129 {
147130 var servers = new List < string > ( ) ;
148131 string ip = null ;
@@ -176,7 +159,7 @@ private IEnumerable<string> ConvertSlaveArrayToList(object[] slaves)
176159 /// </summary>
177160 /// <param name="items"></param>
178161 /// <returns></returns>
179- private IEnumerable < string > ConvertMasterArrayToList ( object [ ] items )
162+ private List < string > ConvertMasterArrayToList ( object [ ] items )
180163 {
181164 var servers = new List < string > ( ) ;
182165 string ip = null ;
@@ -195,11 +178,11 @@ private IEnumerable<string> ConvertMasterArrayToList(object[] items)
195178 return servers ;
196179 }
197180
198- public PooledRedisClientManager GetClientManager ( )
181+ public IRedisClientsManager GetClientManager ( )
199182 {
200183 ConfigureRedisFromSentinel ( ) ;
201184
202- return this . clientsManager ;
185+ return this . redisManager ;
203186 }
204187
205188 public void Dispose ( )
0 commit comments